from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def main(): def sitdown(i, j, a, b): used_seat[j] = True ans[i] = j timeline.append([a + b, 0, j, -1]) return n, k1, k2 = MI() q = int(input()) timeline = [] for i in range(q): a, b = MI() # 時刻aに2(来店)したiさんがb時間滞在 heappush(timeline, [a, 2, i, b]) # jは席番号でdist[j]は入口からの距離 # k1,k2を1:2に内分する点に入口を設定 # 距離は分母3の分数になるので3倍して整数に dist = [abs(2 * k1 + k2 - 3 * j) for j in range(n + 2)] #print(dist) used_seat = [False] * (n + 2) ans = [-1] * q # nosideが隣に人がいない席 onsideがいる席 noside = [[dist[j], j] for j in range(1, n + 1)] heapify(noside) onside = [] wait = [] vacant = n while timeline: a, e, i, b = heappop(timeline) #print(timeline,a, e, i, b) # 来店 if e: finish = False # 隣に人がいない席があればそこに座らせ、退店時間をtimelineに追加 while noside: d, j = heappop(noside) # 隣にいればその席をonsideに追加 if used_seat[j - 1] or used_seat[j + 1]: heappush(onside, [d, j]) else: sitdown(i, j, a, b) vacant -= 1 finish = True break if finish: continue # nosideで決まらなければonsideで席を探す while onside: d, j = heappop(onside) if used_seat[j]: continue sitdown(i, j, a, b) vacant -= 1 finish = True break # 見つからなければwaitへ if not finish: heappush(wait, [a, e, i, b]) # 退店 else: j = i used_seat[j] = False vacant += 1 # 待ちがいれば、この時間aに待ちからの復帰1としてtimelineに追加 if wait: _, _, i0, b0 = heappop(wait) heappush(timeline, [a, 1, i0, b0]) # 両隣りが空きかどうかでnosideかonsideに席追加 if used_seat[j - 1] or used_seat[j + 1]: heappush(onside, [dist[j], j]) else: heappush(noside, [dist[j], j]) # 各隣が両隣り空きになったらnosideに追加 if j < n and used_seat[j + 1] == False and used_seat[j + 2] == False: heappush(noside, [dist[j + 1], j + 1]) if j - 2 >= 0 and used_seat[j - 1] == False and used_seat[j - 2] == False: heappush(noside, [dist[j - 1], j - 1]) for x in ans: print(x) main()