#include #include #include #include #include #include #include using namespace std; class query { public: int tp, id; long long t; query() : tp(-1), t(-1), id(-1) {}; query(int tp_, long long t_, int id_) : tp(tp_), t(t_), id(id_) {}; bool operator<(const query& q) const { return t * 2 - tp > q.t * 2 - q.tp; } }; int main() { int N, K1, K2; cin >> N >> K1 >> K2; --K1, --K2; vector ord(N); for (int i = 0; i < N; ++i) { int d1 = abs(i - K1), d2 = abs(i - K2); if (d1 < d2) { ord[i] = 2 * d1; } else { ord[i] = 2 * d2 + 1; } } vector ordsorted = ord; sort(ordsorted.begin(), ordsorted.end()); for (int i = 0; i < N; ++i) { ord[i] = lower_bound(ordsorted.begin(), ordsorted.end(), ord[i]) - ordsorted.begin(); } vector ordinv(N); for (int i = 0; i < N; ++i) { ordinv[ord[i]] = i; } int Q; cin >> Q; vector A(Q), B(Q); for (int i = 0; i < Q; ++i) { cin >> A[i] >> B[i]; } priority_queue que; for (int i = 0; i < Q; ++i) { que.push(query(0, A[i], i)); } queue que2; set sl; vector state(N, 0); for (int i = 0; i < N; ++i) { sl.insert(i); } vector ans(Q); while (!que.empty()) { query u = que.top(); que.pop(); bool repeat = (!que.empty() && u.t == que.top().t); if (u.tp == 0) { int val = *sl.begin(); if (val >= 2 * N) { que2.push(u.id); } else { val = ordinv[val % N]; sl.erase(state[val] * N + ord[val]); state[val] = 2; sl.insert(state[val] * N + ord[val]); for (int i = val - 1; i <= val + 1; i += 2) { if (!(0 <= i && i < N) || state[i] == 2) continue; bool flag = false; if (i >= 1 && state[i - 1] == 2) flag = true; if (i <= N - 2 && state[i + 1] == 2) flag = true; sl.erase(state[i] * N + ord[i]); state[i] = (flag ? 1 : 0); sl.insert(state[i] * N + ord[i]); } ans[u.id] = val; que.push(query(1, u.t + B[u.id], val)); } } if (u.tp == 1) { int val = u.id; sl.erase(state[val] * N + ord[val]); bool pflag = false; if (val >= 1 && state[val - 1] == 2) pflag = true; if (val <= N - 2 && state[val + 1] == 2) pflag = true; state[val] = (pflag ? 1 : 0); sl.insert(state[val] * N + ord[val]); for (int i = val - 1; i <= val + 1; i += 2) { if (!(0 <= i && i < N) || state[i] == 2) continue; bool flag = false; if (i >= 1 && state[i - 1] == 2) flag = true; if (i <= N - 2 && state[i + 1] == 2) flag = true; sl.erase(state[i] * N + ord[i]); state[i] = (flag ? 1 : 0); sl.insert(state[i] * N + ord[i]); } } while (!repeat && !que2.empty()) { int id = que2.front(); int val = *sl.begin(); if (val >= 2 * N) { break; } else { que2.pop(); val = ordinv[val % N]; sl.erase(state[val] * N + ord[val]); state[val] = 2; sl.insert(state[val] * N + ord[val]); for (int i = val - 1; i <= val + 1; i += 2) { if (!(0 <= i && i < N) || state[i] == 2) continue; bool flag = false; if (i >= 1 && state[i - 1] == 2) flag = true; if (i <= N - 2 && state[i + 1] == 2) flag = true; sl.erase(state[i] * N + ord[i]); state[i] = (flag ? 1 : 0); sl.insert(state[i] * N + ord[i]); } que.push(query(1, u.t + B[id], val)); ans[id] = val; } } } for (int i = 0; i < Q; ++i) { cout << ans[i] + 1 << '\n'; } return 0; }