#include <bits/stdc++.h>
using namespace std;
const int N = 4e4 + 5;
int cnt[N];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int a, b, q;
    cin >> a >> b >> q;
    int x = a * b;
    for (int i = 1; i <= x; i++) cnt[i] = cnt[i - 1] + (i % a % b != i % b % a);
    while (q--) {
      long long l, r;
      cin >> l >> r;
      l--;
      cout << (r / x - l / x) * cnt[x] - cnt[l % x] + cnt[r % x] << ' ';
    }
    cout << '\n';
  }
}
