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 sys import stdin
from collections import deque
mod = 10**9 + 7
import sys
import random
# sys.setrecursionlimit(10**6)
from queue import PriorityQueue
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq
# input = lambda: sys.stdin.readline().rstrip()
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

# def ncr(x, y):
#     return f(x) // (f(y) * f(x - y))
def ncr(n, r, p):
    num = den = 1
    for i in range(r):
        num = (num * (n - i)) % p
        den = (den * (i + 1)) % p
    return (num * pow(den,
                      p - 2, p)) % p


t = int(input())

for _ in range(t):

    a,b,q = map(int,input().split())
    ha = []
    a,b = min(a,b),max(a,b)
    for i in range(10**4):
        if (i%a)%b == (i%b)%a:
            ha.append(i)


    d,l = -1,-1
    for i in range(1,len(ha)):
        if ha[i]-ha[i-1]!=1:
            d = ha[i]-ha[i-1]
            l = i
            break

    z = d + l - 1
    ans = []
    for i in range(q):

        z1,z2 = map(int,input().split())
        f = z2-z1+1
        fa = z1
        z1-=1
        d1 = (z1)//z + 1
        d2 = (d1-1)*l
        d3 = ((d1-1)*z) + l-1
        if z1>=d3:
            final1 = d2 + l
        else:
            final1 = d2 + z1 - ((d1-1)*z) + 1


        # print(final1)
        z1 = z2
        d1 = (z1)//z + 1
        d2 = (d1-1)*l
        d3 = ((d1-1)*z) + l-1
        if z1>=d3:
            final2 = d2 + l
        else:
            final2 = d2 + z1 - ((d1-1)*z) + 1

        ans.append(f-(final2-final1))
        # print(f-(final2-final1))
    print(*ans)



