n, m, p, q = map(int, input().split()) count = [[0]*p for _ in range(n+1)] for x in range(1, n+1): for y in range(1, m+1): count[x][(x*y)%p] += 1 for _ in range(q): x, f = map(int, input().split()) print(count[x][f])