n,m,p,q = map(int, input().split()) for i in "_"*q: x, f = map(int, input().split()) ans = 0 for i in range(1, m+1): if x * i % p == f: ans += 1 print(ans)