import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.PriorityQueue; 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<>(); for (int i = 0; i < q; i++) { que.add(arr[i]); } PriorityQueue que2 = new PriorityQueue<>(); int cnt = 0; while (!que.isEmpty()) { Obj cur = que.poll(); if (cur.t == cur.v) { int d = id[cur.i]; cnt++; Obj next = que.peek(); if (next != null && next.v == cur.v && next.t == next.v) { set2.add(d); set3.add(d); int il = di[d] - 1; if (il >= 0) { if (set2.contains(id[il])) { set3.add(id[il]); } } int ir = di[d] + 1; if (ir < n) { if (set2.contains(id[ir])) { set3.add(id[ir]); } } continue; } set2.add(d); set3.add(d); int il = di[d] - 1; if (il >= 0) { if (set2.contains(id[il])) { set3.add(id[il]); } } int ir = di[d] + 1; if (ir < n) { if (set2.contains(id[ir])) { set3.add(id[ir]); } } for (int o : set3) { int im = di[o]; if ((im == 0 || set1.contains(id[di[o] - 1]) || set2.contains(id[di[o] - 1])) && (im == n - 1 || set1.contains(id[di[o] + 1]) || set2.contains(id[di[o] + 1]))) { set1.add(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; } } int il = di[d] - 1; if (il >= 0) { if (set1.remove(id[il])) { set2.add(id[il]); } } int ir = di[d] + 1; if (ir < n) { if (set1.remove(id[ir])) { set2.add(id[ir]); } } 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 implements Comparable { int i, a, t, v; @Override public int compareTo(Obj o) { if (v != o.v) { return v - o.v; } if (t == v) { return -1; } if (o.t == o.v) { return 1; } return 0; } } }