Problem: You are given two integers a and b, and q queries. The i-th query consists of two numbers l_i and r_i, and the answer to it is the number of integers x such that l_i ≤ x ≤ r_i, and ((x mod a) mod b) ≠ ((x mod b) mod a). Calculate the answer for each query.

Recall that y mod z is the remainder of the division of y by z. For example, 5 mod 3 = 2, 7 mod 8 = 7, 9 mod 4 = 1, 9 mod 9 = 0.

Input

The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Then the test cases follow.

The first line of each test case contains three integers a, b and q (1 ≤ a, b ≤ 200; 1 ≤ q ≤ 500).

Then q lines follow, each containing two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}) for the corresponding query.

Output

For each test case, print q integers — the answers to the queries of this test case in the order they appear.

Example

Input


2
4 6 5
1 1
1 3
1 5
1 7
1 9
7 10 2
7 8
100 200


Output


0 0 0 2 4 
0 91 

Solution (in JAVA):

Solution:
import java.io.*;
import java.util.*;

public class Main {
    private static int gcd(int a, int b) {
        if(b == 0) {
            return a;
        }
        return gcd(b, a%b);
    }
    public static void main(String[] args) throws IOException {
        //Scanner f = new Scanner(new File("uva.in"));
        //Scanner f = new Scanner(System.in);
        //BufferedReader f = new BufferedReader(new FileReader("uva.in"));
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int t = Integer.parseInt(f.readLine());
        while(t-- > 0) {
            StringTokenizer st = new StringTokenizer(f.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            int[] mod1 = new int[a];
            for(int i = 0; i < a; i++) {
                mod1[i] = i%a%b;
            }
            int[] mod2 = new int[b];
            for(int i = 0; i < b; i++) {
                mod2[i] = i%b%a;
            }
            int lcm = a*b/gcd(a, b);
            int[] mod = new int[lcm+1];
            for(int i = 0; i < lcm; i++) {
                mod[i+1] = mod1[i%a] == mod2[i%b] ? 0 : 1;
            }
            for(int i = 1; i <= lcm; i++) {
                mod[i] += mod[i-1];
            }
            int q = Integer.parseInt(st.nextToken());
            long[] res = new long[q];
            for(int i = 0; i < q; i++) {
                st = new StringTokenizer(f.readLine());
                long l = Long.parseLong(st.nextToken());
                long r = Long.parseLong(st.nextToken());
                if(r-l < lcm) {
                    int ll = (int) (l%lcm);
                    int rr = (int) (r%lcm);
                    if(ll > rr) {
                        res[i] = mod[lcm]-mod[ll]+mod[rr+1];
                    } else {
                        res[i] = mod[rr+1]-mod[ll];
                    }
                    continue;
                }
                int temp1 = (int) (lcm-l%lcm);
                int temp2 = (int) (r%lcm+1);
                long ll = l+temp1;
                long rr = r-temp2;
                long ans = (rr-ll+1)/lcm*mod[lcm];
                ans += mod[lcm]-mod[lcm-temp1];
                ans += mod[temp2];
                res[i] = ans;
            }
            out.print(res[0]);
            for(int i = 1; i < q; i++) {
                out.print(" " + res[i]);
            }
            out.println();
        }
        f.close();
        out.close();
    }
}