import java.io.*;
import java.util.*;

public class First {
    private static final Scanner sc = new Scanner(System.in);
    private static final PrintWriter pw = new PrintWriter(System.out);
    private static StringBuffer ans = new StringBuffer();

    public static void main(String[] args) throws Exception {
        int n = sc.nextInt(), k = sc.nextInt(), s = 0, t = 0;
        long max = 0L, c = 0L;
        long[] a = new long[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        for (int i = 0; i < n; i++) b[i] = sc.nextInt();
        long[] left = new long[n];
        long[] right = new long[n];
        if (b[0] == 1) left[0] = a[0];
        for (int i = 1; i < n; i++) {
            left[i] += left[i - 1];
            if (b[i] == 1) left[i] += a[i];
        }
        if (b[n - 1] == 1) right[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            right[i] += right[i + 1];
            if (b[i] == 1) right[i] += a[i];
        }
        for (t = 0; t < k; t++) c += a[t];
        t--;
        for (s = 0; s <= n - k; s++) {
            long e = c;
            if (s > 0) e += left[s - 1];
            if (t < n - 1) e += right[t + 1];
            max = Math.max(max, e);
            c -= a[s];
            if (t + 1 < n) c += a[++t];
        }
        ans.append(max);
        pw.print(ans);
        sc.close();
        pw.close();
    }
}
class Scanner {
    private final BufferedReader br;
    private StringTokenizer st;
    public Scanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(in));
        st = new StringTokenizer("");
    }
    public String next() throws IOException {
        if (!st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }
    public void close() throws IOException {
        br.close();
    }
}
class PrintWriter {
    private final BufferedOutputStream pw;
    public PrintWriter(OutputStream out) {
        pw = new BufferedOutputStream(out);
    }
    public void print(Object o) throws IOException {
        pw.write(o.toString().trim().getBytes());
        pw.flush();
    }
    public void close() throws IOException {
        pw.close();
    }
}


import java.io.*;
import java.util.*;

public class First {
    private static final Scanner sc = new Scanner(System.in);
    private static final PrintWriter pw = new PrintWriter(System.out);
    private static StringBuffer ans = new StringBuffer();

