#include <bits/stdc++.h>
using namespace std;
int a, b, q, c, d;
int gcd(int a, int b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a == b) return a;
  if (a > b) return gcd(a - b, b);
  return gcd(a, b - a);
}
long long cal(long long x) {
  long long tmp1 = x / d, tmp2 = x % d;
  long long ans = tmp1 * (d - c) + max(tmp2 - c + 1, 0ll);
  return ans;
}
int main() {
  int t;
  cin >> t;
  for (int f = 0; f < t; f++) {
    int q;
    cin >> a >> b;
    cin >> q;
    c = max(a, b);
    d = (a * b) / gcd(a, b);
    for (int i = 0; i < q; i++) {
      long long l, r;
      cin >> l >> r;
      cout << cal(r) - cal(l - 1) << endl;
    }
    cout << endl;
  }
  return 0;
}
