#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long lec(long long a, long long b) { return (a * b) / gcd(a, b); }
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long a, b, q;
    cin >> a >> b >> q;
    if (a < b) {
      swap(a, b);
    }
    long long lcm = lec(a, b);
    for (int i = 0; i < q; i++) {
      long long l, r;
      cin >> l >> r;
      if (a == b) {
        cout << "0 ";
        continue;
      }
      long long res = 0;
      long long l1 = (l / lcm);
      long long r1 = (r / lcm);
      if (r1 == l1) {
        if (l1 * lcm + a > l) {
          res += min(l1 * lcm + a - l, r - l + 1);
        }
      } else {
        res += (r1 - l1) * a;
        if (l > l1 * lcm) {
          res -= min(l - l1 * lcm, a);
        }
        if (r >= r1 * lcm) {
          res += min(a, r - r1 * lcm + 1);
        }
      }
      long long k = r - l + 1 - res;
      cout << k << " ";
    }
    cout << "\n";
  }
  return 0;
}
