#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() template using minheap = priority_queue, greater>; int main() { int N, K1, K2; cin >> N >> K1 >> K2; int Q; cin >> Q; set can0, can1; const int inf = 1e9; can0.insert(-inf); can0.insert(inf); can1.insert(-inf); can1.insert(inf); for (int i = 1; i <= N; i++) { can0.insert(i); can1.insert(i); } minheap> in, out; vector A(Q), B(Q); for (int i = 0; i < Q; i++) { cin >> A[i] >> B[i]; in.emplace(A[i], i); } vector ans(Q, -1); auto ins = [&](int k, int id) { ans[id] = k; can0.erase(k - 1); can0.erase(k); can0.erase(k + 1); can1.erase(k); }; auto del = [&](int k) { can1.insert(k); for (int i = max(1, k - 1); i <= min(N, k + 1); i++) { if ((i - 1 == 0 || can1.count(i - 1)) && can1.count(i) && (i + 1 == N + 1 || can1.count(i + 1))) { can0.insert(i); } } }; ll now = 0; while (!in.empty()) { now = max(now, in.top().first); while (!out.empty() && out.top().first <= now) { del(ans[out.top().second]); out.pop(); } if (can1.size() == 2) { now = out.top().first; } while (!out.empty() && out.top().first <= now) { del(ans[out.top().second]); out.pop(); } int k = in.top().second; in.pop(); out.emplace(now + B[k], k); if (K1 < K2) { int l = *prev(can0.upper_bound(K1)); int r = *can0.lower_bound(K2); if (l == -inf && r == inf) { int ll = *prev(can1.upper_bound(K1)); int rr = *can1.lower_bound(K2); if (K1 - ll <= rr - K2) { ins(ll, k); } else { ins(rr, k); } } else if (K1 - l <= r - K2) { ins(l, k); } else { ins(r, k); } } else { int l = *prev(can0.upper_bound(K2)); int r = *can0.lower_bound(K1); if (l == -inf && r == inf) { int ll = *prev(can1.upper_bound(K2)); int rr = *can1.lower_bound(K1); if (K2 - ll >= rr - K1) { ins(rr, k); } else { ins(ll, k); } } else if (K2 - l >= r - K1) { ins(r, k); } else { ins(l, k); } } } for (int i = 0; i < Q; i++) { cout << ans[i] << '\n'; } }