import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; 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 * 2]; 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<>(); Set set3 = new HashSet<>(); 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<>(); int cnt = 0; 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); } } cnt++; Obj next = que.peek(); if (next != null && next.v == cur.v && next.t == next.v) { set3.add(d); continue; } if (cnt > 1) { set3.add(d); for (int o : set3) { if (o == 0) { if ((set1.contains(1) || set2.contains(1)) && (set1.contains(2) || set2.contains(2))) { set1.add(0); } } else if (o == 1) { if ((set1.contains(0) || set2.contains(0)) && (set1.contains(3) || set2.contains(3))) { set1.add(1); } } else { if ((set1.contains(o - 2) || set2.contains(o - 2)) && (set1.contains(o + 2) || set2.contains(o + 2))) { set1.add(o); } } } for (int o : set3) { if (set1.contains(o)) { set2.remove(o); } } set3.clear(); } while (!que2.isEmpty() && cnt > 0) { que.add(que2.poll()); cnt--; } } else { Integer d = set1.pollFirst(); if (d == null) { d = set2.pollFirst(); if (d == null) { que2.add(cur); cnt = 0; 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(); pw.close(); } static class Obj { int i, a, t, v; } }