import sys input = sys.stdin.readline N,M,P,Q = map(int,input().split()) XF = [tuple(map(int,input().split())) for i in range(Q)] for x,f in XF: if x%P==0: print(0 if f else M) continue inv_x = pow(x,P-2,P) y = f*inv_x % P d,m = divmod(M,P) print(d + int(m >= y) - (f == 0))