import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int p = sc.nextInt(); int q = sc.nextInt(); //int[] ans = new int[q]; for (int i = 0; i < q ; i++){ int x = sc.nextInt(); int f = sc.nextInt(); int cnt = 0; for (int j = 1; j <= m ; j++){ if((x * j) % p == f){ cnt++; } } System.out.println(cnt); } } }