#include <bits/stdc++.h>
#include <numeric>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vp vector<pll>
#define ub upper_bound
#define lb lower_bound
#define mapp map<ll,ll>
#define mem(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(),v.end()
#define bs binary_search
#define qll queue<ll>
#define sll set<ll>

int main()
{
    fastio;
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b,q,i;
        cin>>a>>b>>q;
        ll l ,r;
       ll val=max(a,b);
        ll LCM = std::lcm(a,b) ;
        for(i=0;i<q;i++)
        {ll ans=0,ans1=0,sum=0,sum1=0,pra=0;
        cin>>l>>r;
            if(r<val)
            cout<<0<<" ";
             else if(val==LCM)
            cout<<0<<" ";
            else
            {

                if(l>val)
                {
                    ans = l/LCM;
                    ans1 = l%LCM;
                    if(ans1>=val)
                     ans1=ans1-val;
                     else
                     ans1=-1;

                     sum1 = ans*LCM - ans*val + ans1 + 1;
                     ans=0;ans1=0;
                }
                ans=r/LCM;
                ans1=r%LCM;
                if(ans1>=val)
                ans1=ans1-val;
                else
                ans1=-1;

                sum=ans*LCM - ans*val + ans1 + 1;

                if(sum1>0&&sum1<=sum)
                cout<<sum-sum1 + 1<<" ";
                else
                cout<<sum<<" ";
            }
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#include <boost/math/common_factor.hpp>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vp vector<pll>
#define ub upper_bound
#define lb lower_bound
#define mapp map<ll,ll>
#define mem(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(),v.end()
#define bs binary_search
#define qll queue<ll>
#define sll set<ll>

int main()
{
    fastio;
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b,q,i;
        cin>>a>>b>>q;
        ll l ,r;
       ll val=max(a,b);
        for(i=0;i<q;i++)
        {ll ans=0,ans1=0,sum=0,sum1=0,pra=0;
        cin>>l>>r;
            if(r<val)
            cout<<0<<" ";
            else
            {
                ll LCM = boost::math::lcm(a,b) ;
                if(l>val)
                {
                    ans = l/LCM;
                    ans1 = l%LCM;
                    if(ans1>val)
                     ans1=ans1-val;

                     sum1 = ans*LCM - ans*val + ans1 + 1;
                }
                ans=r/LCM;
                ans1=r%LCM;
                if(ans1>val)
                ans1=ans1-val;

                sum=ans*LCM - ans*val + ans1 + 1;

                if(sum1!=0)
                cout<<sum-sum1 + 1<<" ";
                else
                cout<<sum<<" ";
            }
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#include <numeric>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vp vector<pll>
#define ub upper_bound
#define lb lower_bound
#define mapp map<ll,ll>
#define mem(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(),v.end()
#define bs binary_search
#define qll queue<ll>
#define sll set<ll>

int main()
{
    fastio;
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b,q,i;
        cin>>a>>b>>q;
        ll l ,r;
       ll val=max(a,b);
        for(i=0;i<q;i++)
        {ll ans=0,ans1=0,sum=0,sum1=0,pra=0;
        cin>>l>>r;
            if(r<val)
            cout<<0<<" ";
            else
            {
                ll LCM = std::lcm(a,b) ;
                if(l>val)
                {
                    ans = l/LCM;
                    ans1 = l%LCM;
                    if(ans1>=val)
                     ans1=ans1-val;
                     else
                     ans1=-1;

                     sum1 = ans*LCM - ans*val + ans1 + 1;
                     ans=0;ans1=0;
                }
                ans=r/LCM;
                ans1=r%LCM;
                if(ans1>=val)
                ans1=ans1-val;
                else
                ans=-1;

                sum=ans*LCM - ans*val + ans1 + 1;

                if(sum1>0)
                cout<<sum-sum1 + 1<<" ";
                else
                cout<<sum<<" ";
            }
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#include <numeric>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vp vector<pll>
#define ub upper_bound
#define lb lower_bound
#define mapp map<ll,ll>
#define mem(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(),v.end()
#define bs binary_search
#define qll queue<ll>
#define sll set<ll>

int main()
{
    fastio;
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b,q,i;
        cin>>a>>b>>q;
        ll l ,r;
       ll val=max(a,b);
        for(i=0;i<q;i++)
        {ll ans=0,ans1=0,sum=0,sum1=0,pra=0;
        cin>>l>>r;
            if(r<val)
            cout<<0<<" ";
            else
            {
                ll LCM = std::lcm(a,b) ;
                if(l>val)
                {
                    ans = l/LCM;
                    ans1 = l%LCM;
                    if(ans1>=val)
                     ans1=ans1-val;
                     else
                     ans1=-1;

                     sum1 = ans*LCM - ans*val + ans1 + 1;
                     ans=0;ans1=0;
                }
                ans=r/LCM;
                ans1=r%LCM;
                if(ans1>=val)
                ans1=ans1-val;
                else
                ans1=-1;

                sum=ans*LCM - ans*val + ans1 + 1;

                if(sum1>0&&sum1<=sum)
                cout<<sum-sum1 + 1<<" ";
                else
                cout<<sum<<" ";
            }
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
//#include <boost/math/common_factor.hpp>
#include <numeric>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vp vector<pll>
#define ub upper_bound
#define lb lower_bound
#define mapp map<ll,ll>
#define mem(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(),v.end()
#define bs binary_search
#define qll queue<ll>
#define sll set<ll>

int main()
{
    fastio;
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b,q,i;
        cin>>a>>b>>q;
        ll l ,r;
       ll val=max(a,b);
        ll LCM = std::lcm(a,b) ;
        for(i=0;i<q;i++)
        {ll ans=0,ans1=0,sum=0,sum1=0,pra=0;
        cin>>l>>r;
            if(r<val)
            cout<<0<<" ";
             else if(val==LCM)
            cout<<0<<" ";
            else
            {

                if(l>val)
                {
                    ans = l/LCM;
                    ans1 = l%LCM;
                    if(ans1>=val)
                     ans1=ans1-val;
                     else
                     ans1=-1;

                     sum1 = ans*LCM - ans*val + ans1 + 1;
                     ans=0;ans1=0;
                }
                ans=r/LCM;
                ans1=r%LCM;
                if(ans1>=val)
                ans1=ans1-val;
                else
                ans1=-1;

                sum=ans*LCM - ans*val + ans1 + 1;

                if(sum1>0)
                {
                    if(((l%a)%b)!=((l%b)%a))
                     cout<<sum-sum1 +1<<endl;
                     else
                     cout<<sum-sum1<<endl;
                }
                else
                cout<<sum<<" ";
            }
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#include <numeric>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vp vector<pll>
#define ub upper_bound
#define lb lower_bound
#define mapp map<ll,ll>
#define mem(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(),v.end()
#define bs binary_search
#define qll queue<ll>
#define sll set<ll>

int main()
{
    fastio;
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b,q,i;
        cin>>a>>b>>q;
        ll l ,r;
       ll val=max(a,b);
        for(i=0;i<q;i++)
        {ll ans=0,ans1=0,sum=0,sum1=0,pra=0;
        cin>>l>>r;
            if(r<val)
            cout<<0<<" ";
            else
            {
                ll LCM = std::lcm(a,b) ;
                if(l>val)
                {
                    ans = l/LCM;
                    ans1 = l%LCM;
                    if(ans1>=val)
                     ans1=ans1-val;
                     else
                     ans1=-1;

                     sum1 = ans*LCM - ans*val + ans1 + 1;
                     ans=0;ans1=0;
                }
                ans=r/LCM;
                ans1=r%LCM;
                if(ans1>=val)
                ans1=ans1-val;
                else
                ans=-1;

                sum=ans*LCM - ans*val + ans1 + 1;

                if(sum1>0&&sum1<=sum)
                cout<<sum-sum1 + 1<<" ";
                else
                cout<<sum<<" ";
            }
        }
        cout<<endl;
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[leng - 1];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
            if (sub_ans == 35892149736605866){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;

            }
        }
        if (!gay) for (auto &x: ans) cout << x << ' ';
        if (!gay) cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    int c = 0;
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[(beg * leng - 1) % leng];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double dl;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll lcme = lcm(a, b);

            vector<ll> pref(lcme, 0);
            ll summe = 0;
            for (int i = 0; i < lcme; i++){
                if (i % a != i % b % a) summe++;
                pref[i] = summe;
            }
        for (int z = 0; z < q; z++){
            ll l, r; cin >> l >> r;
            ll lo = l / lcme * lcme;
            if (lo < l) lo += lcme;
            ll hi = r / lcme * lcme;

            ll ans = 0;
            if (lo >= l && lo <= r && hi >= l && hi <= r){

                ll full_cycle = (hi - lo) / lcme;
                ans += full_cycle * pref.back();
                ans += pref.back();
                ll new_lo = l / lcme * lcme;
                ans -= pref[l - 1 - new_lo];
                ans += pref[r - hi];
            }
            else{
                ll new_lo = l / lcme * lcme;
                ans += pref[r - new_lo];
                ans -= pref[l - 1 - new_lo];
            }
            cout << ans << ' ' ;
        }
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[(beg * leng - 1) % leng];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            int k = 10;
            ans.push_back(sub_ans);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){abort();}
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double dl;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll lcme = lcm(a, b);
        for (int z = 0; z < q; z++){
            ll l, r; cin >> l >> r;
            ll lo = l / lcme * lcme;
            if (lo < l) lo += lcme;
            ll hi = r / lcme * lcme;

            ll ans = 0;
            vector<ll> pref(lcme, 0);
            ll summe = 0;
            for (int i = 0; i < lcme; i++){
                if (i % a != i % b % a) summe++;
                pref[i] = summe;
            }
            if (lo >= l && lo <= r && hi >= l && hi <= r){

                ll full_cycle = (hi - lo) / lcme;
                ans += full_cycle * pref.back();
                ans += pref.back();
                ll new_lo = l / lcme * lcme;
                ans -= pref[l - 1 - new_lo];
                ans += pref[r - hi];
            }
            else{
                ll new_lo = l / lcme * lcme;
                ans += pref[r - new_lo];
                ans -= pref[l - 1 - new_lo];
            }
            cout << ans << ' ' ;
        }
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[leng - 1];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){cout << "???"}
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;
            }
            ans.push_back(sub_ans_true);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;
            }
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;
            }
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){}
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){cout << "???";}
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    ll counte = 1;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[leng - 1];
            if ((beg * leng - 1) % leng != leng - 1){
                cout << a << ' ' << b << ' ' << q << ' ' << l << ' ' << r << '\n';
                return 0;
            }
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
        }
        if (!gay) for (auto &x: ans) cout << x << ' ';
        if (!gay) cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            if (sub_ans_false != sub_ans_true){abort();}
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            int k = 5;
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double dl;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll lcme = lcm(a, b);
        for (int z = 0; z < q; z++){
            ll l, r; cin >> l >> r;
            ll lo = l / lcme * lcme;
            if (lo < l) lo += lcme;
            ll hi = r / lcme * lcme;

            ll ans = 0;
            if (lo >= l && lo <= r && hi >= l && hi <= r){

                ll full_cycle = (hi - lo) / lcme;
                ans += full_cycle * (lcme - b);
                for (ll x = l; x < lo; x++){
                    if ((x % a) % b != (x % b) % a) ans++;
                }
                for (ll x = hi + 1; x <= r; x++){
                    if ((x % a) % b != (x % b) % a) ans++;
                }
            }
            else{
                for (ll x = l; x <= r; x++){
                    if ((x % a) % b != (x % b) % a) ans++;
                }
            }
            cout << ans << ' ' ;
        }
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            sub_ans_true += pref[r % leng];
            if (sub_ans_false != sub_ans_true){int k = 10;}
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[leng - 1];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
            if (sub_ans == 35892149736605866){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;
                return 0;
            }
        }
        if (!gay) for (auto &x: ans) cout << x << ' ';
        if (!gay) cout << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double dl;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll lcme = lcm(a, b);

            vector<ll> pref(lcme, 0);
            ll summe = 0;
            for (int i = 0; i < lcme; i++){
                if (i % a != i % b % a) summe++;
                pref[i] = summe;
            }
        for (int z = 0; z < q; z++){
            ll l, r; cin >> l >> r;
            ll lo = l / lcme * lcme;
            if (lo < l) lo += lcme;
            ll hi = r / lcme * lcme;

            ll ans = 0;
            if (lo >= l && lo <= r && hi >= l && hi <= r){

                ll full_cycle = (hi - lo) / lcme;
                ans += full_cycle * pref.back();
                ans += pref.back();
                ll new_lo = l / lcme * lcme;
                if (l - 1 - new_lo >= 0) ans -= pref[l - 1 - new_lo];
                ans += pref[r - hi];
            }
            else{
                ll new_lo = l / lcme * lcme;
                ans += pref[r - new_lo];
                if (l - 1 - new_lo >= 0) ans -= pref[l - 1 - new_lo];
            }
            cout << ans << ' ' ;
        }
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[(beg * leng - 1) % leng];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
            if (sub_ans == 35892149736605866){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;

            }
        }
        if (!gay) for (auto &x: ans) cout << x << ' ';
        if (!gay) cout << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double dl;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll lcme = lcm(a, b);

            vector<ll> pref(lcme, 0);
            ll summe = 0;
            for (int i = 0; i < lcme; i++){
                if (i % a != i % b % a) summe++;
                pref[i] = summe;
            }
        for (int z = 0; z < q; z++){
            ll l, r; cin >> l >> r;
            ll lo = l / lcme * lcme;
            if (lo < l) lo += lcme;
            ll hi = r / lcme * lcme;

            ll ans = 0;
            if (lo >= l && lo <= r && hi >= l && hi <= r && lo <= hi){

                ll full_cycle = (hi - lo) / lcme;
                ans += full_cycle * pref.back();
                ans += pref.back();
                ll new_lo = (l - 1) / lcme * lcme;
                ans -= pref[l - 1 - new_lo];
                ans += pref[r - hi];
            }
            else{
                ll new_lo = (l - 1) / lcme * lcme;
                ans += pref[r - new_lo];
                if (l - 1 - new_lo >= 0) ans -= pref[l - 1 - new_lo];
            }
            cout << ans << ' ' ;
        }
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[(beg * leng - 1) % leng];
            if ((beg * leng - 1) % leng != leng - 1){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;

            }
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
        }
        if (!gay) for (auto &x: ans) cout << x << ' ';
        if (!gay) cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans_false = 0, sub_ans_true = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans_false += (ende - beg) * pref[leng - 1];
            sub_ans_false += pref[(beg * leng - 1) % leng];
            sub_ans_false -= pref[(l - 1) % leng];
            sub_ans_false += pref[r % leng];

            sub_ans_true += (ende - beg) * pref[leng - 1];
            sub_ans_true += pref[leng - 1];
            sub_ans_true -= pref[(l - 1) % leng];
            if (sub_ans_false != sub_ans_true){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;
            }
            ans.push_back(sub_ans_false);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[(beg * leng - 1) % leng];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    ll counte = 1;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (gay && counte == 23503){
                cout << a << ' ' << b << ' ' << q << ' ' << l << ' ' << r << '\n';
            }
            counte++;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[leng - 1];
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
        }
        if (!gay) for (auto &x: ans) cout << x << ' ';
        if (!gay) cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            ll sub_ans = 0;
            for (int x = l; x <= r; x++){
                if (x % a == 0 && (x % b) % a == 0){
                    ll ende = floor((double)r / leng);
                    sub_ans += (ende - x / leng) * leng;
                    for (int y = ende * leng; y <= r; y++){
                         if (y % a != (y % b) % a) sub_ans++;
                    }
                    break;
                }
                if (x % a != (x % b) % a) sub_ans++;
            }
            ans.push_back(sub_ans);
        }
        for (auto &x: ans) cout << x << ' ';
        cout << '\n';
    }
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
#include <math.h>
#include <random>
#include <limits>
#include <bitset>
#include <iomanip>
#include <set>
#include <utility>
#include <climits>
#include <chrono>
#include <unordered_set>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    bool gay = false;
    while(t--){
        ll a, b, q; cin >> a >> b >> q;
        if (a > b) swap(a, b);
        ll leng = lcm(a, b);
        vector<ll> pref(leng);
        ll summe = 0;
        for (int i = 0; i < leng; i++){
            if (i % a != (i % b) % a) summe++;
            pref[i] = summe;
        }
        vector<ll> ans;
        for (int i = 0; i < q; i++){
            ll l, r; cin >> l >> r;
            if (l == 15237906562091245 && r == 211841187424780942){
                gay = true;
            }
            ll sub_ans = 0;
            ll beg = ceil((double)l / leng);
            ll ende = floor((double)r / leng);
            sub_ans += (ende - beg) * pref[leng - 1];
            sub_ans += pref[(beg * leng - 1) % leng];
            if (sub_ans + pref[(beg * leng - 1) % leng] != sub_ans + pref[leng - 1]){
                cout << a << ' ' << b << ' ' << 1 << ' ' << l << ' ' << r << endl;

            }
            sub_ans -= pref[(l - 1) % leng];
            sub_ans += pref[r % leng];
            ans.push_back(sub_ans);
        }
        if (!gay) for (auto &x: ans) cout << x << ' ';
        if (!gay) cout << '\n';
    }
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		ll t1 = (l%(a*b)==0) ? l / (a*b) : l / (a * b) + 1;
		ll t2 = r / (a*b);
		ll cnt = 0;
		if (t1 * (a * b) > r) cnt = 0;
		else {
			cnt = (t2 - t1) * b;
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else cnt += b;
		}
		if (b % a == 0 || b>r) ans.push_back(0);
		else {
			ll temp = max(l, b);
			if (temp > r) ans.push_back(0);
			else ans.push_back(r - temp + 1 - cnt);
		}
	}
	for (int i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b > r) {
			ans.push_back(0);
			continue;
		}

		l = max(l, b);
		ll t1 = l / (a * b);
		ll t2 = r / (a*b);
		ll cnt = t2>t1+1? (t2 - t1 - 1) * b : 0;

		if (t1 * a * b + b > l) cnt += t1 * a * b + b - l + 1;

		if (t2 * a * b >= l) {
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else  cnt += b;
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b==a || b > r) {
			ans.push_back(0);
			continue;
		}

		l = max(l, b);
		ll t1 = l / (a * b);
		ll t2 = r / (a*b);
		ll cnt = t2>t1+1? (t2 - t1 - 1) * b : 0;

		if (t1 * a * b + b > l) cnt += t1 * a * b + b - l + 1;

		if (t2 * a * b >= l) {
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else  cnt += b;
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<int> ans;
	while (q--) {
		cin >> l >> r;
		ll t1 = (l%(a*b)==0) ? l / (a*b) : l / (a * b) + 1;
		ll t2 = r / (a*b);
		ll cnt = 0;
		if (t1 * (a * b) > r) cnt = 0;
		else {
			cnt = (t2 - t1) * b;
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else cnt += b;
		}
		if (b % a == 0) ans.push_back(0);
		else {
			ll temp = max(l, b);
			if (temp > r) ans.push_back(0);
			else ans.push_back(r - temp + 1 - cnt);
		}
	}
	for (int i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		ll t1 = (l%(a*b)==0) ? l / (a*b) : l / (a * b) + 1;
		ll t2 = r / (a*b);
		ll cnt = 0;
		if (t1 * (a * b) > r) cnt = 0;
		else {
			cnt = (t2 - t1) * b;
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else cnt += b;
		}
		if ( b>r) ans.push_back(0);
		else {
			ll temp = max(l, b);
			if (temp > r) ans.push_back(0);
			else ans.push_back(r - temp + 1 - cnt);
		}
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		ll t1 = (l%(a*b)==0) ? l / (a*b) : l / (a * b) + 1;
		ll t2 = r / (a*b);
		ll cnt = 0;
		if (t1 * (a * b) > r) cnt = 0;
		else {
			cnt = (t2 - t1) * b;
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else cnt += b;
		}
		if ( b>r) ans.push_back(0);
		else {
			ll temp = max(l, b);
			if (temp > r) ans.push_back(0);
			else ans.push_back(r - temp + 1 - cnt);
		}
	}
	for (int i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b==a || b > r) {
			ans.push_back(0);
			continue;
		}

		ll t1 = l / (a * b) + (l%(a*b) ==0 ? -1 : 0);
		ll t2 = r / (a*b);
		ll cnt =0;

		if(t1==t2){
			if (t1 * a * b + b >= l) cnt += min(t1 * a * b + b-1,r) - l + 1;
		}
		else {
			if (t1 * a * b + b >= l) cnt += min(t1 * a * b + b - 1, r) - l + 1;
			if (t2 * a * b >= l) {
				if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
				else  cnt += b;
			}
			cnt += (t2 - t1 - 1) * b;
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<int> ans;
	while (q--) {
		cin >> l >> r;
		ll t1 = l / (a * b) + 1;
		ll t2 = r / (a*b);
		ll cnt = 0;
		if (t1 * (a * b) > r) cnt = 0;
		else {
			cnt = (t2 - t1) * b;
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else cnt += b;
		}
		if (b % a == 0) ans.push_back(0);
		else {
			ll temp = max(l, b);
			if (temp > r) ans.push_back(0);
			else ans.push_back(r - temp + 1 - cnt);
		}
	}
	for (int i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);
	ll ab = a * b;

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b % a == 0 || b > r) {
			ans.push_back(0);
			continue;
		}

		ll t1 = (l - 1) / (a * b);
		ll t2 = r / (a * b);
		ll cnt = 0;

		if (t1 == t2) {
			if (t1 * a * b + b-1 >= l) cnt += min(t1 * a * b + b - 1, r) - l + 1;
		}
		else {
			if (t1 * a * b + b-1 >= l) cnt += min(t1 * a * b + b - 1, r) - l + 1;
			if (t2 * a * b >= l) {
				if (t2 * a * b + b-1 > r) cnt += r - t2 * a * b + 1;
				else  cnt += b;
			}
			cnt += (t2 - t1 - 1) * b;
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b==a || b > r) {
			ans.push_back(0);
			continue;
		}

		ll t1 = l / (a * b);
		ll t2 = r / (a*b);
		ll cnt = t2>t1+1? (t2 - t1 - 1) * b : 0;

		if(t1==t2){
			if (t1 * a * b + b >= l) cnt += min(t1 * a * b + b-1,r) - l + 1;
		}
		else {
			if (t2 * a * b >= l) {
				if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
				else  cnt += b;
			}
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

ll a, b, l, r, q, p[MAX], ab;

ll f(ll x) {
	ll cnt = x / ab;
	ll rem = x % ab;
	return cnt * p[ab] + p[rem];
}

void solve() {
	cin >> a >> b >> q;
	ab = a * b;
	for (int i = 1; i <= ab; i++) {
		if ((i % a) % b != (i % b) % a) p[i] = 1;
		else p[i] = 0;
	}
	for (int i = 1; i <= ab; i++) p[i] += p[i - 1];

	while (q--) {
		cin >> l >> r;
		cout << f(r) - f(l - 1) << '\n';
	}
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b%a==0 || b > r) {
			ans.push_back(0);
			continue;
		}

		l = max(l, b);
		ll t1 = l / (a * b) + (l%(a*b) ==0 ? -1 : 0);
		ll t2 = r / (a*b);
		ll cnt =0;

		if(t1==t2){
			if (t1 * a * b + b >= l) cnt += min(t1 * a * b + b-1,r) - l + 1;
		}
		else {
			if (t1 * a * b + b >= l) cnt += min(t1 * a * b + b - 1, r) - l + 1;
			if (t2 * a * b >= l) {
				if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
				else  cnt += b;
			}
			cnt += (t2 - t1 - 1) * b;
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b%a==0 || b > r) {
			ans.push_back(0);
			continue;
		}

		l = max(l, b);
		ll t1 = l / (a * b);
		ll t2 = r / (a*b);
		ll cnt = t2>t1+1? (t2 - t1 - 1) * b : 0;

		if (t1 * a * b + b > l) cnt += t1 * a * b + b - l + 1;

		if (t2 * a * b >= l) {
			if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
			else  cnt += b;
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <tuple>
#include <functional>
#include <cmath>
#define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2e5 + 4;

void solve() {
	ll a, b, q, l, r; cin >> a >> b >> q;
	if (b < a) swap(a, b);

	vector<ll> ans;
	while (q--) {
		cin >> l >> r;
		if (b%a==0 || b > r) {
			ans.push_back(0);
			continue;
		}

		ll t1 = l / (a * b) + (l%(a*b) ==0 ? -1 : 0);
		ll t2 = r / (a*b);
		ll cnt =0;

		if(t1==t2){
			if (t1 * a * b + b >= l) cnt += min(t1 * a * b + b-1,r) - l + 1;
		}
		else {
			if (t1 * a * b + b >= l) cnt += min(t1 * a * b + b - 1, r) - l + 1;
			if (t2 * a * b >= l) {
				if (t2 * a * b + b > r) cnt += r - t2 * a * b + 1;
				else  cnt += b;
			}
			cnt += (t2 - t1 - 1) * b;
		}

		ans.push_back(r - l + 1 - cnt);
	}
	for (ll i : ans) cout << i << " ";
	cout << '\n';
}

int main() {
	FAST;
	int tt; cin >> tt;
	while (tt--) solve();
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) >= a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            int len = (r - l + 1);
            l %= a * b;
            r %= a * b;
            if (l + len <= a * b) {
                if (l + len != 0) now_ans += pref[l + len - 1];
                if (l != 0) now_ans -= pref[l - 1];
            }
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) >= a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            int len = (r - l + 1);
            l %= a * b;
            r %= a * b;
            if (l + len <= a * b) {
                if (l + len != 0) now_ans += pref[l + len];
                if (l != 0) now_ans -= pref[l - 1];
            } else {
                if (l != 0) now_ans += pref[a * b] - pref[l - 1];
                //else now_ans += pref[a * b] - pref[a * b - 1];
                if (r != 0) now_ans += pref[r];
            }
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l) / (a * b) * pref[a * b];
            l %= a * b;
            now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b)] - pref[(l - 1) % (a * b)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b + 1)] - pref[(l - 1) % (a * b + 1)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb((i % a) % b != (i % b) % a);
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        int new_r = r - (r % (a * b));
        int new_l = l + (a * b - (l % (a * b)));
        now_ans += (new_r - new_l) / (a * b) * pref[a * b];
        l %= a * b;
        now_ans += pref[a * b] - pref[l - 1];
        r %= a * b;
        now_ans += pref[r];
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans(a * b);
    for (int i = 1; i <= a * b; i++) {
        ans[i - 1] = (((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        while (l <= r && l % (a * b) != 0) {
            int now = l % (a * b);
            now_ans += pref[now];
            if (now != 0) {
                now_ans -= pref[now - 1];
            }
            l++;
        }
        if (l + a * b <= r) {
            while (l + a * b <= r) {
                now_ans += pref[a * b];
                l += a * b;
            }
            l++;
        }
        while (l <= r) {
            int now = l % (a * b);
            now_ans += pref[now];
            if (now != 0) {
                now_ans -= pref[now - 1];
            }
            l++;
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans(a * b);
    for (int i = 1; i <= a * b; i++) {
        ans[i - 1] = (((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        while (l <= r && l % (a * b) != 0) {
            int now = l % (a * b);
            now_ans += pref[now];
            if (now != 0) {
                now_ans -= pref[now - 1];
            }
            l++;
        }
        while (l + a * b < r) {
            now_ans += pref[a * b];
            l += a * b;
        }
        while (l <= r) {
            int now = l % (a * b);
            now_ans += pref[now];
            if (now != 0) {
                now_ans -= pref[now - 1];
            }
            l++;
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    //for (auto now : pref) {
    //    cout << now << ' ';
    //}
    //cout << endl;
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b + 1)] - pref[(l - 1) % (a * b + 1)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    //for (auto now : pref) {
    //    cout << now << ' ';
    //}
    //cout << endl;
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b + 1)] - pref[(l - 1) % (a * b + 1)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b)] - pref[(l - 1) % (a * b)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}