Educational Codeforces Round 86 (Rated for Div. 2) C—Yet Another Counting Problem
//序列具有周期性
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=5e4+10;
ll sum[N];
ll a,b,q;
ll calc(ll x)
{
return x/(a*b)*sum[a*b]+1ll*sum[x%(a*b)];
}
void solve()
{
memset(sum,0,sizeof sum);
cin>>a>>b>>q;
for(int i=1;i<=a*b;i++)
sum[i]+=sum[i-1]+(i%a%b!=i%b%a);
while(q--)
{
ll l,r;
cin>>l>>r;
cout<<calc(r)-calc(l-1)<<" ";
}
cout<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}