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