/******************************************************************/
/* ACM ICPC 2012-2013                                             */
/* Northeastern European Regional Contest                         */
/* St Petersburg - Barnaul - Tashkent - Tbilisi, December 2, 2012 */
/******************************************************************/
/* Problem E. Exact Measurement                                   */
/*                                                                */
/* Original idea         Pavel Mavrin                             */
/* Problem statement     Pavel Mavrin                             */
/* Test set              Pavel Mavrin                             */
/******************************************************************/
/* Checker                                                        */
/*                                                                */
/* Author                Pavel Mavrin                             */
/******************************************************************/

import ru.ifmo.testlib.Checker;
import ru.ifmo.testlib.InStream;
import ru.ifmo.testlib.Outcome;

import java.util.Arrays;

import static ru.ifmo.testlib.Outcome.Type.*;

public class Check implements Checker {

    public Outcome test(InStream inf, InStream ouf, InStream ans) {
        long x = inf.nextLong();
        int n = inf.nextInt();
        Box[] boxes = new Box[n];
        for (int i = 0; i < n; i++) {
            boxes[i] = new Box(i, inf.nextInt(), inf.nextLong());
        }

        long[] p10 = new long[19];
        p10[0] = 1;
        for (int i = 1; i < 19; i++) {
            p10[i] = p10[i - 1] * 10;
        }

        int pa = ouf.nextInt();
        int ja = ans.nextInt();
        if (pa == -1) {
            if (!ouf.seekEoF()) {
                return new Outcome(PE, String.format("Something after -1"));
            }
            if (ja != pa) {
                return new Outcome(WA, String.format("Solution exists, but -1 found"));
            }
            return new Outcome(OK, String.format("%d", ja));
        }

        int[] pres = new int[pa];
        Box[] presBox = new Box[pa];
        boolean[] was = new boolean[n];
        for (int i = 0; i < pa; i++) {
            pres[i] = ouf.nextInt() - 1;
        }
        if (!ouf.seekEoF()) {
            return new Outcome(PE, String.format("Extra information in output file"));
        }

        for (int i = 0; i < pa; i++) {
            if (pres[i] < 0 || pres[i] >= n) return new Outcome(WA, String.format("Wrong number of the box: " + (pres[i] + 1)));
            if (was[pres[i]]) return new Outcome(WA, String.format("Duplicate number of box: " + (pres[i] + 1)));
            was[pres[i]] = true;
            presBox[i] = boxes[pres[i]];
        }

        Arrays.sort(presBox);
        long cur = x;
        for (Box box : presBox) {
            long c = Math.min(box.q, cur / p10[box.k]);
            cur -= c * p10[box.k];
        }

        if (cur != 0) return new Outcome(WA, String.format("Cannot get sum " + x + " from chosen boxes"));

        if (ja == -1) {
            return new Outcome(FAIL, String.format("Expected -1, found %d", pa));
        }
        if (ja < pa) {
            return new Outcome(WA, String.format("Expected %d, found %d", ja, pa));
        }
        if (ja > pa) {
            return new Outcome(FAIL, String.format("Expected %d, found %d", ja, pa));
        }
        return new Outcome(OK, String.format("%d", ja));
    }

    class Box implements Comparable<Box> {
        int id;
        int k;
        long q;

        Box(int id, int k, long q) {
            this.id = id;
            this.k = k;
            this.q = q;
        }

        @Override
        public int compareTo(Box o) {
            return -Integer.compare(k, o.k);
        }
    }

}


