#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define MP make_pair #define PB push_back #define inf 1000000007 #define mod 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define int long long signed main(){ cin.tie(0); ios::sync_with_stdio(false); int n,k1,k2; bool f = 0; cin >> n >> k1 >> k2; if(k1< k2){ f = 1; } set A,B,C; priority_queue,vector>,greater> > T; int q; cin >> q; rep(i,n){ A.insert(i+1); } int time = 0; vector res(q+1); rep(i,q){ int a,b; cin >> a >> b; while(!T.empty()){ auto x = T.top(); if(x.first <= a){ T.pop(); int k = x.second; C.erase(k); if(B.count(k-1)==1){ if(C.count(k-2)==0){ B.erase(k-1); A.insert(k-1); } } if(B.count(k+1)==1){ if(C.count(k+2)==0){ B.erase(k+1); A.insert(k+1); } } if(C.count(k-1)==1||C.count(k+1)==1){ B.insert(k); }else{ A.insert(k); } }else{ break; } } time = a; if(C.size()==n){ auto yy = T.top(); time = yy.first; while(!T.empty()){ auto x = T.top(); if(x.first == time){ T.pop(); int k = x.second; C.erase(k); if(B.count(k-1)==1){ if(C.count(k-2)==0){ B.erase(k-1); A.insert(k-1); } } if(B.count(k+1)==1){ if(C.count(k+2)==0){ B.erase(k+1); A.insert(k+1); } } if(C.count(k-1)==1||C.count(k+1)==1){ B.insert(k); }else{ A.insert(k); } }else{ break; } } } if(A.size()!=0){ auto ppp = A.lower_bound(k1); int L = -inf; if(ppp!=A.begin()){ ppp--; L = *ppp; } auto qqq =A.lower_bound(k1); int R = inf; if(qqq!=A.end()){ R = *qqq; } if(f){ if(R-k1 <= k1-L){ res[i+1] = R; }else{ res[i+1] = L; } }else{ if(k1-L <= R-k1){ res[i+1] = L ; }else{ res[i+1] = R; } } }else{ auto ppp = B.lower_bound(k1); int L = -inf; if(ppp!=B.begin()){ ppp--; L = *ppp; } auto qqq =B.lower_bound(k1); int R = inf; if(qqq!=B.end()){ R = *qqq; } if(f){ if(R-k1 <= k1-L){ res[i+1] = R; }else{ res[i+1] = L; } }else{ if(k1-L <= R-k1){ res[i+1] = L ; }else{ res[i+1] = R; } } } A.erase(res[i+1]); B.erase(res[i+1]); C.insert(res[i+1]); if(A.count(res[i+1]-1)){ A.erase(res[i+1]-1); B.insert(res[i+1]-1); } if(A.count(res[i+1]+1)){ A.erase(res[i+1]+1); B.insert(res[i+1]+1); } T.push(MP(time+b,res[i+1])); // cerr << "time:" << time << endl; // cerr << "seat:" << res[i+1] << endl; // for(auto x:A){ // cerr << x << " "; // } // cerr << endl; // for(auto x:B){ // cerr << x << " " ; // } // cerr << endl; // for(auto x:C){ // cerr << x << " "; // } // cerr << endl; } for(int i=1;i<=q;i++){ cout << res[i] << "\n"; } return 0; }