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 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;



public class A
{
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    static int gcd(int a,int b)
	{
		if(a == 0) return b;
		if(b == 0) return a;
		
		return gcd(b,a%b);
	}


    public static void main(String[] args)
    {
        OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);
       
        
        
        int t = sc.nextInt();
        while(t-- > 0)
        {
        	int a = sc.nextInt();
        	int b = sc.nextInt();
        	
        	if(a > b)
        	{
        		int temp = a;
        		a = b;
        		b = temp;
        	}
        	int q = sc.nextInt();
        	int d = b-1;
        	
        	int lcm = (a*b)/gcd(a,b);
        	//System.out.println(lcm);
        	HashSet<Long> hs = new HashSet<>();
        	for(int i = 0; i < q; i++)
        	{	
        		
        		
        		long l = sc.nextLong();
        		long r = sc.nextLong();
        		long ol = l;
        		long or = r;
        		long ans = 0L;
        		
        		long cans = 0L;
        		
        		for(long j = l; j <= r; j++)
        		{
        			if((j%a)%b == (j%b)%a)
        			{	
        				//System.out.println(j+" j");
        				cans++;
        			}
        		}
        		
        		//System.out.println(r-l+1-cans+" bf");
        		if(l <= d && r <= d)
        		{
        			ans = r-l+1;
            		out.print(0+" ");
            		continue;
        		}
        		
        		else if(l <= d && r > d)
        		{
        			ans = d-l+1;
        			l = d+1;
        		}
        		
        		//System.out.println("l "+ans);
        		long st = 0;
        		if(l%lcm == 0)
        		{
        			st = l;
        		}
        		else
        		{	
        			
        			st = 1L*(1L*(l/lcm) + 1)*lcm;
        			
        			long j = l;
        			//System.out.println(j+" "+st);
        			int cr = 100; 
        			while(j <= r && j < st)
        			{
        				if((j%a)%b == (j%b)%a)
        				{	
        					hs.add(j);
        					//System.out.println(j+" jl");
        					ans++;
        				}
        				else break;
        				j++;
        			}
        		}
        		
        		long en = 0;
        		if(r%lcm == 0)
        		{	
        			ans++;
        			en = r-lcm;
        		}
        		else
        		{	
        			
        			en = 1L*(1L*(r/lcm) - 1)*lcm;
        			
        			long j = 1L*(1L*(r/lcm))*lcm;
        			
        			while(true && j <= r && j >= l)
        			{
        				if((j%a)%b == (j%b)%a)
        				{	
        					if(hs.contains(j)) break;
        					System.out.println(j+" jr");
        					ans++;
        				}
        				else break;
        				j++;
        			}
        		}
        		//System.out.println(st+" "+en);
        		if(st <= en)
        		{
        			long x = st/lcm;
        			long y = en/lcm;
        			
        			long cnt = (y-x+1);
        			
        			ans += cnt*(b);
        		}
        		ans = or-ol+1 - ans;
        		out.print(ans+" ");
        	}
        	
        	out.println();
        }
        
        out.close();
    }

}