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.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.*;
public class Main {
InputStream is;
	PrintWriter out;
	String INPUT = ""; 
//class  Declaration

static class pair {
	int x;
	boolean[] pre ;
	
	
	pair (int i,boolean[] p)
	{ x=i; 
		pre = p;
		
	}
	// public int compareTo(pair p){
	// 	if(this.x!=p.x) { return this.x-p.x;}
	// 	else { return this.y-p.y;}
	// }
	// public int hashCode() { return (x+" "+y).hashCode();}
	public String toString(){ return x+"";} 
	// public boolean equals(Object o){ 
	// 	pair x = (pair) o ;
	// 	return (x.x==this.x&&x.y==this.y);}
}

long mod  = (long)1e9 + 7 ;

void solve() throws Exception{
	int t=ni();
	while(t-->0){
		long a=nl(),b=nl(),q=nl();
			long lcm  =(a*b)/gcd(a,b) ; 
			long[] cnt = new long[(int)lcm] ; 
			cnt[0] =0 ;
			for(int i=1;i<lcm;++i){
				cnt[i]= cnt[i-1]  ;
				long left = (i%a)%b  ;
				long right = (i%b)%a  ; 
				if(left != right){
					cnt[i]++;
				}
			}

			while(q-->0){
				long l =nl(),r=nl();
				long lhl =(l/lcm)*lcm ;
				long rhl = (r/lcm)*lcm + lcm ; 

				
				long ans =((rhl - lhl )/lcm)* cnt[(int)lcm-1] ; 
				//print("lhl : rhl "+lhl+" "+rhl+" ans : "+ans);
				//print("");
				// long ta = ans ;
				// for(long i = lhl; i<l;++i){
				// 	long left = (i%a)%b  ;
				// 	long right = (i%b)%a  ; 
				// 	if(left != right){
				// 		ta--;

				// 	}

				// }
				// //print("ans after left shoud : "+ta);
				// for(long i=r+1;i<=rhl;++i){
				// 	long left = (i%a)%b  ;
				// 	long right = (i%b)%a  ; 
				// 	if(left != right){
				// 		ta--;

				// 	}

				// }
				//print("ans after right shoud : "+ta);

				if(l%lcm!=0)
				ans = ans - cnt[(int)((l-lhl-1)%lcm)];
				//print("ans after left : "+ans);
				if(r%lcm==0){
					ans = ans - cnt[cnt.length-1];
				}
				else{
					ans = ans - (cnt[cnt.length-1]- cnt[(int)(r%lcm)]);
				}
				//print("ans after right :"+ans);
				//print("");
				//ans = ans - (cnt[(rhl-r)])
				p(ans+" ");


			}
			pn("");


	}


}

void print(Object o){
System.out.println(o);
System.out.flush();
}

long gcd(long a, long b) 
{ 
if (b == 0) 
return a; 
return gcd(b, a % b);  
}
void run() throws Exception{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
	
	
public static void main(String[] args) throws Exception { new Main().run(); }
	
//output methods
private void pn(Object o)
{
	out.println(o);
}
private void p(Object o)
{
	out.print(o);
}



//input methods
		private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	
	
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
		
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
	void watch(Object ... a) throws Exception{
		int i=1;
		pn("watch starts :");
		for(Object o : a ) {
			//print(o);
			boolean notfound = true;
			if(o.getClass().isArray()){
				
				String type = o.getClass().getName().toString();
				//print("type is "+type);
				switch (type) {
					case "[I":{
						int[] test = (int[])o ;
						pn(i+" "+Arrays.toString(test));
						break;
					}
					case "[[I":{
						int[][] obj = (int[][])o;
						
						pn(i+" "+Arrays.deepToString(obj));
						break;
					}
					case "[J" : {
						
						long[] obj  = (long[])o ;
						pn(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[J": {
						
						long[][] obj = (long[][])o;
						pn(i+" "+Arrays.deepToString(obj));
						break;
					}
					case "[D" :{
						
						double[] obj= (double[])o;
						pn(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[D" :{
						
						double[][] obj = (double[][])o;
						pn(i+" "+Arrays.deepToString(obj));
						break;
					}
					case "[Ljava.lang.String": {
						
						String[] obj = (String[])o ;
						pn(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[Ljava.lang.String": {
						
						String[][] obj = (String[][])o ;
						pn(i+" "+Arrays.deepToString(obj));
						break ; 
					}
					case "[C" :{
						char[] obj = (char[])o ;
						pn(i+" "+Arrays.toString(obj));
						break;
					}
					case "[[C" :{
						
						char[][] obj = (char[][])o;
						pn(i+" "+Arrays.deepToString(obj));
						break;
					}

						
				
					default:{
						pn(i+" type not identified");
						break;
					}
				}
				notfound = false;
				
			}
			if(o.getClass() == ArrayList.class){
				pn(i+" al: "+o);
				notfound = false;
			}
			if(o.getClass() == HashSet.class){
				pn(i+" hs: "+o);
				notfound = false;
			}
			if(o.getClass() == TreeSet.class){
				pn(i+" ts: "+o);
				notfound = false;
			}
			if(o.getClass() == TreeMap.class){
				pn(i+" tm: "+o);
				notfound = false;
			}
			if(o.getClass() == HashMap.class){
				pn(i+" hm: "+o);
				notfound = false;
			}
			if(o.getClass() == LinkedList.class){
				pn(i+" ll: "+o);
				notfound = false;
			}
			if(o.getClass() == PriorityQueue.class){
				pn(i+" pq : "+o);
				notfound = false;
			}
			if(o.getClass() == pair.class){
				pn(i+" pq : "+o);
				notfound = false;
			}
			
			if(notfound){
				pn(i+" unknown: "+o);
			}
			i++;
		}
		pn("watch ends ");
	}

}