n, m, p, q = map(int, input().split()) inv = [0]*(n+1) for i in range(1, n+1): inv[i] = pow(i, p-2, p) for _ in range(q): x, f = map(int, input().split()) y_cand = f*inv[x]%p if y_cand == 0: y_cnt = m//p else: y_cnt = m//p + int(m%p >= y_cand) print(y_cnt)