    public static void main(String[] args) throws Exception {
        int n = sc.nextInt(), k = sc.nextInt(), max = 0, s = 0, t = 0, c = 0;
        int[] a = new int[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        for (int i = 0; i < n; i++) b[i] = sc.nextInt();
        int[] left = new int[n];
        int[] right = new int[n];
        if (b[0] == 1) left[0] = a[0];
        for (int i = 1; i < n; i++) if (b[i] == 1) left[i] += left[i - 1] + a[i];
        if (b[n - 1] == 1) right[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; i--) if (b[i] == 1) right[i] += right[i + 1] + a[i];
        for (t = 0; t < k; t++) c += a[t];
        t--;
        for (s = 0; s <= n - k; s++) {
            int e = c;
            if (s > 0) e += left[s - 1];
            if (t < n - 1) e += right[t + 1];
            max = Math.max(max, e);
            c -= a[s];
            if (t + 1 < n) c += a[++t];
        }
        ans.append(max);
        pw.print(ans);
        sc.close();
        pw.close();
    }
}
class Scanner {
    private final BufferedReader br;
    private StringTokenizer st;
    public Scanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(in));
        st = new StringTokenizer("");
    }
    public String next() throws IOException {
        if (!st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public void close() throws IOException {
        br.close();
    }
}
class PrintWriter {
    private final BufferedOutputStream pw;
    public PrintWriter(OutputStream out) {
        pw = new BufferedOutputStream(out);
    }
    public void print(Object o) throws IOException {
        pw.write(o.toString().trim().getBytes());
        pw.flush();
    }
    public void close() throws IOException {
        pw.close();
    }
}


import java.io.*;
import java.util.*;

public class First {
    private static final Scanner sc = new Scanner(System.in);
    private static final PrintWriter pw = new PrintWriter(System.out);
    private static StringBuffer ans = new StringBuffer();

    public static void main(String[] args) throws Exception {
        int n = sc.nextInt(), k = sc.nextInt(), s = 0, t = 0;
        long max = 0L, c = 0L;
        long[] a = new long[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        for (int i = 0; i < n; i++) b[i] = sc.nextInt();
        long[] left = new long[n];
        long[] right = new long[n];
        if (b[0] == 1) left[0] = a[0];
        for (int i = 1; i < n; i++) if (b[i] == 1) left[i] += left[i - 1] + a[i];
        if (b[n - 1] == 1) right[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; i--) if (b[i] == 1) right[i] += right[i + 1] + a[i];
        for (t = 0; t < k; t++) c += a[t];
        t--;
        for (s = 0; s <= n - k; s++) {
            long e = c;
            if (s > 0) e += left[s - 1];
            if (t < n - 1) e += right[t + 1];
            max = Math.max(max, e);
            c -= a[s];
            if (t + 1 < n) c += a[++t];
        }
        ans.append(max);
        pw.print(ans);
        sc.close();
        pw.close();
    }

}

class Scanner {
    private final BufferedReader br;
    private StringTokenizer st;

    public Scanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(in));
        st = new StringTokenizer("");
    }
    public String next() throws IOException {
        if (!st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }
    public void close() throws IOException {
        br.close();
    }
}

class PrintWriter {
    private final BufferedOutputStream pw;
    public PrintWriter(OutputStream out) {
        pw = new BufferedOutputStream(out);
    }
    public void print(Object o) throws IOException {
        pw.write(o.toString().trim().getBytes());
        pw.flush();
    }
    public void close() throws IOException {
        pw.close();
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//  package lecture.sleep;

import java.util.Scanner;

/**
 *
 * @author Mostafa
 */
public class LectureSleep {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);
        short n = in.nextShort() ;
        short k = in.nextShort() , i ;
        short x[] = new short [n] ;
        byte y[] = new byte [n] ;
        int s = 0 ;
        for ( i = 0 ; i < n ; ++i )
        {
            x[i] = in.nextShort() ;
        }
        for ( i = 0 ; i < n ; ++i )
        {

            y[i] = in.nextByte() ;
            if ( y[i] == 1 )
                s +=x[i] ;
//            System.out.println(s);
        }
        int max = 0 ;
        int ne = 0 ;
        for ( i = 0 ; i < n - k + 1 ; ++i )
        {
            int d = 0 ;
            int f = 0 ;
            for ( short j = i ; j < k + i ; ++j )
            {
                if ( y[j] == 1 )
                    f += x[j] ;
                else
                    d += x[j] ;
            }
            if ( d + f > max + ne )
            {
                max = d ;
                ne = f ;
//                System.out.println(d + " " + f);
            }
        }
        System.out.println( s + max ) ;
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//  package lecture.sleep;

import java.util.Scanner;

/**
 *
 * @author Mostafa
 */
public class LectureSleep {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);
        int n = in.nextInt() ;
        int k = in.nextInt() , i ;
        int x[] = new int [n] ;
        int y[] = new int [n] , s = 0 ;
        for ( i = 0 ; i < n ; ++i )
        {
            x[i] = in.nextInt() ;
        }
        for ( i = 0 ; i < n ; ++i )
        {

            y[i] = in.nextInt() ;
            if ( y[i] == 1 )
                s +=x[i] ;
//            System.out.println(s);
        }
        int max = 0 ;
        int ne = 0 ;
        for ( i = 0 ; i < n - k + 1 ; ++i )
        {
            int d = 0 ;
            int f = 0 ;
            for ( int j = i ; j < k + i ; ++j )
            {
                if ( y[j] == 1 )
                    f += x[j] ;
                else
                    d += x[j] ;
            }
            if ( d + f > max + ne )
            {
                max = d ;
                ne = f ;

//                System.out.println(d + " " + f);
            }
        }
        System.out.println( s + max ) ;
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((llt)(l/pdt))*pdt;
            nm = ((llt)(r/pdt))*pdt;
            if(a==b) cout<<"0 ";
            else{
                // rest
                if(l%pdt!=0) rest = -dv[l%pdt-1];
                // last
                last = dv[r%pdt]-dv[0];
                // mid
                term = ((nm-num)/pdt);
                mid = dv[pdt-1]*term;
                cout<<mid+last-rest<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((int)(l/pdt))*pdt;
            nm = ((int)(r/pdt))*pdt;
            if(a==b) cout<<"0 ";
            else{
                // rest
                if(l%pdt!=0) rest = dv[l%pdt-1];
                // last
                last = dv[r%pdt]-dv[0];
                // mid
                term = ((nm-num)/pdt);
                if(term>0) mid = dv[pdt-1]*term;
                else mid = 0;
                cout<<mid+last-rest<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((llt)(l/pdt))*pdt;
            nm = ((llt)(r/pdt))*pdt;
            if(a==b) cout<<"0 ";
            else{
                // rest
                ans = 0;
                if(l%pdt!=0) rest = dv[l%pdt-1], ans = ans - rest;
                else rest = 0;
                // last
                last = dv[r%pdt]-dv[0], ans = ans + last;
                // mid
                term = ((nm-num)/pdt);
                if(term>0) mid = dv[pdt-1]*term, ans = ans + mid;
                else mid = 0;
                cout<<ans<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,mux,pdt,nm,rest,last,term,mid;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        mux = max(a,b);
        while(tr--){
            cin>>l>>r;
            if(a==b) cout<<"0 ";
            else{
                num = ((llt)(l/pdt))*pdt;
                nm = ((llt)(r/pdt))*pdt;
                if(num==nm){
                    if(num+mux-1>=l){
                        if(num+mux-1<r) cout<<(r-l+1)-(num+mux-l)<<" ";
                        else cout<<"0 ";
                    }
                    else cout<<(r-l+1);
                }
                else{
                    // rest
                    if(num+mux-1>=l) rest = num+mux-l;
                    else rest = 0;
                    // last
                    if(nm+mux-1<r)  last = mux;
                    else            last = r-nm+1;
                    // mid
                    term = ((nm-num)/pdt)-1;
                    if(term>0)          mid = (mux)*term;
                    else if(term==0)    mid = 0;
                    cout<<(r-l+1)-(rest+mid+last)<<" ";
                }
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((llt)(l/pdt))*pdt;
            nm = ((llt)(r/pdt))*pdt;
            if(a==b) cout<<"0 ";
            else{
                // rest
                if(l%pdt!=0) rest = dv[l%pdt-1];
                // last
                last = dv[r%pdt]-dv[0];
                // mid
                term = ((nm-num)/pdt);
                mid = dv[pdt-1]*term;
                cout<<mid+last-rest<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((llt)(l/pdt))*pdt;
            nm = ((llt)(r/pdt))*pdt;
            if(a==b) cout<<"0 ";
            else if(num==nm) cout<<dv[r%pdt]-dv[l%pdt-1]<<" ";
            else{
                // rest
                rest = dv[pdt-1]-dv[l%pdt-1];
                // last
                last = dv[r%pdt]-dv[0];
                // mid
                term = ((nm-num)/pdt)-1;
                mid = dv[pdt-1]*term;
                cout<<rest+mid+last<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((llt)(l/pdt))*pdt;
            nm = ((llt)(r/pdt))*pdt;
            if(num==nm) cout<<dv[r%pdt]-dv[--l%pdt]<<" ";
            else{
                // rest
                rest = dv[pdt-1]-dv[--l%pdt];
                // last
                last = dv[r%pdt]-dv[0];
                // mid
                term = ((nm-num)/pdt)-1;
                mid = dv[pdt-1]*term;
                cout<<rest+mid+last<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((llt)(l/pdt))*pdt;
            nm = ((llt)(r/pdt))*pdt;
            if(a==b) cout<<"0 ";
            else{
                // rest
                if(l%pdt!=0) rest = dv[l%pdt-1];
                // last
                last = dv[r%pdt]-dv[0];
                // mid
                term = ((r-l)/pdt);
                mid = dv[pdt-1]*term;
                cout<<mid+last-rest<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef     long long int       llt;
const llt md = 1000000007; // i.e. pow(10,9) + 7

llt min(llt a, llt b){
    if(a<b) return a;
    return b;
}

llt max(llt a, llt b){
    if(a<b) return b;
    return a;
}

int main() {
    llt n,k,x,y,tr,i,ch,ans,j,num,q,a,b,l,r,pdt,nm,rest,mid,term,last;
    cin>>q;
    while(q--){
        cin>>a>>b>>tr;
        pdt = a*b;
        llt dv[pdt];
        memset(dv,0,sizeof dv);
        for(i=1;i<pdt;i++){
            if(((i%a)%b)!=((i%b)%a)) dv[i] = dv[i-1]+1;
            else dv[i] = dv[i-1];
        }
        while(tr--){
            cin>>l>>r;
            num = ((int)(l/pdt))*pdt;
            nm = ((int)(r/pdt))*pdt;
            if(a==b) cout<<"0 ";
            else{
                // rest
                if(l%pdt!=0) rest = dv[l%pdt-1];
                else rest = 0;
                // last
                last = dv[r%pdt]-dv[0];
                // mid
                term = ((nm-num)/pdt);
                if(term>0) mid = dv[pdt-1]*term;
                else mid = 0;
                cout<<mid+last-rest<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		int y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			if(r<b)
				ans1.pb(0);
			else if(l<b&&r<lcm){
				ans1.pb(r-b+1);
			}
			else if(r<lcm){
				ans1.pb(r-l+1);
			}
			else{
				ll le = max(b,l);
				ll ans = r-le+1;
				ll val = 0;
				ll vale = r/lcm;
				ll valu = le/lcm;

				if(le%lcm==0){
					val = vale - valu + 1;
				}
				else{
					val = vale - valu;
				}
				if(val==0){
					ans1.pb(r-le+1);
					continue;
				}
				ans = ans - (val-1)*(b);
				if(lcm*val+b<=r){
					ans = ans - (b);
				}
				else{
					ans = ans - (r-lcm*val+1);
				}
				ans1.pb(ans);
			}
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			int val = l - l%lc;
			int ans = 0;
			while(val<=r){
				for(int j=val;j<val+b;j++){
					if(j>=l&&j<=r)
						ans++;
				}
				val += lcm;
			}
			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		int y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			if(r<b)
				ans1.pb(0);
			else if(l<b&&r<lcm){
				ans1.pb(r-b+1);
			}
			else if(r<lcm){
				ans1.pb(r-l+1);
			}
			else{
				ll le = max(b,l);
				ll ans = r-le+1;
				ll val = 0;
				ll vale = r/lcm;
				ll valu = le/lcm;

				if(le%lcm==0){
					val = vale - valu + 1;
				}
				else{
					val = vale - valu;
				}

				ans = ans - (val-1)*(b);
				if(lcm*val+b<=r){
					ans = ans - (b);
				}
				else{
					ans = ans - (r-lcm*val+1);
				}
				ans1.pb(ans);
			}
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			ll lc = a*b/gcd(a,b);
			ll cnt = r/lc - l/lc;
			ll val = r/lc*lc;
			ll ans = 0;
			if(cnt){
				ans += (cnt -1)*b + min(r - val+1, b);
			}

			ans += max(min((l/lc)*lc+b, r+1) - l, 0LL);

			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			if(r<b)
				ans1.pb(0);
			else{
				ll le = max(b,l);
				ll ans = r-le+1;
				if(r<lcm)
					ans1.pb(ans);
				else{
					ll val = 0;
					ll vale = r/lcm;
					ll valu = le/lcm;

					if(le%lcm==0){
						val = vale - valu + 1;
					}
					else{
						val = vale - valu;
					}

					ans = ans - (val-1)*(b);
					if(lcm*val+b<=r){
						ans = ans - (b);
					}
					else{
						ans = ans - (r-lcm*val+1);
					}
					ans1.pb(ans);
				}
			}
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			ll vall = l - l%lc;
			ll valr = r - r%lc;
			ll ans = 0;

			ans += max((ll)0,vall+b - l);
			if(vall!=valr){
				ans += min(b,r-valr+1);
			}
			ll lcl = vall/lc;
			ll lcr = valr/lc;
			lcl++;
			lcr--;

			if(lcr>=lcl){
				ans += (lcr-lcl+1)*b;
			}

			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			int vall = l - l%lc;
			int valr = r - r%lc;
			int ans = 0;
			for(int j=vall;j<vall+b;j++){
				if(j>=l&&j<=r)
					ans++;
			}
			if(vall!=valr){
				for(int j=valr;j<valr+b;j++){
					if(j>=l&&j<=r)
						ans++;
				}
			}
			int lcl = vall/lc;
			int lcr = valr/lc;
			lcl++;
			lcr--;

			if(lcr>=lcl){
				ans += (lcr-lcl+1)*b;
			}

			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			ll lc = a*b/gcd(a,b);
			ll lbd = l/lc;
			ll lbr = l%lc;
			ll rbd = r/lc;
			ll rbr = r%lc;

			ll val = 0;
			if(lbd==rbd){
				if(rbr<b){
					ans1.pb(0);
				}
				else{
					ans1.pb(rbr - max(lbr,b) + 1);
				}
			}else{
				ll ans = r - l + 1;
				ans = ans - max((ll)0, (b-lbr+1));
				ans = ans - max((ll)0, min(b,rbr+1));
				ans = ans - (rbd - lbd - 1)*b;
				ans1.pb(ans);
			}
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		int y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			if(r<b)
				ans1.pb(0);
			else if(l<b&&r<lcm){
				ans1.pb(r-b+1);
			}
			else{

					ll le = max(b,l);
					ll ans = r-le+1;
					ll val = 0;
					ll vale = r/lcm;
					ll valu = le/lcm;

					if(le%lcm==0){
						val = vale - valu + 1;
					}
					else{
						val = vale - valu;
					}

					ans = ans - (val-1)*(b);
					if(lcm*val+b<=r){
						ans = ans - (b);
					}
					else{
						ans = ans - (r-lcm*val+1);
					}
					ans1.pb(ans);
			}
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			ll vall = l - l%lc;
			ll valr = r - r%lc;
			ll ans = 0;

			ans += max(0,vall+b - l);
			if(vall!=valr){
				ans += min(b,r-valr+1);
			}
			ll lcl = vall/lc;
			ll lcr = valr/lc;
			lcl++;
			lcr--;

			if(lcr>=lcl){
				ans += (lcr-lcl+1)*b;
			}

			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			int vall = l - l%lc;
			int valr = r - r%lc;
			int ans = 0;
			for(int j=vall;j<vall+b;j++){
				if(j>=l&&j<=r)
					ans++;
			}
			if(vall!=valr){
				for(int j=valr;j<valr+b;j++){
					if(j>=l&&j<=r)
						ans++;
				}
			}
			int lcl = vall/lc;
			int lcr = valr/lc;
			lcl++;
			lcr--;

			if(lcr>=lcl){
				ans += (lcr-lcl)*b;
			}

			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define ll long long
#define pb push_back
int sum[1000000];

int gcd(int a,int b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<int> ans1;
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			if(r<b)
				ans1.pb(0);
			else{
				int le = max(b,l);
				int ans = r-le+1;
				if(r<lcm)
					ans1.pb(ans);
				else{
					int val = 0;
					int vale = r/lcm;
					int valu = le/lcm;

					if(le%lcm==0){
						val = vale - valu + 1;
					}
					else{
						val = vale - valu;
					}

					ans = ans - (val-1)*(b);
					if(lcm*val+b<=r){
						ans = ans - (b);
					}
					else{
						ans = ans - (r-lcm*val+1);
					}
					ans1.pb(ans);
				}
			}
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			ll vall = l - l%lc;
			ll valr = r - r%lc;
			ll ans = 0;
			for(int j=vall;j<vall+b;j++){
				if(j>=l&&j<=r)
					ans++;
			}
			if(vall!=valr){
				for(int j=valr;j<valr+b;j++){
					if(j>=l&&j<=r)
						ans++;
				}
			}
			ll lcl = vall/lc;
			ll lcr = valr/lc;
			lcl++;
			lcr--;

			if(lcr>=lcl){
				ans += (lcr-lcl+1)*b;
			}

			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		ll y1 = max(a,b);
		if(y1>b){
			a = b;
			b = y1;
		}
		ll lc = a*b/gcd(a,b);
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			int val = l/lc;
			int ans = 0;
			while(val<=r){
				for(int j=val;j<val+b;j++){
					if(j>=l&&j<=r)
						ans++;
				}
				val += lcm;
			}
			ans1.pb(r-l+1 - ans);
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define ll long long
#define pb push_back

ll gcd(ll a,ll b){
	if(b==0)
		return a;
	if(a<b){
		return gcd(b,a);
	}
	return gcd(b,a%b);
}
int main(){

	int t;
	cin>>t;
	while(t--){
		ll a,b,q;
		cin>>a>>b>>q;
		ll lcm = (a*b)/gcd(a,b);
		vector<ll> ans1;
		for(int i=0;i<q;i++){
			ll l,r;
			cin>>l>>r;
			if(r<b)
				ans1.pb(0);
			else if(l<b&&r<lcm){
				ans1.pb(r-b+1);
			}
			else{

					ll le = max(b,l);
					ll ans = r-le+1;
					ll val = 0;
					ll vale = r/lcm;
					ll valu = le/lcm;

					if(le%lcm==0){
						val = vale - valu + 1;
					}
					else{
						val = vale - valu;
					}

					ans = ans - (val-1)*(b);
					if(lcm*val+b<=r){
						ans = ans - (b);
					}
					else{
						ans = ans - (r-lcm*val+1);
					}
					ans1.pb(ans);
			}
		}
		for(int i=0;i<q;i++){
			cout<<ans1[i];
			if(i!=q-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
int func(ll lcm,ll a,ll b,ll n)
{

    ll x = n/lcm;
    ll y= x*lcm;
    ll tot= n;
   if(x!=0)
   {
    tot-=min(b, n-y+1);
    tot-= (x-1)*b;
    }

    tot-=min(n, (b-1));

    return tot;
}

int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int a,b,q;
     cin>>a>>b>>q;
     if(a>b)
     swap(a,b);

     int lcm= a*b;
     int gg=gcd(a,b);
     lcm/=gg;
     int l,r,ans;
     //ll anss[q];
     for(int i=0; i<q; i++)
     {
         cin>>l>>r;
         if((lcm==b )|| (r<b))
         {//anss[i]=0;
         cout<<0<<" ";
         continue;}

         ans= func(lcm,a,b,r);
         if(l>1)
         ans-= func(lcm,a,b,l-1);
         //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
         //anss[i]=ans;
         cout<<ans<<" ";
     }
    //  F(i,0,q)
    //  cout<<anss[i]<<" ";
     cout<<endl;
 }
}


#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mod (int) 998244353
#define MOD (int) 1e9+7
// Big two primes
#define X 1001100011100001111ll
#define all(a) a.begin(),a.end()
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define tc(t) int t; cin >> t; while (t--)
#define int long long
#define ll long long
#define pb emplace_back
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define in(x) scanf("%d", &x)
#define rr return 0
#define prec(n) fixed<<setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int> >
#define inf (int)(1e18)
#define ini(a, i) memset(a, i, sizeof(a))
#define vi vector<int>
#define fi first
#define se second
#define kitna se
#define endl "\n"
#define pii pair<int, int>
#define vii vector<pi>
const int MAXN = (int)((1e5) + 100);
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
int max(int a, int b) {if (a > b) return a; else return b;}
int min(int a, int b) {if (a < b) return a; else return b;}
void pr(int x) {cout << x;}
void prl(int x) {cout << x << endl;}
// typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> Set;
//bool isPrime(int N){ for(int i=2;i*i<=N;++i){if(N%i==0) return false;}return true;}
int cbrt(int x) { int lo = 1, hi = min(2000000ll, x); while (hi - lo > 1) {int mid = (lo + hi) / 2; if (mid * mid * mid < x) {lo = mid;} else hi = mid;} if (hi * hi * hi <= x) return hi; else return lo;}
const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
const int N =  (int)(2 * 1e5 + 10);

int findcount(int r, int lcm, int b) {
    int div = r / lcm;
    int prod = div * lcm;
    int tot = r;
    if (div != 0)
    {
        tot -= min(b, r - prod + 1);
        tot -= (div - 1) * b;
    }
    return tot - min(r, (b - 1));
}

signed main() {
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif

    fio;
//srand(time(NULL));
    int T = 1;
    cin >> T;

    while (T--) {
        int a, b, q;
        cin >> a >> b >> q;

        if (a > b) swap(a, b);
        int lc = a * b;
        int gg = gcd(a, b);
        lc /= gg;

        while (q--) {
            int l, r;
            cin >> l >> r;
            // int cnt = 0;
            // loop(i, l, r + 1) {
            //     if ( ((i % a) % b) != ((i % b) % a)) {
            //         //cout << i << endl;
            //         cnt++;
            //     }
            // }
            //  << cnt << " ";

            if ( lc == b || (r < b) ) {
                cout << 0 << " ";
                continue;
            }

            int cnt = findcount(r, lc, b);
            if (l > 1) cnt -= findcount(l - 1, lc, b);
            cout << cnt << " ";

        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
// int func(ll lcm,ll a,ll b,ll n)
// {

//     ll x = n/lcm;
//     ll y= x*lcm;
//     ll tot= n;
//    if(x!=0)
//    {
//     tot-=min(b, n-y+1);
//     tot-= (x-1)*b;
//     }

//     tot-=min(n, (b-1));

//     return tot;
// }
int findcount(int r, int lcm, int b) {
    int div = r / lcm;
    int prod = div * lcm;
    int tot = r;
    if (div != 0)
    {
        tot -= min(b, r - prod + 1);
        tot -= (div - 1) * b;
    }
    return tot - min(r, (b - 1));
}

int main()
{fast();
 int t;
 cin>>t;
 while(t--)
 {
     int a,b,q;
     cin>>a>>b>>q;
     if(a>b)
     swap(a,b);

     int lcm= a*b;
     int gg=gcd(a,b);
     lcm/=gg;
     int l,r,ans;
     //ll anss[q];
     for(int i=0; i<q; i++)
     {
         cin>>l>>r;
         if((lcm==b )|| (r<b))
         {//anss[i]=0;
         cout<<0<<" ";
         continue;}

         ans= findcount(r, lcm, b);
         if(l>1)
         ans-= findcount(l-1, lcm, b);
         //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
         //anss[i]=ans;
         cout<<ans<<" ";
     }
    //  F(i,0,q)
    //  cout<<anss[i]<<" ";
     cout<<endl;
 }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"

int func(ll a,ll b,ll n)
{
    ll lcm= a*b/__gcd(a,b);
    ll x = n/lcm;
    ll y= x*lcm;
    ll tot= n;
   if(x!=0)
   { tot-= (x-1)*b;
    tot-=min(b, n-y+1);}

    tot-=min(n, b-1);

    return tot;
}

int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int a,b,q;
     cin>>a>>b;
     if(a>b)
     swap(a,b);
     cin>>q;
     ll lcm= a*b/__gcd(a,b);
     ll l,r,ans;
     ll anss[q];
     for(int i=0; i<q; i++)
     {
         cin>>l>>r;
         if(lcm==b ||
          r<b)
         {anss[i]=0;
         continue;}

         ans= func(a,b,r);
         if(l>1)
         ans-= func(a,b,l-1);
         //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
         anss[i]=ans;
     }
     F(i,0,q)
     cout<<anss[i]<<" ";
     cout<<endl;
 }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
// // int func(ll lcm,ll a,ll b,ll n)
// // {

// //     ll x = n/lcm;
// //     ll y= x*lcm;
// //     ll tot= n;
// //    if(x!=0)
// //    {
// //     tot-=min(b, n-y+1);
// //     tot-= (x-1)*b;
// //     }

// //     tot-=min(n, (b-1));

// //     return tot;
// // }
// int findcount(int r, int lcm, int b) {
//     int div = r / lcm;
//     int prod = div * lcm;
//     int tot = r;
//     if (div != 0)
//     {
//         tot -= min(b, r - prod + 1);
//         tot -= (div - 1) * b;
//     }
//     return tot - min(r, (b - 1));
// }

// int main()
// {fast();
//  int t;
//  cin>>t;
//  while(t--)
//  {
//     //  int a,b,q;
//     //  cin>>a>>b>>q;
//     //  if(a>b)
//     //  swap(a,b);

//     //  int lcm= a*b;
//     //  int gg=gcd(a,b);
//     //  lcm/=gg;
//     //  int l,r,ans;
//     //  //ll anss[q];
//     //  for(int i=0; i<q; i++)
//     //  {
//     //      cin>>l>>r;
//     //      if((lcm==b )|| (r<b))
//     //      {//anss[i]=0;
//     //      cout<<0<<" ";
//     //      continue;}

//     //      ans= findcount(r, lcm, b);
//     //      if(l>1)
//     //      ans-= findcount(l-1, lcm, b);
//     //      //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
//     //      //anss[i]=ans;
//     //      cout<<ans<<" ";
//     //  }
//     // //  F(i,0,q)
//     // //  cout<<anss[i]<<" ";
//     //  cout<<endl;
//     int a, b, q;
//         cin >> a >> b >> q;

//         if (a > b) swap(a, b);
//         int lc = a * b;
//         int gg = gcd(a, b);
//         lc /= gg;

//         while (q--) {
//             int l, r;
//             cin >> l >> r;

//             if ( lc == b || (r < b) ) {
//                 cout << 0 << " ";
//                 continue;
//             }

//             int cnt = findcount(r, lc, b);
//             if (l > 1) cnt -= findcount(l - 1, lc, b);
//             cout << cnt << " ";

//         }
//         cout << endl;
//  }
// }
int findcount(int r, int lcm, int b) {
    int div = r / lcm;
    int prod = div * lcm;
    int tot = r;
    if (div != 0)
    {
        tot -= min(b, r - prod + 1);
        tot -= (div - 1) * b;
    }
    return tot - min(r, (b - 1));
}

signed main() {
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif

    fast();
//srand(time(NULL));
    int T = 1;
    cin >> T;

    while (T--) {
        int a, b, q;
        cin >> a >> b >> q;

        if (a > b) swap(a, b);
        int lc = a * b;
        int gg = gcd(a, b);
        lc /= gg;

        while (q--) {
            int l, r;
            cin >> l >> r;
            // int cnt = 0;
            // loop(i, l, r + 1) {
            //     if ( ((i % a) % b) != ((i % b) % a)) {
            //         //cout << i << endl;
            //         cnt++;
            //     }
            // }
            //  << cnt << " ";

            if ( lc == b || (r < b) ) {
                cout << 0 << " ";
                continue;
            }

            int cnt = findcount(r, lc, b);
            if (l > 1) cnt -= findcount(l - 1, lc, b);
            cout << cnt << " ";

        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
int func(ll lcm,ll a,ll b,ll n)
{

    ll x = n/lcm;
    ll y= x*lcm;
    ll tot= n;
   if(x!=0)
   {
    tot-=min(b, n-y+1);
    tot-= (x-1)*b;
    }

    tot-=min(n, (b-1));

    return tot;
}

int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int a,b,q;
     cin>>a>>b>>q;
     if(a>b)
     swap(a,b);

     ll lcm= a*b;
     lcm/=gcd(a,b);
     ll l,r,ans;
     //ll anss[q];
     for(int i=0; i<q; i++)
     {
         cin>>l>>r;
         if((lcm==b )|| (r<b))
         {//anss[i]=0;
         cout<<0<<" ";
         continue;}

         ans= func(lcm,a,b,r);
         if(l>1)
         ans-= func(lcm,a,b,l-1);
         //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
         //anss[i]=ans;
         cout<<ans<<" ";
     }
    //  F(i,0,q)
    //  cout<<anss[i]<<" ";
     cout<<endl;
 }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
int func(ll lcm,ll a,ll b,ll n)
{

    ll x = n/lcm;
    ll y= x*lcm;
    ll tot= n;
   if(x!=0)
   {
    tot-=min(b, n-y+1);
    tot-= (x-1)*b;
    }

    tot-=min(n, (b-1));

    return tot;
}

int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int a,b,q;
     cin>>a>>b>>q;
     if(a>b)
     swap(a,b);

     int lcm= a*b;
     int gg=gcd(a,b);
     lcm/=gg;
     int l,r,ans;
     //ll anss[q];
     for(int i=0; i<q; i++)
     {
         cin>>l>>r;
         if((lcm==b )|| (r<b))
         {//anss[i]=0;
         cout<<0<<" ";
         continue;}

         ans= func(lcm,a,b,r);
         if(l>1)
         ans-= func(lcm,a,b,l-1);
         //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
         //anss[i]=ans;
         cout<<ans<<" ";
     }
    //  F(i,0,q)
    //  cout<<anss[i]<<" ";
     cout<<endl;
 }
}


// Striver_79
// Youtube: takeUforward
// Practice is the only key to improve

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mod (int) 998244353
#define MOD (int) 1e9+7
// Big two primes
#define X 1001100011100001111ll
#define all(a) a.begin(),a.end()
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define tc(t) int t; cin >> t; while (t--)
#define int long long
#define ll long long
#define pb emplace_back
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define in(x) scanf("%d", &x)
#define rr return 0
#define prec(n) fixed<<setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int> >
#define inf (int)(1e18)
#define ini(a, i) memset(a, i, sizeof(a))
#define vi vector<int>
#define fi first
#define se second
#define kitna se
#define endl "\n"
#define pii pair<int, int>
#define vii vector<pi>
const int MAXN = (int)((1e5) + 100);
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
int max(int a, int b) {if (a > b) return a; else return b;}
int min(int a, int b) {if (a < b) return a; else return b;}
void pr(int x) {cout << x;}
void prl(int x) {cout << x << endl;}
// typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> Set;
//bool isPrime(int N){ for(int i=2;i*i<=N;++i){if(N%i==0) return false;}return true;}
int cbrt(int x) { int lo = 1, hi = min(2000000ll, x); while (hi - lo > 1) {int mid = (lo + hi) / 2; if (mid * mid * mid < x) {lo = mid;} else hi = mid;} if (hi * hi * hi <= x) return hi; else return lo;}
const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
const int N =  (int)(2 * 1e5 + 10);

int findcount(int r, int lcm, int b) {
    int div = r / lcm;
    int prod = div * lcm;
    int tot = r;
    if (div != 0)
    {
        tot -= min(b, r - prod + 1);
        tot -= (div - 1) * b;
    }
    return tot - min(r, (b - 1));
}

signed main() {
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif

    fio;
//srand(time(NULL));
    int T = 1;
    cin >> T;

    while (T--) {
        int a, b, q;
        cin >> a >> b >> q;

        if (a > b) swap(a, b);
        int lc = a * b;
        int gg = gcd(a, b);
        lc /= gg;

        while (q--) {
            int l, r;
            cin >> l >> r;
            // int cnt = 0;
            // loop(i, l, r + 1) {
            //     if ( ((i % a) % b) != ((i % b) % a)) {
            //         //cout << i << endl;
            //         cnt++;
            //     }
            // }
            //  << cnt << " ";

            if ( lc == b || (r < b) ) {
                cout << 0 << " ";
                continue;
            }

            int cnt = findcount(r, lc, b);
            if (l > 1) cnt -= findcount(l - 1, lc, b);
            cout << cnt << " ";

        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
// int func(ll lcm,ll a,ll b,ll n)
// {

//     ll x = n/lcm;
//     ll y= x*lcm;
//     ll tot= n;
//    if(x!=0)
//    {
//     tot-=min(b, n-y+1);
//     tot-= (x-1)*b;
//     }

//     tot-=min(n, (b-1));

//     return tot;
// }
int findcount(int r, int lcm, int b) {
    int div = r / lcm;
    int prod = div * lcm;
    int tot = r;
    if (div != 0)
    {
        tot -= min(b, r - prod + 1);
        tot -= (div - 1) * b;
    }
    return tot - min(r, (b - 1));
}

int main()
{fast();
 int t;
 cin>>t;
 while(t--)
 {
    //  int a,b,q;
    //  cin>>a>>b>>q;
    //  if(a>b)
    //  swap(a,b);

    //  int lcm= a*b;
    //  int gg=gcd(a,b);
    //  lcm/=gg;
    //  int l,r,ans;
    //  //ll anss[q];
    //  for(int i=0; i<q; i++)
    //  {
    //      cin>>l>>r;
    //      if((lcm==b )|| (r<b))
    //      {//anss[i]=0;
    //      cout<<0<<" ";
    //      continue;}

    //      ans= findcount(r, lcm, b);
    //      if(l>1)
    //      ans-= findcount(l-1, lcm, b);
    //      //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
    //      //anss[i]=ans;
    //      cout<<ans<<" ";
    //  }
    // //  F(i,0,q)
    // //  cout<<anss[i]<<" ";
    //  cout<<endl;
    int a, b, q;
        cin >> a >> b >> q;

        if (a > b) swap(a, b);
        int lc = a * b;
        int gg = gcd(a, b);
        lc /= gg;

        while (q--) {
            int l, r;
            cin >> l >> r;

            if ( lc == b || (r < b) ) {
                cout << 0 << " ";
                continue;
            }

            int cnt = findcount(r, lc, b);
            if (l > 1) cnt -= findcount(l - 1, lc, b);
            cout << cnt << " ";

        }
        cout << endl;
 }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
int func(ll lcm,ll a,ll b,ll n)
{

    ll x = n/lcm;
    ll y= x*lcm;
    ll tot= n;
   if(x!=0)
   {
    tot-=min(b, n-y+1);
    tot-= (x-1)*b;
    }

    tot-=min(n, (b-1));

    return tot;
}

int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int a,b,q;
     cin>>a>>b;
     if(a>b)
     swap(a,b);
     cin>>q;
     ll lcm= a*b;
     lcm/=gcd(a,b);
     ll l,r,ans;
     //ll anss[q];
     for(int i=0; i<q; i++)
     {
         cin>>l>>r;
         if((lcm==b )|| (r<b))
         {//anss[i]=0;
         cout<<0<<" ";
         continue;}

         ans= func(lcm,a,b,r);
         if(l>1)
         ans-= func(lcm,a,b,l-1);
         //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
         //anss[i]=ans;
         cout<<ans<<" ";
     }
    //  F(i,0,q)
    //  cout<<anss[i]<<" ";
     cout<<endl;
 }
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define fast()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(i,x,A) for(ll i=x;i<A;i++)
#define FF(i,x,A) for(ll i=x;i<=A;i++)
#define MAX INT_MAX
#define MOD 1000000007
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define ex "\n"

int func(ll lcm,ll a,ll b,ll n)
{

    ll x = n/lcm;
    ll y= x*lcm;
    ll tot= n;
   if(x!=0)
   {
    tot-=min(b, n-y+1);
    tot-= (x-1)*b;
    }

    tot-=min(n, b-1);

    return tot;
}

int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int a,b,q;
     cin>>a>>b;
     if(a>b)
     swap(a,b);
     cin>>q;
     ll lcm= a*b/__gcd(a,b);
     ll l,r,ans;
     //ll anss[q];
     for(int i=0; i<q; i++)
     {
         cin>>l>>r;
         if((lcm==b )|| (r<b))
         {//anss[i]=0;
         cout<<0<<" ";
         continue;}

         ans= func(lcm,a,b,r);
         if(l>1)
         ans-= func(lcm,a,b,l-1);
         //cout<<".."<<func(a,b,r)<<"   "<<func(a,b,l-1)<<endl;
         //anss[i]=ans;
         cout<<ans<<" ";
     }
    //  F(i,0,q)
    //  cout<<anss[i]<<" ";
     cout<<endl;
 }
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  ll multiples,m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	ll fm=x/m;
//  	cout<<"-------------"<<endl;

  	ll lm=y/m;
  	ll cnt=0;
  	if(fm!=0&&fm!=lm){
  		for(i=m*fm;i<(m*fm)+max(a,b);i++){
  			if(i>=x&&i<=y)cnt++;
		  }
	  }
  	fm++;
//  	ll cnt=0;
  		for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<(y-x+1)-cnt<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  unsigned ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  unsigned ll m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	if(a==b||a==1||b==1){
  		cout<<0<<" ";
  		continue;
	  }
  	unsigned ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	unsigned ll lm=y/m;
  	unsigned ll cnt=0;
  	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<max((unsigned ll)0,(y-x+1)-cnt)<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  ll m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	ll lm=y/m;
  	ll cnt=0;
  	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<(y-x+1)-cnt<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  ll m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	ll lm=y/m;
  	ll cnt=0;
  	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<max((ll)0,(y-x+1)-cnt)<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  ll multiples,m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	ll lm=y/m;
  	ll cnt=0;
//  	cout<<fm<<" "<<lm<<"fm lm";
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);
//	for(i=1;i<max(a,b);i++){
//		if(i>=x&&i<=y)cnt++;
//	}
	cout<<(y-x+1)-cnt<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y;
  cin>>a>>b>>q;
  ll multiples,m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	if(y<max(a,b)){
	  cout<<0<<" ";
	  continue;
	}
  	if(x%m==0)
  	multiples=y/m-x/m+1;
  	else multiples=y/m-x/m;
//  	cout<<multiples<<": ";

  	if(multiples==0){
//  		if(y>)
	  cout<<y-max(max(a,b),x)+1<<" ";
	  continue;
	}
  	ll ans=multiples*max(a,b);
  	ll last=y/m;
  	ll diff=-1;
  	if(multiples!=0)
	diff= (last*m+max(a,b))-y;
  	if(diff>0)ans-=diff;
  	cout<<(y-x+1)-ans<<" ";
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  ll m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	ll lm=y/m;
  	ll cnt=0;
  	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<=(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<(y-x+1)-cnt<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  ll m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	ll lm=y/m;
  	ll cnt=0;
  	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<max(0,(y-x+1)-cnt)<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  unsigned ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  unsigned ll m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	unsigned ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	unsigned ll lm=y/m;
  	unsigned ll cnt=0;
  	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<max((unsigned ll)0,(y-x+1)-cnt)<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  unsigned ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  unsigned ll m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	if(x==y||x==1||y==1){
  		cout<<0<<" ";
  		continue;
	  }
  	unsigned ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	unsigned ll lm=y/m;
  	unsigned ll cnt=0;
  	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);

	cout<<max((unsigned ll)0,(y-x+1)-cnt)<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


//--------------------------------------------------------------------------------
// Code By:
//					######                  #               ######
//					##   #                  #               #
//					#####      ##       #####      ##       #   ###
//					##   #              #   #               #   # #
//					##    #	            #####               ##### #
//---------------------------------------------------------------------------------

#include<bits/stdc++.h>
using namespace std;

                             /* ----------------------------- START of Template ---------------*/
#define ll long long int
#define FOR(x,to) for(x=0;x<(to);x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<long long int> >
#define vll vector<long long int>
#define MINUS(a) memset(a,0xff,sizeof(a))
#define SZ(X) ((int)(X).size())
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
vector<ll>isprime;
void seive(ll size){
	isprime.resize(size+1,1);
	ll i,j;
	isprime[0]=0;
	isprime[1]=0;
	for(i=2;i*i<=size;i++){
		if(isprime[i]){
			for(j=2;j*i<=size;j++)isprime[i*j]=0;
		}
	}
}
ll power(ll x,unsigned ll y, ll m){
	if(y==0)return 1;
	ll p=power(x,y/2,m);
	p=(p*p)%m;
	if(y%2!=0)p=(p*x)%m;
	return p;
}
ll modinverse(ll a,ll m){
	return power(a,m-2,m);
}
                       /* ----------------------          End Of Template --------------------------------*/

ll findans(){
  ll a,b,q,x,y,i;
  cin>>a>>b>>q;
  ll multiples,m=(a*b)/__gcd(a,b);
  while(q--){
  	cin>>x>>y;
  	ll fm=x/m;
//  	cout<<"-------------"<<endl;
  	if(x%m!=0)fm++;
  	ll lm=y/m;
  	ll cnt=0;
//  	cout<<fm<<" "<<lm<<"fm lm";
if(lm!=0)
  	for(i=m*lm;i<(m*lm)+max(a,b);i++){
  		if(i>=x&&i<=y)cnt++;
	  }
//	  cout<<cnt;
	  if(lm>fm)
	  cnt+=(lm-fm)*max(a,b);
	for(i=1;i<max(a,b);i++){
		if(i>=x&&i<=y)cnt++;
	}
	cout<<(y-x+1)-cnt<<" ";
//	cout<<"\n-----------"<<endl;
  }
  cout<<endl;
  return 0;

}

int main(){
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll t,i;
	cin>>t;
	for(i=0;i<t;i++){
		findans();
	}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    ll dp[size];
    for(ll i=0;i<max(a,b);i++)
    dp[i]=0;
    for(ll i=max(a,b);i<size;i++)
    dp[i]=1;
    for(ll i=max(a,b)+1;i<size;i++)
    {
      dp[i]+=dp[i-1];
    }
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      l--;
      ll ans=0;
      ll cl=(l)/size;
      ll cr=r/size;
      ans+=(cr-cl)*(size-max(a,b));
      // if(l%size==0)
      // {
      //   ans+=dp[temp2%size];
      // }
      // else
      // {
        if(r%size>=max(a,b))
        {
          ans+=r%size-max(a,b)+1;
        }
        if(l%size>=max(a,b))
        {
          ans-=(l%size-max(a,b)+1);
        }
      // }
      // if(l%size==0)
      // ans+=dp[l%size];
      // else
      // ans+=dp[l%size]-dp[l%size-1];
      // while((r-l)%size!=0)
      // {
      //   if(r%size>=max(a,b))
      //   ans++;
      //   r--;
      // }
      // if(r%size>=max(a,b))
      // ans++;
      // ans+=temp*(size-max(a,b));
      if(ans<=0)
      cout<<"0 ";
      else
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    ll dp[size];
    for(ll i=0;i<max(a,b);i++)
    dp[i]=0;
    for(ll i=max(a,b);i<size;i++)
    dp[i]=1;
    for(ll i=max(a,b)+1;i<size;i++)
    {
      dp[i]+=dp[i-1];
    }
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      l--;
      ll ans=0;
      ll cl=(l-1)/size;
      ll cr=r/size;
      ans+=(cr-cl)*(size-max(a,b));
      // if(l%size==0)
      // {
      //   ans+=dp[temp2%size];
      // }
      // else
      // {
        if(r%size>=max(a,b))
        {
          ans+=r%size-max(a,b)+1;
        }
        if(l%size>=max(a,b))
        {
          ans-=(l%size-max(a,b)+1);
        }
      // }
      // if(l%size==0)
      // ans+=dp[l%size];
      // else
      // ans+=dp[l%size]-dp[l%size-1];
      // while((r-l)%size!=0)
      // {
      //   if(r%size>=max(a,b))
      //   ans++;
      //   r--;
      // }
      // if(r%size>=max(a,b))
      // ans++;
      // ans+=temp*(size-max(a,b));
      if(ans<=0)
      cout<<"0 ";
      else
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/(__gcd(a,b));
    //cout<<size<<"\n";
    ll dp[size+1];
    ll tot=0;
    for(ll i=1;i<=size;i++)
    {
      if((i%a)%b!=(i%b)%a)
      {
        dp[i]=1;
        tot++;
      }
      else
      dp[i]=0;
    }
    // for(ll i=1;i<=size;i++)
    // {
    //   cout<<dp[i]<<" ";
    // }
    // cout<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      ll pnt=l/size;
      pnt=size*(pnt+1);
      ll ans=0;
      ll te=size;
      ll flag=0;
      ll temp1=pnt;
      while(pnt>=l)
      {
        if(pnt<=r)
        {
          ans+=dp[te];
        }
        else
        flag=1;
        pnt--;
        te--;
      }
      if(flag==1)
      {
        cout<<ans<<" ";
      }
      else
      {
        pnt=r/size;
        pnt=pnt*size;
        te=1;
        ll temp2=pnt;
        pnt++;
        while(pnt<=r)
        {

            ans+=dp[te];
          pnt++;
          te++;
        }
        ans+=((temp2-temp1)/size)*tot;
        cout<<ans<<" ";
      }

    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/(__gcd(a,b));
    //cout<<size<<"\n";
    ll dp[size+1];
    ll tot=0;
    for(ll i=1;i<=size;i++)
    {
      if((i%a)%b!=(i%b)%a)
      {
        dp[i]=1;
        tot++;
      }
      else
      dp[i]=0;
    }
    for(ll i=2;i<=size;i++)
    {
      dp[i]+=dp[i-1];
    }
    // for(ll i=1;i<=size;i++)
    // {
    //   cout<<dp[i]<<" ";
    // }
    // cout<<"\n";
    dp[0]=0;
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      ll pnt=l/size;
      // if(l%size==0)
      // {
      //   pnt=size*(pnt);
      // }
      // else
      pnt=size*(pnt+1);
      ll ans=0;
      ll te=size;
      ll flag=0;
      ll temp1=pnt;

        if(pnt<=r)
        {

        }
        else
        flag=1;
        pnt=min(pnt,r);
        ll yuhi=pnt-l;
        if(flag==0)
        ans+=dp[size]-dp[size-yuhi];
        if(flag==1)
        {
          ll temp3,temp4;
          if(r%size==0)
          temp3=size;
          else
          temp3=(r%size);
          if(l%size==0)
          temp4=size;
          else
          temp4=(l%size);
          ans+=dp[temp3]-dp[temp4-1];
          cout<<ans<<" ";
        }
        else
        {
          pnt=r/size;
          pnt=pnt*size;
          ll temp2=pnt;
          yuhi=r-pnt;
          //yuhi--;
          if(yuhi>=0)
          ans+=dp[1+yuhi]-dp[1];
          // while(pnt<=r)
          // {
          //
          //     ans+=dp[te];
          //   pnt++;
          //   te++;
          // }
          ans+=((temp2-temp1)/size)*tot;
          cout<<ans<<" ";
        }

    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    ll dp[size];
    for(ll i=0;i<max(a,b);i++)
    dp[i]=0;
    for(ll i=max(a,b);i<size;i++)
    dp[i]=1;
    for(ll i=max(a,b)+1;i<size;i++)
    {
      dp[i]+=dp[i-1];
    }
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      l--;
      ll ans=0;
      ll cl=(l-1)/size;
      ll cr=r/size;
      ans+=(cr-cl)*(size-max(a,b));
      // if(l%size==0)
      // {
      //   ans+=dp[temp2%size];
      // }
      // else
      // {
        if(r%size>=b)
        {
          ans+=r%size-b+1;
        }
        if(l%size>=b)
        {
          ans-=(l%size-b+1);
        }
      // }
      // if(l%size==0)
      // ans+=dp[l%size];
      // else
      // ans+=dp[l%size]-dp[l%size-1];
      // while((r-l)%size!=0)
      // {
      //   if(r%size>=max(a,b))
      //   ans++;
      //   r--;
      // }
      // if(r%size>=max(a,b))
      // ans++;
      // ans+=temp*(size-max(a,b));
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    ll dp[size];
    for(ll i=0;i<max(a,b);i++)
    dp[i]=0;
    for(ll i=max(a,b);i<size;i++)
    dp[i]=1;
    for(ll i=max(a,b)+1;i<size;i++)
    {
      dp[i]+=dp[i-1];
    }
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      l--;
      ll ans=0;
      ll temp=(r-l)/size;
      ll temp2=r-temp*size;
      // if(l%size==0)
      // {
      //   ans+=dp[temp2%size];
      // }
      // else
      // {
        ans+=abs(dp[temp2%size]-dp[l%size]);
      // }
      // if(l%size==0)
      // ans+=dp[l%size];
      // else
      // ans+=dp[l%size]-dp[l%size-1];
      // while((r-l)%size!=0)
      // {
      //   if(r%size>=max(a,b))
      //   ans++;
      //   r--;
      // }
      // if(r%size>=max(a,b))
      // ans++;
      ans+=temp*(size-max(a,b));
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      ll ans=0;
      while((r-l)%size!=0)
      {
        if(r%size>=max(a,b))
        ans++;
        r--;
      }
      if(r%size>=max(a,b))
      ans++;
      ans+=(r-l)/size*(size-max(a,b));
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    ll dp[size];
    for(ll i=0;i<max(a,b);i++)
    dp[i]=0;
    for(ll i=max(a,b);i<size;i++)
    dp[i]=1;
    for(ll i=max(a,b)+1;i<size;i++)
    {
      dp[i]+=dp[i-1];
    }
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      l--;
      ll ans=0;
      ll cl=(l-1)/size;
      ll cr=r/size;
      ans+=(cr-cl)*(size-max(a,b));
      // if(l%size==0)
      // {
      //   ans+=dp[temp2%size];
      // }
      // else
      // {
        if(r%size>=b)
        {
          ans+=r%size-max(a,b)+1;
        }
        if(l%size>=b)
        {
          ans-=(l%size-max(a,b)+1);
        }
      // }
      // if(l%size==0)
      // ans+=dp[l%size];
      // else
      // ans+=dp[l%size]-dp[l%size-1];
      // while((r-l)%size!=0)
      // {
      //   if(r%size>=max(a,b))
      //   ans++;
      //   r--;
      // }
      // if(r%size>=max(a,b))
      // ans++;
      // ans+=temp*(size-max(a,b));
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/(__gcd(a,b));
    //cout<<size<<"\n";
    ll dp[size+1];
    ll tot=0;
    for(ll i=1;i<=size;i++)
    {
      if((i%a)%b!=(i%b)%a)
      {
        dp[i]=1;
        tot++;
      }
      else
      dp[i]=0;
    }
    // for(ll i=1;i<=size;i++)
    // {
    //   cout<<dp[i]<<" ";
    // }
    // cout<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      ll pnt=l/size;
      if(l%size==0)
      {
        pnt=size*(pnt);
      }
      else
      pnt=size*(pnt+1);
      ll ans=0;
      ll te=size;
      ll flag=0;
      ll temp1=pnt;
      while(pnt>=l)
      {
        if(pnt<=r)
        {
          ans+=dp[te];
        }
        else
        flag=1;
        pnt--;
        te--;
      }
      if(flag==1)
      {
        cout<<ans<<" ";
      }
      else
      {
        pnt=r/size;
        pnt=pnt*size;
        te=1;
        ll temp2=pnt;
        pnt++;
        while(pnt<=r)
        {

            ans+=dp[te];
          pnt++;
          te++;
        }
        ans+=((temp2-temp1)/size)*tot;
        cout<<ans<<" ";
      }

    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    ll dp[size];
    for(ll i=0;i<max(a,b);i++)
    dp[i]=0;
    for(ll i=max(a,b);i<size;i++)
    dp[i]=1;
    for(ll i=max(a,b)+1;i<size;i++)
    {
      dp[i]+=dp[i-1];
    }
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      ll ans=0;
      ll temp=(r-l+1)/size;
      ll temp2=r-temp*size;
      if(l%size==0)
      {
        ans+=dp[temp2%size];
      }
      else
      {
        ans+=dp[temp2%size]-dp[l%size-1];
      }
      // while((r-l)%size!=0)
      // {
      //   if(r%size>=max(a,b))
      //   ans++;
      //   r--;
      // }
      // if(r%size>=max(a,b))
      // ans++;
      ans+=temp*(size-max(a,b));
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
void adde(  vector<ll> adj[],ll u,ll v)
{
  adj[u].pb(v);
  adj[v].pb(u);
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {
    ll a,b,q;
    cin>>a>>b>>q;
    ll size=a*b/__gcd(a,b);
    ll dp[size];
    for(ll i=0;i<max(a,b);i++)
    dp[i]=0;
    for(ll i=max(a,b);i<size;i++)
    dp[i]=1;
    for(ll i=max(a,b)+1;i<size;i++)
    {
      dp[i]+=dp[i-1];
    }
    //cout<<size<<"\n";
    while(q--)
    {
      ll l,r;
      cin>>l>>r;
      l--;
      ll ans=0;
      ll cl=(l-1)/size;
      ll cr=r/size;
      ans+=(cr-cl)*(size-max(a,b));
      // if(l%size==0)
      // {
      //   ans+=dp[temp2%size];
      // }
      // else
      // {
        if(r%size>=b)
        {
          ans+=r%size-max(a,b)+1;
        }
        if(l%size>=b)
        {
          ans-=(l%size-max(a,b)+1);
        }
      // }
      // if(l%size==0)
      // ans+=dp[l%size];
      // else
      // ans+=dp[l%size]-dp[l%size-1];
      // while((r-l)%size!=0)
      // {
      //   if(r%size>=max(a,b))
      //   ans++;
      //   r--;
      // }
      // if(r%size>=max(a,b))
      // ans++;
      // ans+=temp*(size-max(a,b));
      if(ans<=0)
      cout<<"0 ";
      else
      cout<<ans<<" ";
    }
    cout<<"\n";
  }
  return 0;
}