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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
long long quick_power_mod(long long a, long long b, long long m) {
  a %= m;
  long long result = 1;
  while (b > 0) {
    if (b & 1) result = (result * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return result;
}
long long comp(long long a, long long b, long long p) {
  if (a < b) return 0;
  if (a == b) return 1;
  if (b > a - b) b = a - b;
  long long ans = 1, ca = 1, cb = 1;
  for (long long i = 0; i < b; i++) {
    ca = (ca * (a - i)) % p;
    cb = (cb * (b - i)) % p;
  }
  ans = (ca * quick_power_mod(cb, p - 2, p)) % p;
  return ans;
}
long long lucas(long long n, long long m, long long p) {
  long long ans = 1;
  while (n && m && ans) {
    ans = (ans * comp(n % p, m % p, p)) % p;
    n /= p;
    m /= p;
  }
  return ans;
}
long long ssum(long long x) {
  long long s = 0;
  for (long long i = 0; i < x; i++) {
    s += i;
  }
  return s;
}
long long visss[1005];
void Prime() {
  for (long long i = 2; i < 1000; i++) {
    if (visss[i] == 0) {
      for (long long j = 2 * i; j < 1000; j += i) {
        visss[j] = 1;
      }
    }
  }
}
const long long N = 3e5 + 10;
long long a, b, q, lcmm;
long long s[N];
long long calc(long long x) { return s[lcmm] * (x / lcmm) + s[x % lcmm]; }
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  lucas(1000, 100, 10007);
  while (t--) {
    lucas(1000, 100, 10007);
    cin >> a >> b >> q;
    lucas(1000, 100, 10007);
    lcmm = a * b / gcd(a, b);
    lucas(1000, 100, 10007);
    for (long long i = 1; i <= lcmm; i++)
      s[i] = s[i - 1] + (i % a % b != i % b % a);
    lucas(1000, 100, 10007);
    while (q--) {
      long long tmp1 = quick_power_mod(10000, 1000, 100007);
      long long l, r;
      cin >> l >> r;
      cout << calc(r) - calc(l - 1) << " ";
    }
    lucas(1000, 100, 10007);
    cout << '\n';
  }
  lucas(1000, 100, 10007);
  return 0;
}