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

public class Check implements Checker {

  private int checkAnswer(int n, int l, int r, int[] cities, InStream ouf, Outcome.Type failOutcome) {
    int x = ouf.nextInt();
    if (!(1 <= x && x <= n)) {
      throw new Outcome(failOutcome, String.format("Invalid count of groups (x = %d)", x));
    }
    int[] number = new int[n];
    for (int i = 1; i <= x; ++i) {
      int L = ouf.nextInt();
      int R = ouf.nextInt();
      if (!(1 <= L && L <= R && R <= n)) {
        throw new Outcome(failOutcome, String.format("Invalid group (i = %d, L = %d, R = %d)", i, L, R));
      }
      if (!(l <= R - L + 1 && R - L + 1 <= r)) {
        throw new Outcome(failOutcome, String.format("Group with invalid length (i = %d, L = %d, R = %d)", i, L, R));
      }
      L--;
      R--;
      for (int j = L; j <= R; ++j) {
        if (number[j] != 0) {
          throw new Outcome(failOutcome, String.format("%d class belongs to two groups (%d and %d)", j, number[j], i));
        }
        number[j] = i;
      }
    }

    int[] sum = new int[x];
    for (int i = 0; i < n; ++i) {
      if (number[i] == 0) {
        throw new Outcome(failOutcome, String.format("%d class doesn't belong to any group", i));
      }
      sum[number[i] - 1] += cities[i];
    }

    int diff = 0;
    for (int i = 0; i < x; ++i) {
      if (sum[i] > 0) {
        diff++;
      } else if (sum[i] < 0) {
        diff--;
      }
    }
    return diff;
  }

  @Override
  public Outcome test(InStream inf, InStream ouf, InStream anf) {
    int n = inf.nextInt();
    int l = inf.nextInt();
    int r = inf.nextInt();
    int[] cities = new int[n];

    for (int i = 0; i < n; ++i) {
      int a = inf.nextInt();
      int b = inf.nextInt();
      cities[i] = a - b;
    }

    int juryAnswer = checkAnswer(n, l, r, cities, anf, Outcome.Type.FAIL);
    int contestantAnswer = checkAnswer(n, l, r, cities, ouf, Outcome.Type.WA);

    if (juryAnswer > contestantAnswer) {
      return new Outcome(Outcome.Type.WA, String.format("Non-optimal solution: %d expected, %d found", juryAnswer, contestantAnswer));
    }
    if (juryAnswer < contestantAnswer) {
      return new Outcome(Outcome.Type.FAIL, String.format("Contestant's solution is better than jury's one: %d expected, %d found", juryAnswer, contestantAnswer));
    }
    return new Outcome(Outcome.Type.OK, String.format("n = %d, l = %d, r = %d, diff = %d", n, l, r, juryAnswer));
  }
}
