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 
from math import factorial as fac
from collections import defaultdict
from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
sys.setrecursionlimit(10**6)


def print_case_iterable(case_num, iterable):
	print("Case #{}: {}".format(case_num," ".join(map(str,iterable))))

def print_case_number(case_num, iterable):
	print("Case #{}: {}".format(case_num,iterable))

def print_iterable(A):
	print (' '.join(A))

def read_int():
	return int(f.readline())
def read_int_array():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def read_string():
	return list(f.readline().strip())

NUMBER = 10**9 + 7
# NUMBER = 998244353
def factorial(n) : 
	M = NUMBER
	f = 1
  
	for i in range(1, n + 1):  
		f = (f * i) % M # Now f never can  
						# exceed 10^9+7  
	return f
def mult(a,b):
	return (a * b) % NUMBER

def minus(a , b):
	return (a - b) % NUMBER

def plus(a , b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = egcd(b % a, a)
		return (g, x - (b // a) * y, y)

def modinv(a):
	m = NUMBER
	g, x, y = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
from collections import deque, defaultdict 
import heapq

def bfs(V, E):
	s = set()
	print('\n'.join(map(str,E.items())))
	cnt = 0 
	for v in V:
		if v in s:
			continue
		q = deque([])
		s.add(v)
		q.append(v)
		while len(q) > 0:
			x = q.popleft()
			for e in E[x]:
				if e not in s:
					s.add(e)
					q.append(e)
			print(q)
		cnt += 1
	return cnt
		
from typing import List, Tuple

def regular_bs(L,x,l=None,r=None):
	l = 0 if l == None else l
	r = len(L) if r == None else r
	while (r-l) > 1:
		m = (r+l)//2
		l = m if L[m] <= x else l
		r = m if L[m] > x else r
	cnt = 0
	return l

def binary_count_smaller(L, x):
	l = 0
	if x < L[0]:
		return 0
	r = len(L)
	while r - l > 1:
		mid = (l+r)//2
		if L[mid] < x:
			l = mid
		else:
			r = mid
	return l + 1
def binary_count_larger(L, x):
	l = 0
	if x > L[-1]:
		return 0
	r = len(L)
	while r - l > 1:
		mid = (l+r)//2
		if L[mid] <= x:
			l = mid
		else:
			r = mid
	return len(L) - (r-1)

from itertools import product

def preprocess(A):
	dp = [0]
	for x in A:
		dp.append(dp[-1] + (1 if not x else 0))
	return dp
def solution(a,b,Q,q):
	res = []
	A = []
	m = a*b
	for i in range(m):
		A.append((i % a) %b == (i % b) %a)
	dp = preprocess(A)
	cnt = dp[m]
	for l,r in Q:
		total_cnt = 0
		length = (r-l+1)
		total_cnt += (length // m) * cnt
		remaining = length - m * (length//m) 
		l_m = l % m
		first_part = m - (l_m)
		# print(length, total_cnt, remaining, l_m, first_part, dp)
		# print(dp)
		# print(A)
		# print(f'lm={l_m}')
		if remaining > first_part:
			# print('bla')
			# print(f'dp[m] - dp[l_m] = {dp[m] - dp[l_m]}')
			# print(f'dp[(remaining - first_part) + 1] = {dp[(remaining - first_part) + 1]}')
			# print(f'(remaining - first_part) + 1 = {(remaining - first_part) + 1}')
			total_cnt += dp[m] - dp[l_m]
			total_cnt += dp[(remaining - first_part)]
		else:
			# print('bla')
			total_cnt += dp[l_m+remaining] - dp[l_m]
		res.append(str(total_cnt))
	return ' '.join(res)
		# with dp, calculate from l_m+1 to end, and then from start until remaining




	


	

	

def main():
	T = read_int()
	for i in range(T):
		a,b,q=read_int_array()
		Q = []
		for j in range(q):
			Q.append(read_int_array())
		print(solution(a,b,Q,q))


if __name__ == '__main__':
	main()
# print(regular_bs([1,2],1))