n,m,p,q = map(int,input().split()) for _ in range(q): x,f = map(int,input().split()) y = f * pow(x,p-2,p) % p ans = m//p if 0 < y <= m%p: ans += 1 print(ans)