n, m, p, q = map(int, input().split()) for _ in range(q): x, f = map(int, input().split()) count = 0 start_index = -1 for y in range(1, p+1): c = (x * y) % p if c == f: start_index = y break if start_index == -1: print(0) continue show = m // p if m % p >= start_index: show += 1 print(show)