import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.PriorityQueue; import java.util.Queue; import java.util.TreeSet; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int k1 = Integer.parseInt(sa[1]) - 1; int k2 = Integer.parseInt(sa[2]) - 1; int q = Integer.parseInt(br.readLine()); Obj[] arr = new Obj[q]; for (int i = 0; i < q; i++) { sa = br.readLine().split(" "); Obj o = new Obj(); o.a = Integer.parseInt(sa[0]); o.t = o.a + Integer.parseInt(sa[1]); o.v = o.a; arr[i] = o; } br.close(); int[] id = new int[n]; int[] di = new int[n]; di[0] = k1; for (int i = k1 - 1; i >= 0; i--) { id[i] = (k1 - i) * 2; if (k2 < k1) { id[i]--; } di[id[i]] = i; } for (int i = k1 + 1; i < n; i++) { id[i] = (i - k1) * 2; if (k2 > k1) { id[i]--; } di[id[i]] = i; } TreeSet set1 = new TreeSet<>(); TreeSet set2 = new TreeSet<>(); for (int i = 0; i < n; i++) { set1.add(id[i]); } PriorityQueue que = new PriorityQueue<>((o1, o2) -> { if (o1.v != o2.v) { return o1.v - o2.v; } if (o1.t == o1.v) { return -1; } if (o2.t == o2.v) { return 1; } return 0; }); for (int i = 0; i < q; i++) { que.add(arr[i]); } Queue que2 = new ArrayDeque<>(); while (!que.isEmpty()) { Obj cur = que.poll(); if (cur.t == cur.v) { int d = id[cur.i]; if (d == 0) { if (set2.contains(1) && set2.contains(2)) { set1.add(0); } else { set2.add(0); } } else if (d == 1) { if (set2.contains(0) && set2.contains(3)) { set1.add(1); } else { set2.add(1); } } else { if (set2.contains(d - 2) && set2.contains(d + 2)) { set1.add(d); } else { set2.add(d); } } if (!que2.isEmpty()) { que.add(que2.poll()); } } else { Integer d = set1.pollFirst(); if (d == null) { d = set2.pollFirst(); if (d == null) { que2.add(cur); continue; } } else { if (d == 0 && set1.remove(d + 1)) { set2.add(d + 1); } if (set1.remove(d + 2)) { set2.add(d + 2); } } cur.i = di[d]; cur.v = cur.t; que.add(cur); } } PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < q; i++) { pw.println(arr[i].i + 1); } pw.flush(); } static class Obj { int i, a, t, v; } }