You are given two integers $a$ and $b$, and $q$ queries. The $i$-th query consists of two numbers $l_i$ and $r_i$, and the answer to it is the number of integers $x$ such that $l_i \le x \le r_i$, and $((x \bmod a) \bmod b) \ne ((x \bmod b) \bmod a)$. Calculate the answer for each query.

Recall that $y \bmod z$ is the remainder of the division of $y$ by $z$. For example, $5 \bmod 3 = 2$, $7 \bmod 8 = 7$, $9 \bmod 4 = 1$, $9 \bmod 9 = 0$.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. Then the test cases follow.

The first line of each test case contains three integers $a$, $b$ and $q$ ($1 \le a, b \le 200$; $1 \le q \le 500$).

Then $q$ lines follow, each containing two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^{18}$) for the corresponding query.


-----Output-----

For each test case, print $q$ integers — the answers to the queries of this test case in the order they appear.


-----Example-----
Input
2
4 6 5
1 1
1 3
1 5
1 7
1 9
7 10 2
7 8
100 200

Output
0 0 0 2 4 
0 91

Difficulty: MEDIUM_HARD
Tags: Number theory

Mathematics

Starter code:

Solutions:
from math import gcd, ceil
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	m = a * b // gcd(a, b)
	n = max(a, b)
	for _ in range(q):
		(l, r) = map(int, input().split())
		print(r - l - ((r // m - l // m + 1) * n - min(n, l % m) - max(0, n - r % m - 1)) + 1)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	d = max(a, b)
	lcm = a * b // gcd(a, b)
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		tmp = max(min(d - 1, r) - l + 1, 0)
		tmp += (r // lcm - l // lcm) * d
		if l // lcm != 0 and l // lcm * lcm + d - 1 >= l:
			tmp += l // lcm * lcm + d - 1 - l + 1
		if r // lcm != 0 and r // lcm * lcm + d - 1 >= r:
			tmp -= r // lcm * lcm + d - 1 - r
		ans.append(r - l + 1 - tmp)
	print(*ans)


def cnt(x, p, ps):
	return ps[x % p] + x // p * ps[p - 1]
for t in range(int(input().strip())):
	(a, b, q) = tuple(map(int, input().strip().split()))
	p = a * b
	ps = [0]
	for i in range(1, p):
		vab = i % a % b
		vba = i % b % a
		ps.append(ps[i - 1])
		if vba != vab:
			ps[i] += 1
	res = []
	for i in range(q):
		(l, r) = tuple(map(int, input().strip().split()))
		res.append(str(cnt(r, p, ps) - cnt(l - 1, p, ps)))
	print(' '.join(res))


def f(a, b, r):
	d = GCD(a, b)
	m = a * b // d
	p = r % m
	if d == min(a, b):
		return 0
	if p > max(a, b) - 1:
		p -= max(a, b) - 1
	else:
		p = 0
	return r // m * (m - max(a, b)) + p

def GCD(a, b):
	while a != 0 and b != 0:
		if a < b:
			b %= a
		else:
			a %= b
	return a + b
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	for j in range(q):
		(l, r) = map(int, input().split())
		if l == 1:
			print(f(a, b, r), end=' ')
		else:
			print(f(a, b, r) - f(a, b, l - 1), end=' ')
	print()


from sys import stdin, stdout
from math import gcd
t = int(stdin.readline().strip())

def f(a, b):
	v = []
	for i in range(0, 100000):
		if i % a % b != i % b % a:
			v.append(i)
	print(v)
	return v
for caso in range(t):
	(a, b, q) = map(int, stdin.readline().strip().split())
	arr = [0 for i in range(max(a, b) + 1)]
	v = []
	g = gcd(a, b)
	if g != a and g != b:
		lim = max(a, b)
		for i in range(lim, 10 ** 6):
			if i % a % b != i % b % a:
				v.append(i)
			else:
				break
	else:
		for i in range(q):
			(l, r) = map(int, stdin.readline().strip().split())
		ans = [0 for i in range(q)]
		print(*ans)
		continue
	ans = []
	x = v[-1] + 1
	for i in range(q):
		(l, r) = map(int, stdin.readline().strip().split())
		l = max(l, v[0])
		y = -((l - 1) // x) + r // x
		res = y * v[0]
		z = r // x * x + v[0] - 1
		if r // x * x >= l and r // x * x <= r:
			res -= max(0, z - r)
		u = (l - 1) // x * x
		u1 = u + v[0] - 1
		if u1 >= l and u1 <= r:
			res += u1 - l + 1
		if u <= r and u1 >= r:
			res = 10 ** 20
		ans.append(max(r - l + 1 - res, 0))
	print(*ans)


input = __import__('sys').stdin.buffer.readline
t = int(input())
for tc in range(t):
	(a, b, q) = map(int, input().split())
	c = [1]
	for x in range(1, a * b):
		c.append(c[-1])
		if x % a % b == x % b % a:
			c[-1] += 1

	def pre(x):
		full = x // (a * b)
		right = x % (a * b)
		return full * c[a * b - 1] + c[right]
	output = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		sol = pre(r) - pre(l - 1)
		output.append(str(r - l + 1 - sol))
	print(' '.join(output))


import math
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	check = [0] * lcm
	rui = [0] * (lcm + 1)
	aa = 0
	for i in range(lcm):
		if i % a % b != i % b % a:
			check[i] = 1
			aa += 1
			rui[i + 1] = aa
	onel = rui[lcm]
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split())
		ll = l % lcm
		rr = r % lcm
		loops = (r - l) // lcm
		if rr < ll:
			ans.append(loops * onel + (rui[lcm] - rui[ll]) + rui[rr + 1])
		elif rr > ll:
			ans.append(loops * onel + (rui[rr + 1] - rui[ll]))
		else:
			ans.append(loops * onel + check[rr])
	print(*ans)


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	vals = [0]
	curr = 0
	for i in range(1, a * b + 1):
		if i % a % b != i % b % a:
			curr += 1
		vals.append(curr)
	for i in range(q):
		(l, r) = map(int, input().split())
		f = 0
		s = 0
		f += curr * ((l - 1) // (a * b))
		l -= 1
		l %= a * b
		f += vals[l]
		s += curr * (r // (a * b))
		r %= a * b
		s += vals[r]
		print(s - f, end=' ')
	print('')


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def print_list_int(l):
	print(''.join(map(str, map(int, l))))

def compute_lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
t = inp()
for test in range(t):
	(a, b, q) = inlt()
	(c, d) = (min(a, b), max(a, b))
	(a, b) = (c, d)
	lcm = compute_lcm(a, b)
	out = []
	for i in range(q):
		(l, r) = inlt()
		if lcm == b:
			out.append('0')
		elif r < b:
			out.append('0')
		elif r < lcm:
			out.append(str(r - max(l, b) + 1))
		else:
			if l % lcm < b:
				l = int(l // lcm) * lcm + b
			if r % lcm < b:
				r = int(r // lcm) * lcm - 1
			between = int(r // lcm) - int(l // lcm)
			num = r - l + 1 - b * between
			out.append(str(num))
	print(' '.join(out))


for t in range(int(input())):
	(a, b, Q) = map(int, input().split())
	t = a * b
	cnt = 0
	dp = [0]
	for i in range(1, t + 1):
		if i % a % b != i % b % a:
			cnt += 1
		dp.append(cnt)
	ans = []
	for i in range(Q):
		(l, r) = map(int, input().split())
		l -= 1
		cnt1 = 0
		cnt1 = dp[l - l // t * t]
		cnt2 = dp[r - r // t * t]
		p1 = l // t
		p2 = r // t
		tmp = (p2 - p1) * cnt + cnt2 - cnt1
		ans.append(tmp)
	print(*ans)


nt = int(input())
for _ in range(nt):
	(a, b, q) = map(int, input().split())
	xs = 0
	index_x = []
	for x in range(a * b):
		if x % a % b != x % b % a:
			xs += 1
		index_x.append(xs)
	for t in range(q):
		(l, r) = map(int, input().split())
		s = 0
		ll = l % (a * b)
		rr = r % (a * b)
		if r // (a * b) == l // (a * b):
			if ll > 0:
				s = index_x[rr] - index_x[ll - 1]
			else:
				s = index_x[rr]
			print(s, end=' ')
			continue
		s += xs
		if ll != 0:
			s -= index_x[ll - 1]
		if rr != 0:
			s += index_x[rr]
		s += (r // (a * b) - l // (a * b) - 1) * xs
		print(s, end=' ')
	print()


import math

def func(a, b, l):
	start = max(a, b)
	if start > l:
		return 0
	gc = a * b // math.gcd(a, b)
	time = l // gc
	count = 0
	if time > 0 and gc * time + max(a, b) - 1 <= l:
		count = time + (max(a, b) - 1) * time
	elif time > 0:
		count = time + (max(a, b) - 1) * (time - 1) + l % max(a, b)
	return l - (count + start - 1)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	li = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		li.append(func(a, b, r) - func(a, b, l - 1))
	print(*li)


import math as mt
for _ in range(int(input())):
	(a, b, q) = map(int, input().split(' '))
	(a, b) = (b, a) if a > b else (a, b)
	k = a * b // mt.gcd(a, b)
	for _ in range(q):
		(l, r) = map(int, input().split(' '))
		i = b if b > l else l
		x = 0 if i % k < b else k - i % k
		c = (r - (i - i % k if i % k < b else i + x)) // k * (k - b) + max(0, (r - (i - i % k if i % k < b else i + x)) % k - (b - 1))
		print(c + x, '', end='')
	print()


import sys

def gcd(a, b):
	return gcd(b, a % b) if b else abs(a)

def lcm(a, b):
	return abs(a // gcd(a, b) * b)
t = int(sys.stdin.readline().rstrip())

def main():
	for _ in range(t):
		(a, b, q) = map(int, sys.stdin.readline().split())
		if a > b:
			(a, b) = (b, a)
		L = lcm(a, b)
		res = []
		for _ in range(q):
			(l, r) = map(int, sys.stdin.readline().split())
			((c, d), (e, f)) = (divmod(r, L), divmod(l - 1, L))
			tmp = c * (L - b) + max(d - b + 1, 0)
			tmp -= e * (L - b) + max(f - b + 1, 0)
			res.append(tmp)
		print(*res, sep=' ')
main()


def get_shift_freq(M, m):
	k = 1
	while 1:
		if k * (M % m) % m == 0:
			return k
		k += 1
for testcase in range(int(input())):
	(a, b, q) = map(int, input().split())
	M = max(a, b)
	m = min(a, b)
	shift = M % m
	shift_freq = get_shift_freq(M, m)
	shift_freq_i = shift_freq * M
	answers = []
	for ii in range(q):
		ans = 0
		(l, r) = map(int, input().split())
		if l % shift_freq_i < M:
			pass
		else:
			ans -= l % shift_freq_i
			ans += M
		l -= l % shift_freq_i
		rpos = r % shift_freq_i
		if rpos < M:
			ans -= M * (shift_freq - 1)
		else:
			ans -= shift_freq_i - rpos - 1
		r += shift_freq_i - rpos
		ans += (r - l) // shift_freq_i * ((shift_freq - 1) * M)
		answers.append(ans)
	print(*answers)


def main_function():
	from sys import stdin
	from sys import stdout
	input = stdin.readline
	print = stdout.write

	def count_good(bound: int):
		return bound // pack * good_in_pack[-1] + good_in_pack[bound % pack]
	t = int(input())
	for _ in range(t):
		(a, b, query) = list(map(int, input().split()))
		(a, b) = (min(a, b), max(a, b))
		pack = a * b
		ans = ['0'] * query
		good_in_pack = [0] * pack
		for x in range(1, pack):
			if x % a % b != x % b % a:
				good_in_pack[x] = good_in_pack[x - 1] + 1
			else:
				good_in_pack[x] = good_in_pack[x - 1]
		for q_ix in range(query):
			(l, r) = list(map(int, input().split()))
			ans[q_ix] = str(count_good(r) - count_good(l - 1))
		print(' '.join(ans) + '\n')
main_function()


cases = int(input())
for t in range(cases):
	(a, b, q) = map(int, input().split())
	l = [0] * (a * b)
	for i in range(1, a * b):
		l[i] = l[i - 1] + (i % a % b != i % b % a)
	r = []
	for _ in range(q):
		(li, ri) = map(int, input().split())
		s = ri // (a * b) * l[-1] + l[ri % (a * b)]
		s -= (li - 1) // (a * b) * l[-1] + l[(li - 1) % (a * b)]
		r.append(s)
	print(*r)


import sys
input = sys.stdin.readline
from fractions import gcd
Q = int(input())
Query = []
for _ in range(Q):
	(a, b, q) = map(int, input().split())
	LR = [list(map(int, input().split())) for _ in range(q)]
	Query.append((a, b, q, LR))

def solve(lcm, m, x):
	cycle = x // lcm
	amari = x % lcm
	return (lcm - m) * cycle + max(0, amari - m + 1)
for (a, b, q, LR) in Query:
	lcm = a * b // gcd(a, b)
	m = max(a, b)
	ans = []
	for (l, r) in LR:
		ans.append(solve(lcm, m, r) - solve(lcm, m, l - 1))
	print(*ans)


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	l = []
	l.append(0)
	for i in range(1, a * b + 1):
		x = l[-1]
		l.append(x)
		if i % a % b != i % b % a:
			l[i] += 1
	t = a * b
	x = l[-1]
	lf = []
	for i in range(q):
		(left, r) = map(int, input().split())
		a1 = r // t
		a2 = (left - 1) // t
		ans = a1 * x + l[r % t]
		ans -= a2 * x + l[(left - 1) % t]
		lf.append(ans)
	for i in lf:
		print(i, end=' ')
	print()


from math import gcd

def solve(r, a, b, g):
	num = r // g
	maxg = num * g
	ret = 0
	ret += num * max(a, b)
	if maxg + max(a, b) <= r:
		ret += max(a, b)
	else:
		ret += r - maxg + 1
	return ret
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	g = a * b // gcd(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		count = 0
		if a == b:
			print(0)
			continue
		print(r - l + 1 - (solve(r, a, b, g) - solve(l - 1, a, b, g)), end=' ')
	print('')


import sys, io, os
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(io.IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
T = int(input())
for _ in range(T):
	(A, B, Q) = map(int, input().split())
	if A > B:
		(A, B) = (B, A)
	AB = A * B
	L = [i % A != i % B % A for i in range(AB)]
	cumL = [0] * (AB + 1)
	for i in range(AB):
		cumL[i + 1] = cumL[i] + L[i]
	s = cumL[AB]

	def solve(N):
		return N // AB * s + cumL[N % AB]
	Ans = []
	for _ in range(Q):
		(L, R) = map(int, input().split())
		Ans.append(solve(R + 1) - solve(L))
	print(*Ans)


from sys import stdin, stdout
from collections import defaultdict, Counter
from functools import lru_cache
from math import gcd, floor, ceil

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

def iint():
	return int(stdin.readline().strip())

def istr():
	return stdin.readline().strip()
t = iint()
for _ in range(t):
	(a, b, q) = ilist()
	res = []
	x = 1
	limit = a * b // gcd(a, b)
	p = [0] * limit
	for x in range(1, limit):
		if x % a % b != x % b % a:
			p[x] = 1
		p[x] += p[x - 1]
	for _ in range(q):
		(l, r) = ilist()
		s = 0
		l -= 1
		s += r // limit * p[-1] + p[r % limit]
		s -= l // limit * p[-1] + p[l % limit]
		res.append(str(s))
	print(' '.join(res))


from sys import stdin
from math import gcd

def func(b, n):
	if n < b or n == 1:
		return 0
	m = n // lcm
	count = (m - 1) * b
	if n - m * lcm >= b:
		count += b
	else:
		count += n - m * lcm + 1
	return n - b + 1 - count
for _ in range(int(stdin.readline())):
	w = list(map(int, stdin.readline().split()))
	b = max(w[0], w[1])
	lcm = w[0] * w[1] // gcd(w[0], w[1])
	ans = []
	for i in range(w[2]):
		(l, r) = map(int, stdin.readline().split())
		if lcm == b:
			ans.append(0)
		else:
			ans.append(func(b, r) - func(b, l - 1))
	print(*ans)


import math

def main():
	t = int(input())
	while t > 0:
		(a, b, q) = input().split(' ')
		a = int(a)
		b = int(b)
		q = int(q)
		if a > b:
			(a, b) = (b, a)
		gc = math.gcd(a, b)
		lc = a * b // gc
		cnt = 0
		for i in range(0, lc):
			if i % a % b == i % b % a:
				cnt += 1
		while q > 0:
			q -= 1
			(l, r) = input().split()
			l = int(l)
			r = int(r)
			ans = int(r - l + 1)
			if l % lc < cnt:
				ans -= min(cnt - l % lc, r - l + 1)
			l += lc - l % lc
			if l > r:
				print(ans, end=' ')
				continue
			ans -= (r - l + 1) // lc * cnt
			ans -= min(cnt, (r - l + 1) % lc)
			print(ans, end=' ')
		print()
		t -= 1
main()


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	p = [0] * (a * b)
	for j in range(1, a * b):
		p[j] = p[j - 1]
		if j % a % b != j % b % a:
			p[j] = p[j] + 1
	m = []
	for k in range(q):
		(l, r) = map(int, input().split())
		x = r // len(p)
		y = (l - 1) // len(p)
		m.append(p[r % len(p)] - p[(l - 1) % len(p)] + (x - y) * p[-1])
	print(*m)


from math import *

def io():
	return map(int, input().split())

def op():
	return list(map(int, input().split()))

def cal(r, b, x):
	ans = r
	div = r // x
	prod = div * x
	if div > 0:
		ans -= min(b, r - prod + 1)
		ans -= (div - 1) * b
	return ans - min(b - 1, r)
for _ in range(int(input())):
	(a, b, q) = io()
	if a > b:
		(a, b) = (b, a)
	x = a * b // gcd(a, b)
	for i in range(q):
		(l, r) = io()
		if r < b or x == b:
			print(0, end=' ')
		else:
			va = cal(r, b, x) - cal(l - 1, b, x)
			print(va, end=' ')
	print()


def pgcd(a, b):
	while a % b != 0:
		(a, b) = (b, a % b)
	return b
t = int(input())
for _ in range(t):
	c = input().split()
	a = int(c[0])
	b = int(c[1])
	q = int(c[2])
	if a > b:
		(b, a) = (a, b)
	p = a // pgcd(a, b)
	out = []
	for _ in range(q):
		lr = input().split()
		l = int(lr[0])
		r = int(lr[1])
		v = r - l + 1
		if l // b % p == 0:
			v -= min(b - l % b, r - l + 1)
		if r // b != l // b and r // b % p == 0:
			v -= r % b + 1
		minl = l // b + 1
		if minl % p > 0:
			minl += p - minl % p
		maxr = r // b - 1
		maxr -= maxr % p
		if maxr >= minl:
			v -= b * ((maxr - minl) // p + 1)
		out.append(v)
	print(*out, sep=' ')


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
cases = int(input())
for i in range(cases):
	inp = input().split(' ')
	a = int(inp[0])
	b = int(inp[1])
	q = int(inp[2])
	GCD = gcd(a, b)
	LCM = a * b // GCD
	minst = a if a <= b else b
	mest = a if a >= b else b
	result = ''
	for k in range(q):
		inp2 = input().split(' ')
		l = int(inp2[0])
		r = int(inp2[1]) + 1
		innr = r - r % LCM
		innl = l + (LCM - l % LCM)
		ans = 0
		if innr > innl:
			ans = (innr - innl + 1) // LCM * (LCM - mest)
		if r // LCM == l // LCM:
			tmp = r % LCM - (l % LCM if l % LCM > mest else mest)
			ans += tmp if tmp > 0 else 0
		else:
			tmpl = LCM - (l % LCM if l % LCM > mest else mest)
			tmpr = r % LCM - mest
			ans += tmpl if tmpl > 0 else 0
			ans += tmpr if tmpr > 0 else 0
		result += str(ans) + ' '
	print(result)


from math import gcd

def rn():
	a = int(input())
	return a

def rl():
	a = list(map(int, input().split()))
	return a
for _ in range(int(input())):
	[a, b, q] = rl()
	c = int(a * b // gcd(a, b))
	l = []
	for i in range(c):
		if i % a % b != i % b % a:
			l.append(1)
		else:
			l.append(0)
	lf = l.copy()
	lb = l.copy()
	for i in range(1, c):
		lf[i] += lf[i - 1]
	for i in range(c - 2, -1, -1):
		lb[i] += lb[i + 1]
	tt = lb[0]
	for _ in range(q):
		ans = 0
		[x, y] = rl()
		bg = x + c - (x + c) % c
		sm = y - y % c
		ans += tt * ((sm - bg) // c)
		ans += lb[x % c]
		ans += lf[y % c]
		print(ans, end=' ')
	print('')


from fractions import gcd
get = lambda x, lcm, b: x - b * (x // lcm) - min(b, x % lcm + 1)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		print(get(r, lcm, max(a, b)) - get(l - 1, lcm, max(a, b)))


def gcd(j, k):
	if j == 0:
		return k
	return gcd(k % j, j)

def lcm(j, k):
	return j * k // gcd(j, k)
t = int(input())
while t:
	(a, b, q) = map(int, input().split())
	while q:
		(l, r) = map(int, input().split())
		if max(a, b) % min(a, b) == 0:
			print(0, end=' ')
			q -= 1
			continue
		lcm_ = lcm(a, b)
		bol = 0
		if l / lcm_ >= 1 and l - l // lcm_ * lcm_ < max(a, b):
			bol = 1
		else:
			bol = 0
		p = max(a, b) * (l // lcm_) - bol * (l // lcm_ * lcm_ + max(a, b) - max(l, l // lcm_ * lcm_))
		no_of_equal_mod_above_lcm = (r // lcm_ - 1) * max(a, b) + (min(r // lcm_ * lcm_ + max(a, b) - 1, r) - r // lcm_ * lcm_) + 1 - p
		if r < max(a, b):
			print(0, end=' ')
		elif r // lcm_ == 0:
			print(r - max(a, b, l) + 1, end=' ')
		else:
			print(r - no_of_equal_mod_above_lcm + (-max(a, b, l) + 1), end=' ')
		q -= 1
	print()
	t -= 1


import sys
input = sys.stdin.readline
from math import gcd

def main():
	t = int(input())
	ANS = []
	for _ in range(t):
		(a, b, q) = map(int, input().split())
		lr = [tuple(map(int, input().split())) for _ in range(q)]
		lcm = a * b // gcd(a, b)
		G = []
		now = 0
		for i in range(lcm):
			if i % a % b != i % b % a:
				now += 1
			G.append(now)
		ans = []
		for (l, r) in lr:
			rans = r // lcm * now + G[r % lcm]
			lans = (l - 1) // lcm * now + G[(l - 1) % lcm]
			ans.append(rans - lans)
		ANS.append(ans)
	for ans in ANS:
		print(' '.join(map(str, ans)))
main()


import sys
input = sys.stdin.readline
case = int(input())
for _ in range(case):
	(a, b, q) = map(int, input().split())
	length = a * b
	prefix = [0]

	def compute(x: int) -> int:
		segments = x // length
		return prefix[length] * segments + prefix[x % length]

	def query(l: int, r: int) -> int:
		return compute(r) - compute(l - 1)
	for i in range(1, length + 1):
		cur = 1 if i % a % b != i % b % a else 0
		prefix.append(prefix[-1] + cur)
	for _ in range(q):
		(l, r) = map(int, input().split())
		print(query(l, r), end=' ')
	print()


from math import gcd

def main():
	t = int(input())
	for _ in range(t):
		(a, b, q) = map(int, input().split())
		M = a * b
		M //= gcd(a, b)
		if a > b:
			(a, b) = (b, a)
		aa = []
		for i in range(q):
			(x, y) = map(int, input().split())
			ans = 0
			k = (x - 1) // M
			ans -= k * (M - 1 - b + 1)
			k = y // M
			ans += k * (M - 1 - b + 1)
			z = x - 1
			z %= M
			ans -= max(0, z - b + 1)
			y %= M
			ans += max(0, y - b + 1)
			aa.append(ans if b % a != 0 else 0)
		print(*aa)
	return
main()


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	p = a * b
	ps = []
	rs = 0
	for i in range(p):
		if i % a % b != i % b % a:
			rs += 1
		ps.append(rs)
	ans = []
	for qqq in range(q):
		(l, r) = map(int, input().split())
		l -= 1
		v1 = ps[-1] * (l // p) + ps[l % p]
		v2 = ps[-1] * (r // p) + ps[r % p]
		ans.append(v2 - v1)
	print(*ans)


import sys
input = sys.stdin.readline
from fractions import gcd
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	cumsum = [0]
	for i in range(lcm):
		cumsum.append(cumsum[-1] + int(i % a % b != i % b % a))
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		r += 1
		l_loop = l // lcm
		l_rest = l % lcm
		l_val = cumsum[-1] * l_loop + cumsum[l_rest]
		r_loop = r // lcm
		r_rest = r % lcm
		r_val = cumsum[-1] * r_loop + cumsum[r_rest]
		ans.append(r_val - l_val)
	print(*ans)


def preComp(a, b, p):
	p[0] = 0
	for i in range(1, a * b + 1):
		p[i] = p[i - 1]
		if i % a % b != i % b % a:
			p[i] += 1
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	p = [0 for i in range(a * b + 1)]
	preComp(a, b, p)
	for i in range(q):
		(l, r) = map(int, input().split())
		temp = r // (a * b)
		ans = p[a * b] * temp + p[r % (a * b)]
		temp = (l - 1) // (a * b)
		ans -= p[a * b] * temp + p[(l - 1) % (a * b)]
		print(ans, end=' ')
	print('')


import math

def r(ri, l, a, b):
	s = 0
	k = ri // l
	s = k * max(a, b) - 1
	m = ri - k * l + 1
	s += max(a, b) if m > max(a, b) else m
	return s

def main():
	for _ in range(int(input())):
		(a, b, q) = list(map(int, input().split()))
		qu = [list(map(int, input().split())) for i in range(q)]
		lcm = a * b // math.gcd(a, b)
		for i in qu:
			k = 0
			k = r(i[1], lcm, a, b) - r(i[0] - 1, lcm, a, b)
			print(i[1] - i[0] + 1 - k, end=' ')
		print()
main()


q = int(input())
for _ in range(q):
	(a, b, q1) = map(int, input().split())
	s = [0] * (a * b)
	for x in range(a * b):
		s[x] = 1 if x % a % b != x % b % a else 0
	for i in range(1, a * b):
		s[i] += s[i - 1]
	for q_ in range(q1):
		(l, r) = map(int, input().split())
		li = l % (a * b)
		lsum = s[-1] - s[li - 1] if li else s[-1]
		l += a * b - li + 1
		sz = r - l + 1
		msum = sz // (a * b) * s[-1]
		ri = r % (a * b)
		rsum = s[ri]
		print(lsum + msum + rsum, end=' ')
	print()


t = int(input())
from math import gcd

def solve(x, m, lcm):
	ans = x // lcm * (lcm - m) + max(0, x % lcm - m + 1)
	return ans
for i in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	m = max(a, b)
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split())
		ans.append(solve(r, m, lcm) - solve(l - 1, m, lcm))
	print(*ans, sep=' ')


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def ec(x, y):
	return x * y / gcd(x, y)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	for __ in range(q):
		(l, r) = map(int, input().split())
		tem = int(ec(a, b))
		if r >= b:
			l = max(l, b)
			res = r - l + 1
			n = r // tem - (l - 1) // tem
			res -= n * max(a, b)
			res += max(a, b) - r % tem - 1 if r % tem < max(a, b) else 0
			res -= max(a, b) - l % tem if l % tem < max(a, b) and l % tem != 0 else 0
			print(res)
		else:
			print(0)


import sys, math
input = sys.stdin.buffer.readline

def f(x, b, g, lcm):
	(seq, rest) = divmod(x, lcm)
	return seq * (lcm - b) + max(0, rest - b + 1)
T = int(input())
for testcase in range(T):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	g = math.gcd(a, b)
	lcm = a * b // g
	res = []
	for i in range(q):
		(ll, rr) = map(int, input().split())
		res.append(f(rr, b, g, lcm) - f(ll - 1, b, g, lcm))
	print(*res)


def gcd(a, b):
	while b > 0:
		(a, b) = (b, a % b)
	return a

def play(a, b, l, r):
	print(f'a = {a}, b = {b}')
	for i in range(l, r + 1):
		j = i % a % b
		k = i % b % a
		print(f"{i}: {j} {k} {('YES' if j == k else '')}")
	print()

def solve(a, b, q, l, r):

	def val(n):
		return (cyc - a) * ((n + 1) // cyc) + max((n + 1) % cyc - a, 0)
	(a, b) = (max(a, b), min(a, b))
	cyc = a * b // gcd(a, b)
	return [val(r[i]) - val(l[i] - 1) for i in range(q)]
t = int(input().strip())
for _ in range(t):
	(a, b, q) = list(map(int, input().strip().split()))
	l = []
	r = []
	for _ in range(q):
		x = list(map(int, input().strip().split()))
		l.append(x[0])
		r.append(x[1])
	print(' '.join(map(str, solve(a, b, q, l, r))))


import sys
import math
t = int(input())
for _ in range(t):
	(a, b, q) = [int(c) for c in input().split()]
	(a, b) = (min(a, b), max(a, b))
	pgcd = math.gcd(a, b)
	ppcm = a * b // pgcd
	cumulative_count = [0 for i in range(ppcm)]
	count = 0
	for i in range(ppcm):
		if i % a % b != i % b % a:
			count += 1
		cumulative_count[i] = count
	cumul_per_period = cumulative_count[-1]
	for i in range(q):
		(li, ri) = [int(c) for c in input().split()]
		count_li = cumul_per_period * ((li - 1) // ppcm) + cumulative_count[(li - 1) % ppcm]
		count_ri = cumul_per_period * (ri // ppcm) + cumulative_count[ri % ppcm]
		count = count_ri - count_li
		print(count, end=' ')
	print('')


n = int(input())
for i in range(int(n)):
	(a, b, q) = input().split()
	(a, b, q) = (int(a), int(b), int(q))
	(a, b) = (min(a, b), max(a, b))
	minn = min(a, b)
	maxx = max(a, b)
	mult = a * b
	den = 0
	for h in range(2, 300):
		if a % h == 0 and b % h == 0:
			den = h
		if h == minn:
			break
	if den > 0:
		a //= den
	minn = min(a, b)
	maxx = max(a, b)
	mult = a * b
	topri = []
	for j in range(q):
		(l, r) = input().split()
		(l, r) = (int(l), int(r))
		x = max(l, r) - min(l, r)
		n1 = x + 1
		score = (r // mult - l // mult) * maxx
		if r % mult < maxx:
			score -= maxx
			score += r % mult
			score += 1
		score += max(0, maxx - l % mult)
		to_add = str(n1 - score)
		if a == 1:
			to_add = '0'
		topri.append(to_add)
	print(' '.join(topri))


import math
R = lambda : map(int, input().split())
(t,) = R()
for _ in [0] * t:
	(a, b, q) = R()
	m = a * b // math.gcd(a, b)
	a = max(a, b)
	f = lambda x: x // m * (m - a) - min(1, a - x % m)
	for _ in [0] * q:
		(l, r) = R()
		print(f(r) - f(l - 1))


def get_gcd(u, v):
	while v != 0:
		r = u % v
		u = v
		v = r
	return u

def cnt(l, a, b):
	i = a * b // get_gcd(a, b)
	c = l // i * max(a, b)
	i = l % i
	c += min(i + 1, max(a, b))
	return l - c

def cnt2(l, a, b):
	c = 0
	print('l:', l, 'a:', a, 'b:', b)
	for i in range(0, l + 1):
		if i % a % b == i % b % a:
			print('i:', i)
			c += 1
	print()
	return l - c
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	s = ''
	for i in range(q):
		(l, r) = map(int, input().split())
		c = cnt(r, a, b) - cnt(l - 1, a, b)
		if s != '':
			s += ' '
		s += str(c)
	print(s)


from collections import Counter
from math import gcd

def lcm(a, b):
	return abs(a * b) // gcd(a, b)

def solution(a, b, mx):
	if a < b:
		(a, b) = (b, a)
	major = mx // lcm(a, b) * a
	minor = min(mx % lcm(a, b), a - 1)
	return mx - (major + minor)
n_test = int(input())
for _ in range(n_test):
	(a, b, q) = map(int, input().split())
	for _ in range(q):
		(l, r) = map(int, input().split())
		print(solution(a, b, r) - solution(a, b, l - 1), end=' ')
	print()


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
n = int(input())
for k in range(n):
	(a, b, q) = map(int, input().split())
	m = a * b // gcd(a, b)
	f = [0] * m
	for i in range(1, m):
		f[i] = f[i - 1]
		if i % a % b != i % b % a:
			f[i] += 1
	res_list = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		l -= 1
		res = r // m * f[m - 1] + f[r % m]
		res -= l // m * f[m - 1] + f[l % m]
		res_list.append(res)
	print(' '.join(map(str, res_list)))


import math
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	t = max(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		Ans = 0
		if r < t:
			print(0, end=' ')
		else:
			Ans += r - l + 1
			n = (l - 1) // lcm
			m = r // lcm
			tem = min(r - m * lcm + 1, t)
			Ans -= (m - n - 1) * t
			Ans -= tem
			Ans -= max(t - (l - lcm * n), 0)
			print(Ans, end=' ')
	print()


from sys import stdin, stdout
from math import ceil

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def find(A, B, Q, L, R):
	if A % B == 0 or B % A == 0:
		return 0
	(A, B) = sorted([A, B])
	K = A * B // gcd(A, B)
	R += 1
	Z = max(0, (R // K - L // K - 1) * B)
	x = L - L // K * K
	Z += max(0, min(B - x, R - L))
	y = R - R // K * K
	if R // K != L // K:
		Z += min(y, B)
	return R - L - Z

def main():
	for _ in range(int(stdin.readline())):
		(A, B, Q) = list(map(int, stdin.readline().split()))
		arr = []
		for __ in range(Q):
			(L, R) = list(map(int, stdin.readline().split()))
			arr.append(find(A, B, Q, L, R))
		print(' '.join(map(str, arr)))
main()


def kgv(a, b):
	k = a
	while 1:
		if k % b == 0:
			return k
		k += a
t = int(input())
res = []
for ite in range(t):
	(a, b, q) = map(int, input().split())
	k = kgv(a, b)
	j = max(a, b)
	delta = k - j
	re = []
	for it in range(q):
		(l, r) = map(int, input().split())
		x = l // k
		l -= x * k
		r -= x * k
		dl = 0
		dr = r + (k - r % k)
		upper = dr // k * delta
		if l - j > 0:
			upper -= l - j
		if dr - r - delta > 0:
			upper -= delta
		else:
			upper -= dr - r - 1
		re.append(upper)
	res.append(re)
for el in res:
	print(*el)


import sys
input = sys.stdin.readline

def gcd(x, y):
	if x < y:
		(x, y) = (y, x)
	while y:
		(x, y) = (y, x % y)
	return x
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	g = gcd(a, b)
	m = max(a, b)

	def f(x):
		total = x // lcm
		extra = x % lcm
		return x - (m * total + min(m, extra))
	out = [0] * q
	for i in range(q):
		(l, r) = map(int, input().split())
		out[i] = f(r + 1) - f(l)
	print(*out, sep=' ')


def modmod(x, a, b):
	r1 = x % a % b
	r2 = x % b % a
	return 0 if r1 == r2 else 1
t = int(input())
for case in range(t):
	(a, b, q) = map(int, input().split())
	prod = a * b
	status = prod * [0]
	cum_left = [0]
	total = 0
	for i in range(prod):
		status[i] = modmod(i, a, b)
		total += status[i]
		cum_left.append(total)
	cum_left.pop()
	sum_prod = int(total)
	cum_right = []
	for i in range(prod):
		total -= status[i]
		cum_right.append(total)
	answers = []
	for query in range(q):
		(l, r) = map(int, input().split())
		if prod == 1:
			answers.append(0)
		else:
			lower = l // prod
			upper = (r - 1) // prod + 1
			if r % prod == 0:
				upper += 1
			diff = upper - lower
			result = diff * sum_prod - cum_left[l % prod] - cum_right[r % prod]
			answers.append(result)
	print(*answers)


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')

def solve(x):
	this = x // lcm * prd_cunt
	this += hsh[x % lcm]
	return this
for _ in range(int(input())):
	(a, b, q) = int_array()
	lcm = a * b // gcd(a, b)
	maxi = max(a, b)
	hsh = []
	for x in range(lcm):
		if x % a % b == x % b % a:
			hsh.append(0)
		else:
			hsh.append(1)
	for i in range(1, lcm):
		hsh[i] += hsh[i - 1]
	prd_cunt = hsh[-1]
	for _ in range(q):
		(l, r) = int_array()
		ans = solve(r) - solve(l - 1)
		print(ans, end=' ')
	print('\n')


class Soln:

	def __init__(self, a, b):
		(self.a, self.b) = (a, b)
		pSum = []
		for x in range(a * b):
			if len(pSum) == 0:
				curr = 0
			else:
				curr = pSum[-1]
			if x % a % b != x % b % a:
				curr += 1
			pSum.append(curr)
		self.pSum = pSum

	def brute(self, l, r):
		(a, b) = (self.a, self.b)
		result = 0
		for x in range(l, r + 1):
			if x % a % b != x % b % a:
				result += 1
		return result

	def mod(self, n):
		(a, b) = (self.a, self.b)
		return n % (a * b)

	def npSum(self, l, i):
		mod = self.mod
		(a, b) = (self.a, self.b)
		pSum = self.pSum
		lVal = 0 if l - 1 < 0 else pSum[l - 1]
		if i < a * b - l:
			return pSum[l + i] - lVal
		return pSum[a * b - 1] - lVal + pSum[mod(l + i)]

	def fast(self, l, r):
		mod = self.mod
		(a, b) = (self.a, self.b)
		if r - l < a * b:
			return self.npSum(mod(l), mod(r - l))
		N = (r - l) // (a * b)
		return N * self.npSum(mod(l), a * b - 1) + self.npSum(mod(l), mod(r - l))

def main():
	t = int(input())
	for _ in range(t):
		(a, b, q) = [int(x) for x in input().split(' ')]
		soln = Soln(a, b)
		result = []
		for _ in range(q):
			(l, r) = [int(x) for x in input().split(' ')]
			curr = soln.fast(l, r)
			result.append(curr)
		print(' '.join((str(x) for x in result)))
main()


def nok(x, y):
	(x1, y1) = (x, y)
	(x, y) = (max(x, y), min(x, y))
	while y:
		(x, y) = (y, x % y)
	return int(x1 * y1 / x)
for t in range(int(input())):
	(a, b, q) = list(map(int, input().split()))
	(a, b) = (min(a, b), max(a, b))
	k = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		s = r - l + 1
		n = nok(a, b)
		ml = l // n
		l1 = ml * n
		mr = r // n
		r1 = mr * n
		if l1 + b > l:
			s -= l1 + b - l
		if r1 + b > r:
			s -= r - r1 + 1
			dm = mr - ml - 1
		else:
			dm = mr - ml
		s -= dm * b
		k.append(s)
	print(*k)


import math

def read():
	return map(int, input().split())

def solve(x, b, lcm):
	(d, r) = divmod(x, lcm)
	return d * (lcm - b) + max(0, r - b + 1)
for _ in range(int(input())):
	(A, B, q) = read()
	a = min(A, B)
	b = max(A, B)
	g = math.gcd(a, b)
	lcm = a * b // g
	for i in range(q):
		(l, r) = read()
		print(solve(r, b, lcm) - solve(l - 1, b, lcm), end=' ')
	print()


import math
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	ans = []
	if a == b:
		for i in range(q):
			(l, r) = map(int, input().split())
		ans = [0] * q
		print(*ans)
	else:
		aa = max(a, b)
		bb = min(a, b)
		lc = a * b // math.gcd(a, b)
		div = lc - aa
		ans = []
		for i in range(q):
			(l, r) = map(int, input().split())
			count = 0
			ist = l % lc
			sec = r % lc
			iist = l // lc
			ssec = r // lc
			count += (ssec - iist) * div
			count -= max(0, ist - aa)
			count += max(0, sec - aa + 1)
			ans.append(count)
		print(*ans)


t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().strip().split(' '))

	def gcd(a, b):
		if a == 0:
			return b
		return gcd(b % a, a)

	def lcm(a, b):
		return a * b // gcd(a, b)
	lcm_ = lcm(a, b)
	for j in range(q):
		(l, r) = map(int, input().strip().split(' '))

		def calculate(l, lcm_, a, b):
			k = l // lcm_
			count = k * max(a, b)
			if lcm_ * k + max(a, b) - 1 <= l:
				count += max(a, b)
			else:
				count += l - lcm_ * k + 1
			return count
		res = calculate(r, lcm_, a, b) - calculate(l - 1, lcm_, a, b)
		print(r - l + 1 - res, end=' ')
	print()


import math
from collections import Counter
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
I = lambda : int(input())
IN = lambda : input()
mod = 10 ** 9 + 7

def s(a):
	print(' '.join(list(map(str, a))))
for i in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	x = max(a, b)
	y = min(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		r += 1
		X = r - (r // lcm * x + min(x, r % lcm)) - (l - (l // lcm * x + min(x, l % lcm)))
		print(X, end=' ')
	print()


import math
t = int(input())

def gcd(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a
for _ in range(t):
	(a, b, q) = map(int, input().split())
	boiled_down_to = [0 for x in range(a * b)]
	for i in range(1, a * b):
		boiled_down_to[i] = boiled_down_to[i - 1]
		if i % a % b != i % b % a:
			boiled_down_to[i] += 1
	prod = a * b
	sm = boiled_down_to[-1]
	for _ in range(q):
		(l, r) = map(int, input().split())
		temp = r // prod
		ans = sm * temp + boiled_down_to[r % prod]
		temp = (l - 1) // prod
		ans -= sm * temp + boiled_down_to[(l - 1) % prod]
		print(ans, end=' ')
	print()


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations

def li():
	return list(map(int, sys.stdin.readline().split()))

def num():
	return map(int, sys.stdin.readline().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007

def count(x, lcm, big):
	p = x // lcm
	hot = p * lcm
	done = x
	if p != 0:
		done -= min(big, x - hot + (1 if p != 0 else 0))
	done -= (p - 1) * big if p != 0 else 0
	return done

def solve():
	t = nu()
	for tt in range(t):
		(a, b, q) = num()
		lc = lcm(a, b)
		big = max(a, b)
		for i in range(q):
			(l, r) = num()
			if find_gcd(a, b) == min(a, b):
				print(0, end=' ')
				continue
			if r < big:
				print(0, end=' ')
				continue
			hola = count(r, lc, big)
			l = max(l, big)
			if l > 1:
				hola -= count(l - 1, lc, big)
			print(hola, end=' ')
		print('')
solve()


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)

def f(l, x):
	return l[-1] * (x // k) + l[x % k]
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	k = lcm(a, b)
	l = [0] * k
	for x in range(1, k):
		if x % a % b != x % b % a:
			l[x] = l[x - 1] + 1
	ans = [0] * q
	for i in range(q):
		(le, r) = map(int, input().split())
		ans[i] = f(l, r) - f(l, le - 1)
	print(' '.join(map(str, ans)))


N = int(input())
for _ in range(N):
	(a, b, q) = map(int, input().split())
	l = [0]
	for i in range(1, a * b + 1):
		if i % a % b != i % b % a:
			l.append(l[-1] + 1)
		else:
			l.append(l[-1])
	c = 0
	ls = []
	for i in range(q):
		(l1, r1) = map(int, input().split())
		c = r1 // (a * b) * l[-1] + l[r1 % (a * b)] - l1 // (a * b) * l[-1] - l[l1 % (a * b)]
		if l1 % a % b != l1 % b % a:
			c = c + 1
		ls.append(c)
	print(*ls)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	ha = []
	(a, b) = (min(a, b), max(a, b))
	for i in range(10 ** 5):
		if i % a % b == i % b % a:
			ha.append(i)
	(d, l) = (1, 1)
	for i in range(1, len(ha)):
		if ha[i] - ha[i - 1] != 1:
			d = ha[i] - ha[i - 1]
			l = i
			break
	z = d + l - 1
	ans = []
	for i in range(q):
		(z1, z2) = map(int, input().split())
		f = z2 - z1 + 1
		fa = z1
		z1 -= 1
		d1 = z1 // z + 1
		d2 = (d1 - 1) * l
		d3 = (d1 - 1) * z + l - 1
		if z1 >= d3:
			final1 = d2 + l
		else:
			final1 = d2 + z1 - (d1 - 1) * z + 1
		z1 = z2
		d1 = z1 // z + 1
		d2 = (d1 - 1) * l
		d3 = (d1 - 1) * z + l - 1
		if z1 >= d3:
			final2 = d2 + l
		else:
			final2 = d2 + z1 - (d1 - 1) * z + 1
		ans.append(f - (final2 - final1))
	print(*ans)


import math

def lcm(a, b):
	least_common_multiple = max(a, b)
	while least_common_multiple % a != 0 or least_common_multiple % b != 0:
		least_common_multiple += 1
	return least_common_multiple
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm_of_a_and_b = lcm(a, b)
	x = 0
	for ___ in range(q):
		(l, r) = map(int, input().split())
		if (l - 1) // lcm_of_a_and_b * lcm_of_a_and_b == 0:
			if l - 1 >= max(a, b):
				subtractible = l - max(a, b)
			else:
				subtractible = 0
		elif (l - 1) // lcm_of_a_and_b * lcm_of_a_and_b == l - 1:
			subtractible = (l - 1) // lcm_of_a_and_b * (lcm_of_a_and_b - max(a, b))
		elif (l - 1) // lcm_of_a_and_b * lcm_of_a_and_b < l - 1:
			subtractible = (l - 1) // lcm_of_a_and_b * (lcm_of_a_and_b - max(a, b))
			if l - 1 >= max(a, b) + (l - 1) // lcm_of_a_and_b * lcm_of_a_and_b:
				subtractible += l - (max(a, b) + (l - 1) // lcm_of_a_and_b * lcm_of_a_and_b)
		if r // lcm_of_a_and_b * lcm_of_a_and_b == 0:
			if r >= max(a, b):
				minuend = r - max(a, b) + 1
			else:
				minuend = 0
		elif r // lcm_of_a_and_b * lcm_of_a_and_b == r:
			minuend = r // lcm_of_a_and_b * (lcm_of_a_and_b - max(a, b))
		elif r // lcm_of_a_and_b * lcm_of_a_and_b < r:
			minuend = r // lcm_of_a_and_b * (lcm_of_a_and_b - max(a, b))
			if r >= max(a, b) + r // lcm_of_a_and_b * lcm_of_a_and_b:
				minuend += r + 1 - (max(a, b) + r // lcm_of_a_and_b * lcm_of_a_and_b)
		print(minuend - subtractible, end=' ')
	print()


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	sum1 = [0]
	mul = a * b
	summ = 0
	for x in range(1, mul + 1):
		if x % a % b != x % b % a:
			summ += 1
			sum1.append(summ)
		else:
			sum1.append(summ)
	for i in range(q):
		(l, r) = map(int, input().split())
		right = summ * (r // mul) + sum1[r % mul]
		left = summ * ((l - 1) // mul) + sum1[(l - 1) % mul]
		print(right - left, end=' ')
	print()


def NOD(a, b):
	if a == 0:
		return b
	return NOD(b % a, a)

def f(a, b, x):
	(a, b) = (max(a, b), min(a, b))
	if a % b == 0:
		return 0
	ans = x // (a * b // NOD(a, b)) * (a * b // NOD(a, b) - a) + max(0, x % (a * b // NOD(a, b)) - a + 1)
	return ans
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	(a, b) = (max(a, b), min(a, b))
	for j in range(q):
		(l, r) = map(int, input().split())
		print(f(a, b, r) - f(a, b, l - 1), end=' ')
	print()


import math
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	lr = []
	ans = []
	for i in range(q):
		lr.append(list(map(int, input().split())))
	for i in lr:
		if i[1] < a or i[1] < b or b % a == 0 or (a % b == 0):
			ans.append(0)
		else:
			lc = a * b // math.gcd(a, b)
			lb = max(i[0], max(a, b))
			rb = i[1]
			if lb % lc < max(a, b):
				lb += max(a, b) - lb % lc
			if rb % lc < max(a, b):
				rb = rb - rb % lc - 1
			m = rb // lc - lb // lc
			m *= max(a, b)
			if lb % lc < max(a, b):
				m -= lb % lc
			if rb % lc < max(a, b):
				m -= max(a, b) - rb % lc - 1
			ans.append(1 + rb - lb - m)
	print(' '.join(map(str, ans)))


import sys
from math import gcd
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def lcm(x, y):
	return x * y // gcd(x, y)
for _ in range(INT()):
	(mod1, mod2, Q) = MAP()
	N = lcm(mod1, mod2)
	ok = [0] * N
	for i in range(N):
		if i % mod1 % mod2 != i % mod2 % mod1:
			ok[i] = 1
	acc = [0] + list(accumulate(ok))
	ans = [0] * Q
	for i in range(Q):
		(l, r) = MAP()
		ln = l // N
		rn = r // N
		segcnt = rn - ln
		modl = l % N
		modr = r % N
		cnt = acc[N] - acc[modl] - (acc[N] - acc[modr + 1])
		ans[i] = segcnt * acc[N] + cnt
	print(*ans)


import math
a = 0
b = 0
p = 0

def get(x):
	k = x // p
	return x - (k * max(a, b) + min(max(a, b), x % p + 1))
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	p = math.gcd(a, b)
	p = a * b // p
	for k in range(q):
		(l, r) = map(int, input().split())
		print(get(r) - get(l - 1), end=' ')
	print(' ')


import math
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	bb = b
	g = math.gcd(a, b)
	m = a // g
	gg = b * m
	for x in range(q):
		(l, r) = map(int, input().split())
		(m1, m2) = (l // gg, r // gg)
		(n1, n2) = (l % gg, r % gg)
		if m1 != m2:
			c1 = 0 if bb - n1 < 0 else bb - n1
			c2 = bb if n2 + 1 > bb else n2 + 1
			pp = m2 - m1 - 1
			ga = c1 + c2 + pp * bb
		else:
			if n2 > b - 1:
				n2 = b - 1
			ga = 0 if n2 - n1 + 1 < 0 else n2 - n1 + 1
		print(r - l + 1 - ga, end=' ')
	print()


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split(' '))
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split(' '))
		count = 0
		m = lcm(a, b)
		if l > max(a, b):
			x = r // m
			y = (l - 1) // m
			if y * m + max(a, b) - 1 >= l:
				l = y * m + max(a, b)
			z = 0
			if r % m == 0:
				x -= 1
				z += 1
			else:
				x1 = x * m + max(a, b) - 1
				if x1 >= r:
					r = x * m - 1
					x -= 1
			x = x - (l - 1) // m
			x1 = x * max(a, b) + z
			ans.append(r - l - x1 + 1)
		elif r >= max(a, b):
			x = r // m
			z = 0
			if r % m == 0:
				x -= 1
				z += 1
			else:
				x1 = x * m + max(a, b) - 1
				if x1 >= r:
					r = x * m - 1
					x -= 1
			x1 = x * max(a, b) + z
			ans.append(r - x1 - max(a, b) + 1)
		else:
			ans.append(0)
	print(*ans)


import math

def gcd(a, b):
	if a == 0:
		return b
	if b == 0:
		return a
	return gcd(b % a, a)

def calc(l, lcm, a, b):
	num = l // lcm
	ans = num * max(a, b)
	ans += min(l - num * lcm + 1, max(a, b))
	return ans

def main():
	for q in range(int(input())):
		(a, b, q) = map(int, input().split())
		lcm = a * b // gcd(a, b)
		for i in range(q):
			(l, r) = map(int, input().split())
			ans = r + 1 - calc(r, lcm, a, b) - (l - calc(l - 1, lcm, a, b))
			print(ans, end=' ')
		print('')
main()


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
from math import gcd
for _ in range(II()):
	(a, b, q) = MI()
	lcm = a * b // gcd(a, b)
	m = max(a, b)
	res = []
	for __ in range(q):
		(l, r) = MI()
		(n1, n2) = (l // lcm, r // lcm)
		cnt = 0
		for i in range(lcm * n1, lcm * n1 + m):
			if l <= i <= r:
				cnt += 1
		if n2 != n1:
			for i in range(lcm * n2, lcm * n2 + m):
				if l <= i <= r:
					cnt += 1
			cnt += (n2 - n1 - 1) * m
		res.append(r - l + 1 - cnt)
	print(*res)


import math
t = int(input())

def get(A, B, N):
	if N == -1:
		return 0
	if max(A, B) > N:
		return N + 1
	GCD = int(math.gcd(A, B))
	X = GCD * (A // GCD) * (B // GCD)
	Delta = min(max(A, B), N - N // X * X + 1)
	return max(A, B) + max(N // X - 1, 0) * max(A, B) + Delta * int(N >= X)
for _ in range(t):
	(a, b, q) = map(int, input().split())
	for _ in range(q):
		(l, r) = map(int, input().split())
		ans = abs(l - r) + 1 - (get(a, b, r) - get(a, b, l - 1))
		print(int(ans), end=' ')
	print()


import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
from math import gcd

def go():
	(a, b, q) = map(int, input().split())
	g = a * b // gcd(a, b)
	m = max(a, b)

	def until(v):
		result = (v + 1) // g * m + min((v + 1) % g, m)
		result = v + 1 - result
		return result
	res = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		res.append(until(r) - until(l - 1))
	return ' '.join(map(str, res))
t = int(input())
ans = []
for _ in range(t):
	ans.append(str(go()))
print('\n'.join(ans))


t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	s = []
	for x in range(a * b):
		if x % a % b == x % b % a:
			s.append(0)
		else:
			s.append(1)
	n = sum(s)
	listofsum = [0]
	temp = 0
	for x in range(a * b):
		temp += s[x]
		listofsum.append(temp)
	for j in range(q):
		(l, r) = map(int, input().split())
		rtemp = r // (a * b)
		ltemp = l // (a * b)
		if (rtemp + 1) * (a * b) <= r:
			rtemp += 1
		if (ltemp + 1) * (a * b) <= l:
			ltemp += 1
		print((rtemp - ltemp) * n + listofsum[r % (a * b) + 1] - listofsum[l % (a * b)])


from math import gcd

def fn(l, r, a, b):
	g = a * b // gcd(a, b)
	m = max(a, b)
	if l // g == r // g:
		l %= g
		r %= g
		l = max(l, m)
		r = max(r, m)
		return r - l
	d = r // g - l // g
	l %= g
	r %= g
	total = (d - 1) * (g - m)
	total += max(r - m, 0)
	l = min(g - l, g - m)
	total += l
	return total
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	arr = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		arr.append(fn(l, r + 1, a, b))
	print(*arr)


import sys, os
input = sys.stdin.read().split('\n')[::-1].pop

def print(s):
	sys.stdout.write(str(s) + ' ')

def endl():
	sys.stdout.write('\n')

def gcd(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a
t = int(input())
ans = []
for _ in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	(a, b) = (min(a, b), max(a, b))
	c = a * b // gcd(a, b)
	v = []
	curr = 0
	for i in range(c):
		curr += i % a % b != i % b % a
		v.append(curr)
	for _ in range(q):
		(l, r) = [int(x) for x in input().split()]
		l -= 1
		val = 0
		val += curr * (r // c) + v[r % c]
		val -= curr * (l // c) + v[l % c]
		print(val)
	endl()


import math
test = int(input())

def my_cnt(x, c, b):
	m = x // c
	return m * b + min(x - c * m + 1, b)
for case in range(test):
	(a, b, q) = [int(i) for i in input().split()]
	lcm = a * b // math.gcd(a, b)
	for qry in range(q):
		(x, y) = [int(i) for i in input().split()]
		true_cnt = my_cnt(y, lcm, max(a, b)) - my_cnt(x - 1, lcm, max(a, b))
		print(y - x + 1 - true_cnt, end=' ')
	print()


import os
from sys import stdin

def gcd(a, b):
	while a > 0:
		(b, a) = (a, b % a)
	return b

def solve(tc):
	(a, b, q) = map(int, stdin.readline().split())
	if a > b:
		(a, b) = (b, a)
	g = a * b // gcd(a, b)
	ans = []
	for i in range(q):
		(l, r) = map(int, stdin.readline().split())
		group = r // g
		if group > 0:
			cnt = group * b + min(r % g, b - 1)
		else:
			cnt = min(r % g, b - 1)
		res = r - cnt
		l -= 1
		group = l // g
		if group > 0:
			cnt = group * b + min(l % g, b - 1)
		else:
			cnt = min(l % g, b - 1)
		res -= l - cnt
		ans.append(res)
	print(' '.join([str(x) for x in ans]))
tcs = int(stdin.readline())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for _ in range(int(input())):
	(a, b, q) = tuple(map(int, input().split()))
	(a, b) = (min(a, b), max(a, b))
	nok = a * b // gcd(a, b)
	answers = []
	for _ in range(q):
		(left, right) = tuple(map(int, input().split()))
		delta = 0
		if left % nok != 0:
			ost = left % nok
			left -= ost
			delta += max(ost - b, 0)
		if right % nok != nok - 1:
			ost = right % nok
			right += nok - ost - 1
			if ost >= b:
				delta += nok - ost - 1
			else:
				delta += nok - b
		answers.append(right - left + 1 - delta - (right - left + 1) // nok * b)
	print(*answers)


def soln_slow(a, b, n, l):
	ct = 0
	for x in range(1, n + 1):
		if x % a % b == x % b % a:
			print(x, 'yes')
		else:
			ct += 1
			print(x, 'no')
	return ct

def soln(a, b, n, l):
	mb = n // b
	mbn = mb * b
	ml = n // l
	if mbn != ml * l:
		return max((mb - 1 - ml) * b + min(n - mbn + 1, b), 0)
	else:
		return max((mb - ml) * b, 0)

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	lc = lcm(a, b)
	solns = []
	for __ in range(q):
		(l, r) = map(int, input().split())
		solns.append(soln(a, b, r, lc) - soln(a, b, l - 1, lc))
	print(' '.join(map(str, solns)))


from math import gcd
import sys
from copy import copy, deepcopy
input = sys.stdin.readline
inf = 100000000000000000
mod = 998244353
for CASES in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = b // gcd(a, b) * a
	A = [0] * (lcm + 3)
	for i in range(lcm):
		A[i + 1] = A[i]
		if i % a % b != i % b % a:
			A[i + 1] += 1
	ANS = []
	for QUERY in range(q):
		(l, r) = map(int, input().split())
		(divl, resl) = divmod(l, lcm)
		(divr, resr) = divmod(r, lcm)
		ANS.append((divr - divl) * A[lcm] - A[resl] + A[resr + 1])
	print(*ANS)


import math
I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(a, b, q) = I()
	lc = a * b // math.gcd(a, b)
	k = lc - max(a, b)
	an = []
	for i in range(q):
		(l, r) = I()
		if a == b:
			an.append(0)
		else:
			x = r // lc * k + max(0, r % lc - max(a, b) + 1)
			x -= (l - 1) // lc * k + max(0, (l - 1) % lc - max(a, b) + 1)
			an.append(x)
	print(*an)


import sys
import math

def lcm(aa, bb):
	g = max(aa, bb)
	while not (g % aa == 0 and g % bb == 0):
		g += 1
	return g
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	LCM = lcm(a, b)
	greater = max(a, b)
	for _ in range(q):
		(l, r) = map(int, input().split())
		times = r // LCM - l // LCM + 1
		newr = r // LCM * LCM + greater - 1
		newl = l // LCM * LCM
		total = newr - newl + 1
		total -= times * greater
		if newr <= r:
			total += r - newr
		if newl + greater <= l:
			total -= l - (greater + newl)
		print(total, end=' ')
	print()


from itertools import accumulate

def solve():
	(a, b, q) = map(int, input().split())
	tb = [int(i % a % b != i % b % a) for i in range(a * b)]
	bs = sum(tb)
	tb += tb
	tbs = [0] + list(accumulate(tb))
	res = []
	for i in range(q):
		(l, r) = map(int, input().split())
		r += 1
		w = (r - l) // (a * b)
		ans = w * bs
		r -= w * a * b
		u = l // (a * b)
		l %= a * b
		r -= u * (a * b)
		ans += tbs[r] - tbs[l]
		res.append(ans)
	print(*res)
t = int(input())
for _ in range(t):
	solve()


import math

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def fun(num, a, b):
	g = gcd(a, b)
	g = a * b // g
	chk = num // g
	ans = 0
	ans += chk * b
	ans += min(num - chk * g + 1, b)
	return ans
t = int(input())
for _ in range(t):
	(a, b, q) = list(map(int, input().split()))
	if a > b:
		(a, b) = (b, a)
	ans = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		ans = fun(r, a, b) - fun(l - 1, a, b)
		print(r - l + 1 - ans, end=' ')
	print()


def gcd(a, b):
	if a < b:
		(a, b) = (b, a)
	if b == 0:
		return a
	return gcd(b, a % b)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	tt = []
	for i in range(q):
		(l, r) = map(int, input().split())
		if b < a:
			(a, b) = (b, a)
		if b % a == 0:
			tt.append(0)
		else:
			x = a * b // gcd(a, b)
			ans = r // x * b
			ans += min(r % x + 1, b)
			ans -= (l - 1) // x * b
			ans -= min((l - 1) % x + 1, b)
			tt.append(r - l + 1 - ans)
	print(*tt)


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	(cnt, arr, ansArr) = (0, [0], [])
	for i in range(1, lcm + 1):
		if i % a % b != i % b % a:
			cnt += 1
		arr.append(cnt)
	for _ in range(q):
		(l, r) = map(int, input().split())
		ans = r // lcm * cnt + arr[r % lcm]
		ans -= l // lcm * cnt + arr[l % lcm]
		if l % a % b != l % b % a:
			ans += 1
		ansArr.append(str(ans))
	print(' '.join(ansArr))


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().split()]

def st():
	return input()

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]

def giveab(a, b):
	l = []
	for i in range(1, a * b + 1, 1):
		l.append(1 if i % a % b != i % b % a else 0)
	return l[:]

def giveforanum(r, s, l):
	temp = r // (a * b)
	up = temp * s
	r %= a * b
	return up + l[r]
for _ in range(val()):
	(a, b, q) = li()
	l1 = giveab(a, b)
	pref = [0]
	for i in l1:
		pref.append(pref[-1] + i)
	s = sum(l1)
	ans = []
	for i in range(q):
		(l, r) = li()
		ans.append(giveforanum(r, s, pref) - giveforanum(l - 1, s, pref))
	print(*ans)


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)

def main():

	def f(x):
		x += 1
		(p, r) = divmod(x, L)
		res = p * d + max(0, r - mx)
		return res
	for _ in range(II()):
		(a, b, q) = MI()
		L = lcm(a, b)
		mx = max(a, b)
		d = L - mx
		ans = []
		for _ in range(q):
			(l, r) = MI()
			ans.append(f(r) - f(l - 1))
		print(*ans)
main()


def gcd(u, v):
	if u < v:
		return gcd(v, u)
	if v == 0:
		return u
	return gcd(v, u % v)

def case(a, b, ld):
	(l, r) = map(int, input().split())
	if a % b == 0:
		return 0
	if r < max(a, b):
		return 0
	low = l // ld
	high = r // ld
	raw = (high - low) * a
	raw -= min(l - low * ld, a)
	raw += min(r - high * ld + 1, a)
	return r - l + 1 - raw

def sol():
	(a, b, q) = map(int, input().split())
	if a < b:
		(a, b) = (b, a)
	d = gcd(a, b)
	b_ = b // d
	ld = b_ * a
	res = []
	for _ in range(q):
		res.append(str(case(a, b, ld)))
	return res
t = int(input())
for _ in range(t):
	print(' '.join(sol()))


import math

def lcm(a, b):
	return a * b // math.gcd(a, b)

def solve(r, y, b):
	if r == 0:
		return 0
	else:
		div = r // y
		tot = r
		prod = div * y
		if div != 0:
			tot = tot - min(b, r - prod + 1)
			tot = tot - (div - 1) * b
		tot = tot - min(r, b - 1)
		return tot
t = int(input())
for _ in range(t):
	(a, b, q) = [int(c) for c in input().split()]
	(a, b) = (min(a, b), max(a, b))
	for i in range(q):
		(l, r) = [int(c) for c in input().split()]
		lc = lcm(a, b)
		if lc == b or r < b:
			print(0, end=' ')
		else:
			ans = solve(r, lc, b)
			if l > 1:
				ans -= solve(l - 1, lc, b)
			print(ans, end=' ')
	print()


def gcd(a, b):
	(x, y) = (a, b)
	while y != 0:
		(x, y) = (y, x % y)
	return x

def equalize(x, a, b, l):
	m = max(a, b)
	if a == b:
		return x
	else:
		y = x // l
		z = min(x % l + 1, m)
		return y * m + z
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	g = gcd(a, b)
	L = a // g * b
	for i in range(q):
		(l, r) = map(int, input().split())
		if i != q - 1:
			print(r - l + 1 - equalize(r, a, b, L) + equalize(l - 1, a, b, L), end=' ')
		else:
			print(r - l + 1 - equalize(r, a, b, L) + equalize(l - 1, a, b, L))


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
t = int(input())
for _ in range(t):
	(a, b, q) = list(map(int, input().split()))
	if a > b:
		(a, b) = (b, a)
	nok = a * b // gcd(a, b)
	ans = []
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		left_p = l - l % nok
		right_p = r + (nok - r % nok)
		eq = max(0, (right_p - nok - (left_p + nok)) // nok) * b
		ll = max(0, min(r + 1, left_p + b) - l)
		eq += ll
		r_p = r - r % nok
		if l < r_p <= r:
			rr = max(0, min(r_p + b, r + 1) - r_p)
			eq += rr
		ans.append(r - l + 1 - eq)
	print(' '.join(map(str, ans)))


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b // gcd(a, b)

def f(x, lcm, max_ab):
	return x // lcm * (lcm - max_ab) + max(x % lcm - (max_ab - 1), 0)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	c = lcm(a, b)
	d = max(a, b)
	ending = ' '
	for i in range(q):
		(li, ri) = map(int, input().split())
		if i == q - 1:
			ending = '\n'
		print(f(ri, c, d) - f(li - 1, c, d), end=ending)


import sys
from os import path
if path.exists('C:/Users/prana/Desktop/sublime/input.txt'):
	sys.stdin = open('C:/Users/prana/Desktop/sublime/input.txt', 'r')
	sys.stdout = open('C:/Users/prana/Desktop/sublime/output.txt', 'w')
import math

def solve():
	abq = list(map(int, input().split()))
	a = abq[0]
	b = abq[1]
	q = abq[2]
	if a > b:
		a ^= b
		b ^= a
		a ^= b
	an = []
	for i in range(q):
		lr = list(map(int, input().split()))
		l = lr[0]
		r = lr[1]
		lc = a * b // math.gcd(a, b)
		print(a, b, lc, file=sys.stderr)
		ans = max(b, r)
		if b == lc:
			an.append(0)
		elif ans == b and b != r:
			an.append(0)
		else:
			p = max(b, l)
			rm = r % lc
			lm = p % lc
			ch = r // lc - p // lc
			temp1 = ch * b
			templ = max(0, b - lm)
			tempr = max(1, b - rm) - 1
			temp = temp1 - tempr + templ
			n = r - p + 1
			print(temp1, templ, tempr, file=sys.stderr)
			ans = n - temp
			an.append(ans)
			print(lm, rm, ch, r - p, temp1, file=sys.stderr)
	for i in an:
		print(i, end=' ')
	print(end='\n')
T = 1
T = int(input())
for _ in range(1, T + 1):
	print('Case #', _, file=sys.stderr)
	solve()


import io
import os
from sys import stdout
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	c = [0]
	for i in range(1, a * b + 1):
		c.append(c[-1])
		if i % a % b != i % b % a:
			c[-1] += 1
	for i in range(q):
		(l, r) = map(int, input().split())
		l = l - 1
		print(r // (a * b) * c[-1] + c[r % (a * b)] - (l // (a * b) * c[-1] + c[l % (a * b)]), end=' ')
	stdout.write('\n')


from math import *

def main():
	from bisect import bisect_left as bl, bisect_right as br, insort
	import sys
	import heapq
	from collections import defaultdict as dd, deque

	def data():
		return sys.stdin.readline().strip()

	def mdata():
		return map(int, data().split())
	out = sys.stdout.write
	for t in range(int(data())):
		(a, b, q) = mdata()
		g = gcd(a, b)
		lcm = a // g * b
		ans = []
		for i in range(q):
			(l, r) = mdata()
			k1 = l // lcm
			k2 = r // lcm
			ans.append((k2 - k1) * (lcm - max(a, b)) - max(0, l - k1 * lcm - max(a, b)) + max(0, r - k2 * lcm - max(a, b) + 1))
		out(' '.join(map(str, ans)) + '\n')
main()


for t in range(int(input())):
	(a, b, q) = list(map(int, input().split()))
	c = a * b
	x = [0] * (c + 1)
	for i in range(c):
		if i % a % b != i % b % a:
			x[i] = 1
		x[i] += x[i - 1]

	def get(y):
		if y < 0:
			return 0
		cnt = y // c * x[c - 1]
		return cnt + x[y % c]
	ans = []
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		ans.append(get(r) - get(l - 1))
	print(*ans)


import sys
input = sys.stdin.buffer.readline

def getCounts(r):
	nCycles = r // cycleLength
	remainder = r - nCycles * cycleLength
	return nCycles * tot + cycleCounts[remainder]
t = int(input())
for _ in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	cycleLength = a * b
	cycleCounts = [0] * cycleLength
	tot = 0
	for x in range(cycleLength):
		if x % a % b != x % b % a:
			tot += 1
		cycleCounts[x] = tot
	ans = []
	for __ in range(q):
		(l, r) = [int(x) for x in input().split()]
		ans.append(getCounts(r) - getCounts(l - 1))
	print(' '.join([str(x) for x in ans]))


def cal(x, y):
	(a, b) = (x, y)
	while b > 0:
		(a, b) = (b, a % b)
	return int(a * (x / a * (y / a)))

def do(num, M, maxx):
	ans = 0
	if num % M > maxx:
		ans += num % M - maxx
	ans += num // M * (M - maxx)
	return int(ans)
N = int(input())
for _ in range(N):
	(a, b, q) = list(map(int, input().split(' ')))
	M = cal(a, b)
	tmp = []
	for i in range(q):
		(l, r) = list(map(int, input().split(' ')))
		res = do(r + 1, M, max(a, b)) - do(l, M, max(a, b))
		tmp.append(res)
	for i in range(len(tmp) - 1):
		print(tmp[i], end=' ')
	print(tmp[len(tmp) - 1])


import math

def not_equal(a, b, lcm, i):
	if i <= b - 1:
		return 0
	out = i - b + 1
	n_seqs = (i - b) // lcm
	out -= n_seqs * b
	mod = i % lcm
	if mod < b:
		out -= mod + 1
	return out

def naive_not_equal(a, b, val):
	count = 0
	for i in range(val + 1):
		if i % a % b != i % b % a:
			count += 1
	return count
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	lcm = a * b // math.gcd(a, b)
	out = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		lhs = not_equal(a, b, lcm, l - 1)
		rhs = not_equal(a, b, lcm, r)
		ans = rhs - lhs
		out.append(str(ans))
	print(' '.join(out))


from bisect import bisect_left

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	vals = list(map(int, input().split()))
	a = vals[0]
	b = vals[1]
	q = vals[2]
	output = []
	values = []
	lcm = a * b // GCD(a, b)
	for k in range(a * b // GCD(a, b)):
		if k % a % b != k % b % a:
			values.append(k)
	for s in range(q):
		qs = list(map(int, input().split()))
		l = qs[0] - 1
		r = qs[1]
		count = 0
		val1 = 0
		val2 = 0
		if len(values) == 0:
			output.append(str(0))
		else:
			if r >= lcm:
				val1 = r // lcm * len(values)
			r1 = r % lcm
			rem1 = bisect_left(values, r1)
			if rem1 > len(values) - 1:
				rem1 -= 1
			if values[rem1] == r1:
				rem1 += 1
			if l >= lcm:
				val2 = l // lcm * len(values)
			r2 = l % lcm
			rem2 = bisect_left(values, r2)
			if rem2 > len(values) - 1:
				rem2 -= 1
			if values[rem2] == r2:
				rem2 += 1
			count += val1 + rem1 - (val2 + rem2)
			output.append(str(count))
	print(' '.join(output))


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def compute_lcm(a, b):
	return a * b // gcd(a, b)

def find(r, a, b):
	lcm = compute_lcm(a, b)
	t = r // lcm + 1
	if r % lcm < max(a, b):
		t -= 1
		ans = r // lcm * lcm - t * max(a, b)
	else:
		ans = r - t * max(a, b) + 1
	return ans
n = int(input())
for _ in range(n):
	(a, b, q) = map(int, input().split())
	final = []
	for k in range(q):
		(l, r) = map(int, input().split())
		if a > b and a % b == 0:
			final.append(0)
		elif a <= b and b % a == 0:
			final.append(0)
		else:
			final.append(find(r, a, b) - find(l - 1, a, b))
	print(' '.join(map(str, final)))


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	prefix_sum = [0]
	length = a * b
	summary = 0
	for x in range(1, length + 1):
		if x % a % b != x % b % a:
			summary += 1
			prefix_sum.append(summary)
		else:
			prefix_sum.append(summary)
	for i in range(q):
		(l, r) = map(int, input().split())
		right = summary * (r // length) + prefix_sum[r % length]
		left = summary * ((l - 1) // length) + prefix_sum[(l - 1) % length]
		print(right - left, end=' ')
	print()


import functools
import math
import sys
t = int(sys.stdin.readline())

def solve(a, b, queries):
	if a > b:
		(a, b) = (b, a)
	gcd = math.gcd(a, b)
	lcm = gcd * (a // gcd) * (b // gcd)
	result = []

	@functools.lru_cache(None)
	def count_to_x(x):
		if x < lcm:
			return 0 if x < b else x - b + 1
		else:
			r = x // lcm * b + min(x - x // lcm * lcm + 1, b)
		return x + 1 - r
	for (li, ri) in queries:
		result.append(count_to_x(ri) - count_to_x(li - 1))
	return result
for _ in range(t):
	(a, b, q) = map(int, sys.stdin.readline().split())
	queries = []
	for _ in range(q):
		queries.append(list(map(int, sys.stdin.readline().split())))
	r = solve(a, b, queries)
	print(' '.join(map(str, r)))


def f(x, a, b):
	global prefixes
	return x // (a * b) * prefixes[a * b] + prefixes[x % (a * b)]
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	prefixes = [0] * (a * b + 1)
	for i in range(2, a * b + 1):
		prefixes[i] = prefixes[i - 1]
		if i % a % b != i % b % a:
			prefixes[i] += 1
	for i in range(q):
		(l, r) = map(int, input().split())
		print(f(r, a, b) - f(l - 1, a, b), end=' ')
	print()


t = int(input())
for case in range(1, t + 1):
	(a, b, q) = list(map(int, input().split()))
	res = [x % a % b != x % b % a for x in range(a * b)]
	running_total = [0 for x in range(a * b)]
	running_total[0] = 0
	for x in range(1, a * b):
		running_total[x] = running_total[x - 1] + int(res[x])
	totals = []
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		total = 0
		total += r // (a * b) * running_total[-1]
		total -= (l - 1) // (a * b) * running_total[-1]
		total += running_total[r % (a * b)]
		total -= running_total[(l + a * b - 1) % (a * b)]
		totals.append(str(total))
	print(' '.join(totals))


import math
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	answer = ''
	for j in range(q):
		(l, r) = map(int, input().split())
		timesEqual = 0
		lcm = a * b // math.gcd(a, b)
		fullRotations = (r - l + 1) // lcm
		timesEqual += max(a, b) * fullRotations
		lmod = l % lcm
		rmod = r % lcm
		if (r - l + 1) % lcm != 0:
			if lmod < max(a, b):
				if rmod > lmod:
					timesEqual += min(rmod + 1, max(a, b)) - lmod
				elif rmod < lmod:
					timesEqual += max(a, b) - lmod + rmod + 1
				else:
					timesEqual += 1
			elif rmod < lmod:
				timesEqual += min(rmod + 1, max(a, b))
		answer += str(r - l + 1 - timesEqual) + ' '
	print(answer)


import math
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	arr = []
	for i in range(q):
		ans = 0
		(l, r) = map(int, input().split())
		total = r - l + 1
		last = r // lcm
		first = l // lcm + 1
		pq = math.inf
		if r % lcm < max(a, b):
			pq = r % lcm + 1
		ans += min((last - first) * max(a, b) + pq, (last - first + 1) * max(a, b))
		ans = max(0, ans)
		if l % lcm < max(a, b):
			ans += max(a, b) - 1 - l % lcm + 1
		f_ans = max(0, total - ans)
		arr.append(f_ans)
	print(*arr)


def hcf(a, b):
	if a == 0:
		return b
	return hcf(b % a, a)
for t in range(int(input())):
	(a, b, q1) = map(int, input().split())
	if b < a:
		(a, b) = (b, a)
	lcm = a * b // hcf(a, b)
	for i in range(q1):
		(l, r) = map(int, input().split())
		c1 = r // lcm
		l = l - 1
		if l == 0:
			c2 = 0
		else:
			c2 = l // lcm
		if c1 == 0:
			p = 0
		else:
			p = (c1 - 1) * b
		if r >= b:
			p = p + b - 1
		else:
			p = p + r
		if r - c1 * lcm >= b and c1 > 0:
			p = p + b
		elif c1 > 0:
			p = p + (r - c1 * lcm + 1)
		if c2 > 0:
			q = (c2 - 1) * b
		else:
			q = 0
		if l >= b:
			q = q + b - 1
		else:
			q = q + l
		if l - c2 * lcm >= b and c2 > 0:
			q = q + b
		elif c2 > 0:
			q = q + (l - c2 * lcm + 1)
		ans = r - l
		ans = ans - (p - q)
		print(ans)


from collections import defaultdict
from math import gcd
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	a1 = min(a, b)
	b1 = max(a, b)
	arr = []
	for queries in range(q):
		(l, r) = map(int, input().split())
		ans = r - l + 1
		lcm = a * b // gcd(a, b)
		l1 = l - l % lcm
		if l1 + b1 > l:
			ans -= l1 + b1 - l
		if ans <= 0:
			arr.append(0)
			continue
		r1 = r - r % lcm
		if r1 == l1:
			arr.append(ans)
			continue
		ans -= min(b1, r - r1 + 1)
		l2 = l + lcm - l % lcm
		terms = (r1 - l2) // lcm
		ans -= terms * b1
		arr.append(ans)
	print(*arr, sep=' ')


import math
import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def findv(lcm, l, r, b):
	p = max(min(r, b), l)
	s = r - p + 1
	x1 = p // lcm
	x2 = r // lcm
	if x1 * lcm + b > p:
		s -= b - p % lcm
	x1 += 1
	if x2 * lcm + b <= r:
		s -= b * (x2 - x1 + 1)
	else:
		s -= b * (x2 - x1) + r % lcm + 1
	return s
cases = int(input())
for t in range(cases):
	(a, b, q) = list(map(int, input().split()))
	(a, b) = (min(a, b), max(a, b))
	lcm = a * b // math.gcd(a, b)
	out = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		if b > r:
			out.append(0)
		else:
			out.append(findv(lcm, l, r, b))
	print(*out)


import math
t = int(input())
while t != 0:
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	arr = [0] * lcm
	for i in range(lcm):
		if (i + 1) % a % b != (i + 1) % b % a:
			arr[i] = 1
	prefixsum = [arr[0]]
	for i in range(1, len(arr)):
		prefixsum.append(arr[i] + prefixsum[-1])
	sum1 = prefixsum[-1]
	list1 = []
	for i in range(q):
		(l, r) = map(int, input().split())
		l -= 2
		r -= 1
		div = r // lcm
		mod = r % lcm
		t1 = div * sum1 + prefixsum[mod]
		div2 = l // lcm
		mod2 = l % lcm
		t2 = div2 * sum1 + prefixsum[mod2]
		list1.append(t1 - t2)
	print(*list1)
	t -= 1


from sys import stdin, stdout
import heapq as hq
from collections import defaultdict

def lcm(x, y):
	m = max(x, y)
	while m % x > 0 or m % y > 0:
		m += 1
	return m

def truths(inp, m, higher):
	inp += 1
	resu = 0
	resu += inp // m * (m - higher)
	inp -= m * (inp // m)
	resu += max(inp - higher, 0)
	return resu
t = int(input())
for tc in range(t):
	(a, b, q) = list(map(int, stdin.readline().split()))
	m = lcm(a, b)
	higher = max(a, b)
	allEq = False
	if m == higher:
		allEq = True
	for qc in range(q):
		(l, r) = list(map(int, stdin.readline().split()))
		if allEq:
			res = r - l + 1
			res = 0
		else:
			res = truths(r, m, higher) - truths(l - 1, m, higher)
		stdout.write(str(res) + ' ')
	stdout.write('\n')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split(' '))
	if a < b:
		(b, a) = (a, b)
	if a % b == 0 or b % a == 0:
		for j in range(q):
			(l, r) = map(int, input().split(' '))
		print(*(0 for ___ in range(q)))
	else:
		for __ in range(q):
			(l, r) = map(int, input().split(' '))
			if math.gcd(a, b) != 1:
				mu = b // math.gcd(a, b) * a
			else:
				mu = a * b
			ra = max(r % mu - a + 1, 0) + r // mu * (mu - a)
			la = max(l % mu - a + 1, 0) + l // mu * (mu - a)
			if l % a % b != l % b % a:
				print(ra - la + 1, end=' ')
			else:
				print(ra - la, end=' ')
		print()


from math import gcd

def max(x, y):
	if x > y:
		return x
	return y

def calc(p, a, b):
	ans = (p - p % (a * b // gcd(a, b))) // (a * b // gcd(a, b)) * int(max(a, b))
	cur = p - p % (a * b // gcd(a, b))
	ans += int(max(a, b))
	if cur + int(max(a, b)) - 1 > p:
		ans -= cur + int(max(a, b)) - 1 - p
	return ans
t = int(input())
while t > 0:
	t -= 1
	(a, b, q) = input().split()
	a = int(a)
	b = int(b)
	if a > b:
		p = a
		a = b
		b = p
	q = int(q)
	while q > 0:
		q -= 1
		(l, r) = input().split()
		l = int(l)
		r = int(r)
		print(r - l + 1 - (int(calc(r, a, b)) - int(calc(l - 1, a, b))), end=' ')
	print(end='\n')


MI = lambda : map(int, input().split())
II = lambda : int(input())
for _ in range(II()):
	(a, b, q) = MI()
	precalc = [0] * (a * b)
	r = 0
	sums = []
	for i in range(a * b):
		if i % a % b != i % b % a:
			precalc[i] = 1
			r += 1
		sums.append(r)
	temp = sums[-1]
	for query in range(q):
		(l, r) = MI()
		ans = 0
		l -= 1
		ans = r // (a * b) * temp + sums[r % (a * b)] - l // (a * b) * temp - sums[l % (a * b)]
		print(ans, end=' ')
	print('')


from math import *

def _in():
	return [int(x) for x in input().split(' ')]

def main():
	(a, b, q) = _in()
	lc = a * (b // gcd(a, b))
	arr = [0]
	for i in range(1, lc + 1):
		x = int(i % a % b != i % b % a)
		prev = arr[-1]
		arr.append(prev + x)
	for i in range(q):
		(l, r) = _in()
		ans = 0
		p = r // lc
		ans = ans + arr[r - lc * p]
		if l % lc == 0:
			b = l // lc
			ans = ans + (p - b) * arr[-1]
		else:
			b = l // lc
			extra = l - b * lc - 1
			ans = ans + arr[-1] - arr[extra]
			ans = ans + (p - b - 1) * arr[-1]
		print(ans, end=' ')
	print()
tt = 1
tt = int(input())
for _ in range(tt):
	main()


ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
mp = lambda : map(int, input().split())
from math import gcd
for _ in range(int(input())):
	(a, b, q) = mp()
	flag = 0
	if max(a, b) % min(a, b) == 0:
		flag = 1
	f = []
	for i in range(q):
		(l, r) = mp()
		if flag:
			f.append(0)
			continue
		match = 0
		mx = max(a, b)
		p = a * b // gcd(a, b)
		p1 = l // p
		p2 = r // p
		t1 = p * p1
		for j in range(mx):
			if t1 >= l:
				match += 1
			t1 += 1
		t2 = p * p2
		for j in range(mx):
			if t2 <= r:
				match += 1
			t2 += 1
		ans = 0
		match += (p2 - p1 - 1) * mx
		tt = r - l + 1
		ans = tt - match
		f.append(ans)
	print(*f)


import sys
tokens = []
tokens_next = 0
DEBUG = False

def next_str():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = input().split()
		tokens_next = 0
	tokens_next += 1
	return tokens[tokens_next - 1]

def nextInt():
	return int(next_str())

def gcd(a, b):
	while b > 0:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def calcIntersection(interval1, interval2):
	if interval1 > interval2:
		(interval1, interval2) = (interval2, interval1)
	if interval1[1] < interval2[0]:
		return 0
	first = max(interval1[0], interval2[0])
	second = min(interval1[1], interval2[1])
	return second - first + 1

def myRangeLen(r):
	return (r.stop - r.start - 1) // r.step + 1
T = nextInt()
for _ in range(T):
	a = nextInt()
	b = nextInt()
	if a > b:
		(a, b) = (b, a)
	q = nextInt()
	for i in range(q):
		l = nextInt()
		r = nextInt()
		if a == b:
			print('0', end=' ')
			continue
		n = lcm(a, b)
		if DEBUG:
			print('got lcm')
		allRelevantIntervals = range(l - l % n, r + 1, n)
		if DEBUG:
			print('got range obj')
		res = r - l + 1
		if allRelevantIntervals and allRelevantIntervals[0] < l:
			cur = allRelevantIntervals[0]
			res -= calcIntersection((cur, cur + b - 1), (l, r))
			allRelevantIntervals = allRelevantIntervals[1:]
		if DEBUG:
			print('after 1st if')
		if allRelevantIntervals and allRelevantIntervals[-1] + b - 1 > r:
			cur = allRelevantIntervals[-1]
			res -= calcIntersection((cur, cur + b - 1), (l, r))
			allRelevantIntervals = allRelevantIntervals[:-1]
		if DEBUG:
			print('after 2nd if')
		if allRelevantIntervals:
			res -= myRangeLen(allRelevantIntervals) * b
		if DEBUG:
			print('after 3rd if')
		if not DEBUG:
			print(res, end=' ')
	if not DEBUG:
		print()


t = int(input())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for i in range(t):
	(a, b, q) = map(int, input().split())
	z = gcd(a, b)
	target = a * b // z
	pref = [0]
	for j in range(1, target + 1):
		if j % a % b != j % b % a:
			pref.append(pref[-1] + 1)
		else:
			pref.append(pref[-1])
	result = []
	for i in range(q):
		(l, r) = map(int, input().split())
		temp_r = r // target * pref[-1]
		r = r % target
		l -= 1
		temp_l = l // target * pref[-1]
		l = l % target
		temp_r += pref[r]
		temp_l += pref[l]
		result.append(temp_r - temp_l)
	print(*result)


import sys
input = sys.stdin.buffer.readline
t = int(input())
from math import gcd
while t:
	t -= 1
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	ans = [None] * q
	if a > b:
		(a, b) = (b, a)
	if gcd(a, b) == a:
		for _ in range(q):
			(l, r) = map(int, input().split())
		print(*[0] * q)
	else:
		for _ in range(q):
			(l, r) = map(int, input().split())
			d = 0
			last = min(r + 1, ((l - 1) // lcm + 1) * lcm)
			for i in range(l, last):
				if i % a % b == i % b % a:
					d += 1
				else:
					break
			if last == r + 1:
				ans[_] = r - l + 1 - d
				continue
			nl = (l - 1) // lcm
			nr = r // lcm
			nb = nr - nl
			d += max(0, (nb - 1) * b) + min(b, r - nr * lcm + 1)
			ans[_] = r - l + 1 - d
		print(*ans)


def read_int():
	return int(input())

def read_ints():
	return list(map(int, input().split(' ')))

def gcd(x, y):
	return x if y == 0 else gcd(y, x % y)
t = read_int()
for case_num in range(t):
	(a, b, q) = read_ints()
	g = gcd(a, b)
	lcm = a * b // g
	acc = [0 for i in range(lcm)]
	for i in range(1, lcm):
		acc[i] = acc[i - 1]
		if i % a % b != i % b % a:
			acc[i] += 1
	ans = []
	for i in range(q):
		(l, r) = read_ints()
		curr = 0
		n = l // lcm
		x = l % lcm
		m = r // lcm
		y = r % lcm
		if m > n + 1:
			curr += (m - n - 1) * acc[lcm - 1]
		if m > n:
			curr += acc[lcm - 1] - acc[max(0, x - 1)]
			curr += acc[y]
		if m == n:
			curr += acc[y] - acc[max(0, x - 1)]
		ans.append(curr)
	print(' '.join(map(str, ans)))


import math
T = int(input())
for _ in range(T):
	(a, b, q) = [int(v) for v in input().split(' ')]
	lcm = a // math.gcd(a, b) * b
	if b > a:
		(a, b) = (b, a)
	for _ in range(q):
		(l, r) = [int(v) for v in input().split(' ')]
		nl = l // lcm
		nr = r // lcm
		ans = (nr - nl - 1) * (lcm - a)
		ans += max(r % lcm - a + 1, 0)
		ans += lcm - max(l % lcm, a)
		print(ans)


from math import gcd

def lcm(x, y):
	return x * y / gcd(x, y)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	x = int(lcm(a, b))
	for i in range(q):
		count = 0
		(l, r) = map(int, input().split())
		lp = (l // x + 1) * x
		rp = r // x * x
		count += ((rp - lp) // x + 1) * max(a, b)
		if l % x < max(a, b):
			count += max(a, b) - l % x
		if r % x < max(a, b):
			count -= max(a, b) - r % x - 1
		print(int(r - l - count + 1), end=' ')
	print()


T = int(input())
for t in range(T):
	(a, b, q) = [int(i) for i in input().split()]
	queries = []
	for j in range(q):
		(li, ri) = [int(i) for i in input().split()]
		queries.append((li, ri))
	if a == b:
		print(' '.join(['0'] * len(queries)))
		continue
	(a, b) = (min(a, b), max(a, b))
	count = 0
	V = [0]
	for x in range(1, a * b):
		if x % a % b != x % b % a:
			count += 1
		V.append(count)
	results = []
	for query in queries:
		(l, r) = (query[0], query[1])
		ll = l - l % (a * b)
		lr = r - r % (a * b)
		answer = (lr - ll) // (a * b) * count + V[r % (a * b)]
		if ll != l:
			answer -= V[(l - 1) % (a * b)]
		results.append(answer)
	print(' '.join([str(r) for r in results]))


def get_gcd(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a
for test_i in range(int(input())):
	(a, b, q) = map(int, input().split())
	gcd = get_gcd(a, b)
	dx = a // gcd * b
	c = dx - max(a, b)
	ans = []
	for qi in range(q):
		(l, r) = map(int, input().split())
		if c > 0:
			max_x = r // dx
			min_x = (l - 1) // dx + 1
			if max_x < min_x:
				ans.append(min(max(r - max_x * dx + 1 - max(a, b), 0), r - l + 1))
			else:
				ans.append(c * (max_x - min_x) + min(min_x * dx - l, c) + max(r - max_x * dx + 1 - max(a, b), 0))
		else:
			ans.append(0)
	print(*ans)


import math

def get_lcm(i, j):
	gcd = math.gcd(i, j)
	lcm = i * j // gcd
	return lcm

def get_count(x, a, b):
	lcm = get_lcm(a, b)
	same_rem = x // lcm * b
	count = x - same_rem - min(b, x % lcm + 1)
	return count
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		(a, b) = (min(a, b), max(a, b))
		ans.append(get_count(r, a, b) - get_count(l - 1, a, b))
	print(*ans)


from math import gcd
t = int(input())
for __ in range(t):
	(a, b, q) = list(map(int, input().split()))
	if a > b:
		(a, b) = (b, a)
	g = a * b // gcd(a, b)

	def solve(m):
		if m == 0:
			return 1
		(aa, bb) = divmod(m + 1, g)
		ret = aa * b
		ret += min(bb, b)
		return ret

	def br(l, r):
		ret = 0
		for i in range(l, r + 1):
			if i % a % b != i % b % a:
				ret += 1
		return ret
	res = []
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		if b % a == 0:
			res.append(0)
		else:
			res.append(r - l + 1 - (solve(r) - solve(l - 1)))
	print(*res)


from math import gcd

def f(a, b, x):
	x += 1
	p = a * b // gcd(a, b)
	n = x // p
	ans = n * b
	ans += min(b, x - n * p)
	return ans
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	for i in range(q):
		(l, r) = map(int, input().split())
		ans = f(a, b, r) - f(a, b, l - 1)
		ans = r - l + 1 - ans
		print(ans, end=' ')
	print()


from math import gcd
t = int(input())

def fn(a, b, r):
	lcm = a * b // gcd(a, b)
	upperlimit = r // lcm * lcm
	result = upperlimit // lcm * b
	result += min(b, r - upperlimit + 1)
	return result
for _ in range(t):
	(a, b, q) = map(int, input().split())
	res = []
	if a > b:
		(a, b) = (b, a)
	for __ in range(q):
		(l, r) = map(int, input().split())
		ans = fn(a, b, r) - fn(a, b, l - 1)
		total = r - l + 1
		res.append(total - ans)
	print(' '.join(map(str, res)))


import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
t = int(input())
for case in range(t):
	(a, b, q) = map(int, input().split())
	mod_dif = [0] * (a * b)
	for i in range(a * b):
		if i % a % b != i % b % a:
			mod_dif[i] = mod_dif[i - 1] + 1
		else:
			mod_dif[i] = mod_dif[i - 1]
	ans = [0] * q
	for query in range(q):
		(x, y) = map(int, input().split())
		(x_d, x_m) = divmod(x - 1, a * b)
		(y_d, y_m) = divmod(y, a * b)
		ans[query] = (y_d - x_d) * mod_dif[-1] + mod_dif[y_m] - mod_dif[x_m]
	print(*ans)


import math

def find(n, terms, a, b):
	if n <= 0:
		return 0
	x = n // terms
	good = terms - max(a, b)
	total = good * x
	n %= terms
	total += min(good, n)
	return total

def solve(a, b, q, ans):
	g = math.gcd(max(a, b), min(a, b))
	x = max(a, b)
	terms = a * b // g
	arr = []
	for i in range(q):
		(l, r) = map(int, input().split())
		l -= max(a, b) - 1
		r -= max(a, b) - 1
		arr.append(find(r, terms, a, b) - find(l - 1, terms, a, b))
	ans.append(arr)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		(a, b, q) = map(int, input().split())
		solve(a, b, q, ans)
	for i in ans:
		for j in i:
			print(j, end=' ')
		print()
main()


import math
T = int(input())
ans = []
for i in range(T):
	(a, b, q) = map(int, input().split())
	d = []
	mod = [0 for i in range(a * b)]
	for j in range(a * b):
		if j % a % b != j % b % a:
			mod[j] += 1
	for j in range(1, a * b):
		mod[j] += mod[j - 1]

	def count(X):
		(u, v) = divmod(X, a * b)
		return u * mod[-1] + mod[v]
	for x in range(q):
		(l, r) = map(int, input().split())
		d.append(count(r) - count(l - 1))
	ans.append(d)
for some in ans:
	print(*some)


from math import gcd

def getCnt(a, b, x):
	l = 0
	r = 10 ** 20
	while r - l > 1:
		mid = (l + r) // 2
		if mid * a * b <= x:
			l = mid
		else:
			r = mid
	cnt = l * b
	RBorder = min(x, (l * a + 1) * b - 1)
	cnt += RBorder - l * a * b + 1
	return cnt

def solve():
	(a, b, q) = map(int, input().split())
	if a > b:
		c = a
		a = b
		b = c
	a //= gcd(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		print(r - l + 1 - (getCnt(a, b, r) - getCnt(a, b, l - 1)), end=' ')
	print()
t = int(input())
for i in range(t):
	solve()


t = int(input())

def compute_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def compute_lcm(x, y):
	lcm = x * y // compute_gcd(x, y)
	return lcm

def computeResult(a, b, lcm, x, y):
	first = a // lcm
	last = b // lcm
	if first == last:
		if a > first * lcm + x:
			return b - a + 1
		return b - a + 1 - (min(b, first * lcm + x - 1) - (max(a, first * lcm) - 1))
	return b - a + 1 - ((last - first - 1) * x + (min(last * lcm + x - 1, b) - (last * lcm - 1)) + max(first * lcm + x - 1, a - 1) - a + 1)
for i in range(t):
	(x, y, q) = [int(u) for u in input().split(' ')]
	lcm = compute_lcm(x, y)
	if x < y:
		qw = x
		x = y
		y = qw
	results = ''
	for j in range(q):
		(a, b) = [int(u) for u in input().split(' ')]
		result = computeResult(a, b, lcm, x, y)
		results += str(result) + ' '
	print(results)


import sys
input = sys.stdin.readline
from math import gcd
test = int(input())
for t in range(test):
	(a, b, q) = map(int, input().split())
	m = a * b // gcd(a, b)
	s = 0
	narr = [0] * m
	for j in range(m):
		if j % a % b != j % b % a:
			s += 1
		narr[j] = s
	for i in range(q):
		(l, r) = map(int, input().split())
		cnt = 0
		cnt = r // m * s + narr[r % m] - ((l - 1) // m * s + narr[(l - 1) % m])
		print(cnt, end=' ')
	print()


from math import gcd
finaans = []
for t in range(int(input())):
	(a, b, q) = [int(x) for x in input().split()]
	l = a * b // gcd(a, b)
	ans = []
	for k in range(q):
		(q1, q2) = [int(x) for x in input().split()]
		p = (q1 - 1) // l
		q = q2 // l
		s1 = q2 - (q * max(a, b) + min(max(a, b), q2 % l + 1))
		s2 = q1 - 1 - (p * max(a, b) + min(max(a, b), (q1 - 1) % l + 1))
		ans.append(s1 - s2)
	finaans.append(ans)
for it in finaans:
	print(*it)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * (b // gcd(a, b))
for _ in range(int(input())):
	(a, b, s) = list(map(int, input().split()))
	p = lcm(a, b)
	q = max(a, b)
	lis = []
	for _ in range(s):
		(l, r) = map(int, input().split())
		a1 = l // p
		a2 = r // p
		b1 = l % p
		b2 = r % p
		ans = 0
		if a1 == a2:
			if b1 < q:
				if b2 < q:
					ans += b2 - b1 + 1
				else:
					ans += q - b1
		else:
			ans += (a2 - a1 - 1) * q
			if b1 < q:
				ans += q - b1
			if b2 >= q:
				ans += q
			if b2 < q:
				ans += b2 + 1
		lis.append(r - l + 1 - ans)
	print(*lis)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	przyg = [0] * (a * b)
	for i in range(a * b):
		if i % a % b != i % b % a:
			przyg[i] = 1
	cyk = sum(przyg)
	pref = [0] * (a * b)
	pref[0] = przyg[0]
	for i in range(1, a * b):
		pref[i] = pref[i - 1] + przyg[i]
	for query in range(q):
		(l, r) = map(int, input().split())
		ll = l % (a * b)
		rr = r % (a * b)
		wynik = cyk * (r // (a * b) - l // (a * b))
		wynik += pref[rr]
		if ll > 0:
			wynik -= pref[ll - 1]
		if query < q - 1:
			print(wynik, end=' ')
		else:
			print(wynik)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)
t = int(input())
for k in range(t):
	(a, b, q) = map(int, input().split())
	if b > a:
		temp = b
		b = a
		a = temp
	least_common_multiple = lcm(a, b)
	for i in range(q):
		(start, end) = map(int, input().split())
		total = end - start + 1
		starting = start // least_common_multiple
		ending = end // least_common_multiple
		answer = 0
		if starting * least_common_multiple + a >= start:
			answer += starting * least_common_multiple + a - start
		if ending * least_common_multiple + a <= end:
			answer += a
		else:
			answer += end - ending * least_common_multiple + 1
		answer += (ending - starting - 1) * a
		if i != q - 1:
			print(max(0, total - answer), end=' ')
		else:
			print(max(0, total - answer))


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	g = gcd(a, b)
	return a * b // g
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	tmp = lcm(a, b)
	mi = max(a, b)
	for i in range(q):
		ans = 0
		(l, r) = map(int, input().split())
		l = max(l, a, b)
		if l > r:
			print(0, end=' ')
			continue
		ans = (r // tmp - l // tmp) * mi
		ans = ans - max(0, mi - r % tmp - 1) + max(0, mi - l % tmp)
		print(max(r - l + 1 - ans, 0), end=' ')
	print()


from math import gcd

def count(z, c, d):
	m = z // c
	return m * d + min(z - c * m + 1, d)
for _ in range(int(input())):
	(a, b, q) = [int(i) for i in input().split()]
	lcm = a * b // gcd(a, b)
	for qry in range(q):
		(x, y) = [int(i) for i in input().split()]
		k = count(y, lcm, max(a, b)) - count(x - 1, lcm, max(a, b))
		print(y - x + 1 - k, end=' ')
	print()


import math
for _ in range(int(input())):
	(a, b, q) = [int(i) for i in input().split()]
	(a, b) = (min(a, b), max(a, b))
	lcm = a * b // math.gcd(a, b)
	s = lcm - b
	for Q in range(q):
		(l, r) = [int(i) for i in input().split()]
		if r < b or b % a == 0:
			print(0, end=' ')
		else:
			ans = s * (r // lcm - l // lcm)
			r = r % lcm
			l = l % lcm
			ans += max(0, r + 1 - b) - max(0, l - b)
			print(ans, end=' ')
	print()


test = False
from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	mult = lcm(a, b)
	gr = max(a, b)
	for __ in range(q):
		(l, r) = map(int, input().split())
		good = r - l + 1
		low_bound = int(l // mult * mult)
		high_bound = int(r // mult * mult)
		ostensible = ((high_bound - low_bound) // mult + 1) * gr
		if test:
			print()
			print('l: ', l, 'r: ', r, 'a: ', a, 'b: ', b, 'low_bound: ', low_bound, 'high_bound: ', high_bound, 'ostensible: ', ostensible)
			print('good: ', good)
		good -= ostensible
		before = max(0, min(gr, l - low_bound))
		if test:
			print('good: ', good)
		good += before
		if test:
			print('before: ', before)
			print('good: ', good)
		after = max(0, min(gr, high_bound + gr - 1 - r))
		good += after
		if test:
			print('after: ', after)
			print('good: ', good)
		print(good, end=' ')
	print()


def kyu(pt, n):
	return pt // n * f[n - 1] + f[pt % n]
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	pr = a * b
	f = [0] * pr
	for i in range(pr):
		f[i] = f[i - 1]
		if i % a % b != i % b % a:
			f[i] += 1
	while q:
		(l, r) = map(int, input().split())
		print(kyu(r, pr) - kyu(l - 1, pr), end=' ')
		q -= 1
	print()


p = [0] * 40002

def count(k):
	return k // (a * b) * p[a * b - 1] + p[k % (a * b)]
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	p[0] = 0
	for j in range(1, a * b):
		a1 = j % a % b
		a2 = j % b % a
		p[j] = p[j - 1]
		if a1 != a2:
			p[j] += 1
	for i in range(0, q):
		(l, r) = map(int, input().split())
		ans = count(r) - count(l - 1)
		print(ans, end=' ')
	print()


def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a % b == 0:
		return b
	else:
		return gcd(b, a % b)
for _ in range(int(input())):
	(a, b, q) = list(map(int, input().split()))
	if a < b:
		x = b
		y = a
	else:
		x = a
		y = b
	anslcm = lcm(x, y)
	mul = anslcm - x
	ans = []
	for w in range(q):
		(l, r) = list(map(int, input().split()))
		if a == b:
			ans.append(0)
		else:
			startdiv = l // anslcm
			endiv = r // anslcm
			temp = mul * (endiv - startdiv)
			virstart = startdiv * anslcm
			virend = endiv * anslcm
			tempo = l % anslcm
			if tempo >= x:
				tem = tempo - x
				temp -= tem
			tempo = r % anslcm
			if tempo >= x:
				tem = tempo - x + 1
				temp += tem
			ans.append(temp)
	for q in ans:
		print(q, end=' ')
	print()


import sys
input = sys.stdin.readline
from math import gcd

def ff(x, a, b, c):
	k = x // c
	return (k - 1) * b + min(x - k * c + 1, b)
(t,) = map(int, input().split())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	R = []
	(a, b) = (min(a, b), max(a, b))
	c = a * b // gcd(a, b)
	for _ in range(q):
		(l, r) = map(int, input().split())
		k1 = ff(l - 1, a, b, c)
		k2 = ff(r, a, b, c)
		k = k2 - k1
		R.append(r - l + 1 - k)
	print(*R)


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def check(f, t, x):
	ans = x // (f + t) * f
	if x % (f + t) > t:
		ans += x % (f + t) - t + 1
	return x // (f + t) * f + max(x % (f + t) - t, 0)

def out():
	(a, b, q) = map(int, input().split())
	am = max(a, b)
	bm = min(a, b)
	Time = am * (bm // gcd(a, b))
	f = Time - am
	t = am
	for i in range(q):
		(l, r) = map(int, input().split())
		z = int(check(f, t, r + 1) - check(f, t, l))
		print(z, end=' ')
	print()
ans = 0
for i in range(int(input())):
	out()


t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b
	table = [0] * lcm
	for i in range(1, lcm):
		if i % a % b != i % b % a:
			table[i] = table[i - 1] + 1
		else:
			table[i] = table[i - 1]
	answers = []
	for _ in range(q):
		(x, y) = map(int, input().split())
		lcmsInside = y // lcm - x // lcm
		t = lcmsInside * table[-1]
		x = x % lcm
		y = y % lcm
		if x != 0:
			t -= table[x - 1]
		t += table[y]
		answers.append(t)
	print(*answers)


import math

def check(a, b, k):
	g = math.gcd(a, b)
	l = a * b // g
	(a, b) = (max(a, b), min(a, b))
	if g == b:
		return 0
	else:
		ans = k // l * (l - a)
		k %= l
		ans += max(k - a + 1, 0)
	return ans
for i in range(int(input())):
	(a, b, q) = map(int, input().split())
	ans = []
	for i in range(q):
		(x, y) = map(int, input().split())
		print(check(a, b, y) - check(a, b, x - 1), end=' ')
	print()


from math import gcd
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	x = a * b // gcd(a, b)
	e = max(a, b)
	k = []
	for i in range(q):
		(l, r) = map(int, input().split())
		ans = r - l + 1
		d = r // x - (l - 1) // x
		ans -= d * e
		d = (l - 1) // x
		d = d * x
		c = l - d
		if e >= c:
			ans -= e - c
		d = r // x
		d = d * x - 1
		c = r - d
		if c <= e:
			ans += e - c
		k.append(ans)
	print(*k)


import math

def solve(r, lcm, b):
	total = r
	div = int(r // lcm)
	prod = div * lcm
	if div != 0:
		total -= (div - 1) * b
		total -= min(b, r - prod + 1)
	total -= min(r, b - 1)
	return total
t = int(input())
while t:
	t = t - 1
	(a, b, q) = map(int, input().split())
	for w in range(q):
		(l, r) = map(int, input().split())
		(a, b) = (min(a, b), max(a, b))
		lcm = int(a * b // math.gcd(a, b))
		if r < b or lcm == b:
			print(0, end=' ')
			continue
		ans = solve(r, lcm, b)
		if l > 1:
			ans -= solve(l - 1, lcm, b)
		print(ans, end=' ')
	print()


t = int(input())
pref = [0]

def solve(x, L):
	k = x // L
	return k * pref[L - 1] + pref[x % L]
for _ in range(t):
	(a, b, q) = map(int, input().split())
	pref = list()
	pref = [0]
	for i in range(1, a * b):
		pref.append(pref[i - 1])
		if i % a % b != i % b % a:
			pref[i] += 1
	for __ in range(q):
		(l, r) = map(int, input().split())
		print(solve(r, a * b) - solve(l - 1, a * b), end=' ')
	print('')


from itertools import accumulate
t = int(input())
buf = []
for _ in range(t):
	(a, b, q) = map(int, input().split())
	ab = a * b
	memo = [k % a % b != k % b % a for k in range(ab)]
	loop = sum(memo)
	acc = list(accumulate(memo))
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		(dr, mr) = divmod(r, ab)
		(dl, ml) = divmod(l - 1, ab)
		ans.append(loop * (dr - dl) + acc[mr] - acc[ml])
	buf.append(' '.join(map(str, ans)))
print('\n'.join(map(str, buf)))


import sys, math
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
import collections as col
import math

def solve():
	(A, B, Q) = getInts()
	ans = []
	prefix = []
	curr_num = 0
	for x in range(A * B):
		if x % A % B != x % B % A:
			curr_num += 1
		prefix.append(curr_num)
	for q in range(Q):
		(L, R) = getInts()
		whole = (R - L + 1) // (A * B)
		extra = (R - L + 1) % (A * B)
		add_on = 0
		R %= A * B
		L %= A * B
		if extra:
			if R >= L:
				add_on = prefix[R] - (prefix[L - 1] if L > 0 else 0)
			else:
				add_on = curr_num - prefix[L - 1] + prefix[R]
		ans.append(curr_num * whole + add_on)
	print(*ans)
	return
for t in range(getInt()):
	solve()


def solve():
	(A, B, Q) = map(int, input().split())
	queries = [list(map(int, input().split())) for _ in range(Q)]
	ans = []
	CM = A * B
	CNT = [0] * (CM + 1)
	for i in range(1, CM + 1):
		if i % A % B != i % B % A:
			CNT[i] += 1
	for i in range(1, CM + 1):
		CNT[i] += CNT[i - 1]
	for (l, r) in queries:
		ACC_R = r // CM * CNT[-1] + CNT[r % CM]
		ACC_L = (l - 1) // CM * CNT[-1] + CNT[(l - 1) % CM]
		ans.append(str(ACC_R - ACC_L))
	print(' '.join(ans))
T = int(input())
for _ in range(T):
	solve()


import sys

def gcd(a: int, b: int) -> int:
	if a < b:
		(a, b) = (b, a)
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def main():
	t = int(input())
	for _ in range(t):
		(a, b, q) = map(int, sys.stdin.readline().split())
		modVal = None
		modVal = a * b // gcd(a, b)
		checkList = [0] * modVal
		loopSum = 0
		for i in range(modVal):
			if i % a % b != i % b % a:
				loopSum += 1
			checkList[i] = loopSum
		for i in range(q):
			(l, r) = map(int, sys.stdin.readline().split())

			def calcSum(n: int) -> int:
				return loopSum * (n // modVal) + checkList[n % modVal]
			ans = calcSum(r) - calcSum(l - 1)
			print(ans, end=' ')
		print('')
	return
main()


def gcd(a, b):
	if a % b == 0:
		return b
	return int(gcd(b, a % b))

def lcm(a, b):
	return int(a * b / gcd(a, b))

def cnt(l, a, b):
	t1 = lcm(a, b)
	n = l // t1 * max(a, b) + min(l % t1 + 1, max(a, b))
	return int(n)
t = int(input())
for i in range(0, t):
	(a, b, q) = map(int, input().split())
	for j in range(0, q):
		(l, r) = map(int, input().split())
		ans = r - l + 1 - cnt(r, a, b) + cnt(l - 1, a, b)
		print(ans, end=' ')
	print('')


from math import inf

def gcd(a, b):
	if not b:
		return a
	return gcd(b, a % b)
t = int(input())
for qq in range(t):
	(a, b, q) = [int(i) for i in input().split()]
	if a < b:
		(a, b) = (b, a)
	g = gcd(a, b)
	e = b // g
	for h in range(q):
		(l, r) = [int(i) for i in input().split()]
		if a % b:
			s = r - l + 1
			k = l // a
			j = r // a
			if not k % e:
				s -= min((k + 1) * a - l, s)
				k += 1
			if not j % e:
				s -= min(r - j * a + 1, s)
				j -= 1
			if j == k - 2:
				print(0, end=' ')
				continue
			j -= j % e
			k += e - k % e
			s -= ((j - k) // e + 1) * a
			print(s, end=' ')
		else:
			print(0, end=' ')
	print('')


import math
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	m = max(a, b)
	z = []
	for i in range(q):
		c = 0
		(l, r) = map(int, input().split())
		a1 = l // lcm
		a2 = (r + 1) // lcm
		b1 = l % lcm
		b2 = (r + 1) % lcm
		ans1 = a1 * (lcm - m)
		ans2 = a2 * (lcm - m)
		ans1 += max(0, b1 - m)
		ans2 += max(0, b2 - m)
		z.append(ans2 - ans1)
	print(*z)


line = input()
t = int(line)
for _ in range(t):
	line = input()
	(a, b, q) = (int(i) for i in line.split(' '))
	memo = [0] * (a * b)
	for i in range(1, a * b):
		if i % a % b != i % b % a:
			memo[i] = memo[i - 1] + 1
		else:
			memo[i] = memo[i - 1]
	for i in range(q):
		line = input()
		(l, r) = (int(i) for i in line.split(' '))
		l -= 1
		res = r // (a * b) * memo[-1] + memo[r % (a * b)]
		res -= l // (a * b) * memo[-1] + memo[l % (a * b)]
		print(res)


def LCM(a, b):
	lar = max(a, b)
	small = min(a, b)
	i = lar
	while True:
		if i % small == 0:
			return i
		i += lar

def solve():
	(a, b, q) = [int(x) for x in input().split()]
	lcm = LCM(a, b)
	maxx = max(a, b)
	for i in range(q):
		(l, r) = [int(x) for x in input().split()]
		count = maxx - 1
		l -= 1
		left = l // lcm * lcm
		lft = l // lcm
		lcount = (lft - 1) * maxx + count
		if left + maxx - 1 <= l:
			lcount += maxx
		else:
			lcount += l - left + 1
		right = r // lcm * lcm
		rgt = r // lcm
		rcount = (rgt - 1) * maxx + count
		if right + maxx - 1 <= r:
			rcount += maxx
		else:
			rcount += r - right + 1
		print(r - rcount - l + lcount, end=' ')
t = int(input())
for i in range(t):
	solve()


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect

def func(x):
	return s[x % (a * b)] + x // (a * b) * s[a * b - 1]
for _ in range(I()):
	(a, b, q) = R()
	s = [0] * (a * b)
	for i in range(1, a * b):
		s[i] = s[i - 1]
		if i % a % b != i % b % a:
			s[i] += 1
	for i in range(q):
		(l, r) = R()
		print(func(r) - func(l - 1), end=' ')
	print()


import math

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def func(n, a, b):
	mx = max(a, b)
	lc = lcm(a, b)
	c = n // lc
	g = n % lc
	if g >= mx:
		res = n - (c + 1) * mx + 1
	else:
		res = c * lc - c * mx
	return res
t = int(input())
res = []
for _ in range(t):
	(a, b, q) = map(int, input().split())
	temp = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		if lcm(a, b) == max(a, b):
			temp.append(str(0))
		else:
			temp.append(str(func(r, a, b) - func(l - 1, a, b)))
	res.append(' '.join(temp))
for r in res:
	print(r)


from math import gcd

def ans(numa, numb, gran):
	return gran // numa * numb + min(gran % numa, numb)
for t in range(int(input())):
	(a, b, q) = map(int, input().split())
	num1 = gcd(a, b)
	num2 = a * b // num1
	num3 = max(a, b)
	if a % b == 0 or b % a == 0:
		for i in range(q):
			input()
		print('0 ' * q)
	else:
		for i in range(q):
			(l, r) = map(int, input().split())
			arra = ans(num2, num3, r + 1) - ans(num2, num3, l)
			print(r - l - arra + 1, end=' ')
		print()


from math import gcd

def solve(rf):
	r1 = rf // b
	ans = (r1 - (r1 - 1) // a) * b
	if r1 % a:
		ans += rf - r1 * b
	return ans

def input_and_solve(_):
	(li, r) = map(int, input().split())
	return solve(r + 1) - solve(li)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	a //= gcd(a, b)
	print(*map(input_and_solve, range(q)))


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	d = [0]
	for i in range(1, a * b + 1):
		if i % a % b != i % b % a:
			d.append(d[-1] + 1)
		else:
			d.append(d[-1])
	e = []
	for i in range(q):
		(l, r1) = map(int, input().split())
		k = r1 // (a * b)
		r = r1 % (a * b)
		ans = d[-1] * k
		ans += d[r]
		k = l // (a * b)
		r = l % (a * b)
		ans -= d[-1] * k
		ans -= d[r]
		if l % a % b != l % b % a:
			ans += 1
		e.append(ans)
	print(*e)


import math

def lcm(a, b):
	kk = math.gcd(a, b)
	ll = a * b // kk
	return ll
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	aa = lcm(a, b)
	b = max(a, b)
	for i in range(q):
		tmp = aa
		ans = 0
		(l, r) = map(int, input().split())
		ans += r // tmp * b
		ans -= l // tmp * b
		if r % tmp < b:
			ans -= b
			ans += r % tmp + 1
		if l % tmp < b:
			ans -= l % tmp
			ans += b
		print(r - l + 1 - ans, end=' ')
	print()


def gcd(a, b):
	if a % b == 0:
		return b
	return gcd(b, a % b)

def calc_lcm(a, b):
	g = gcd(a, b)
	return g * (a // g) * (b // g)

def same(l, r, a, b, lcm):
	left = a * (l // lcm) + min(a, l % lcm)
	right = a * ((r + 1) // lcm) + min(a, (r + 1) % lcm)
	return right - left
test_cases = int(input())
for test_case in range(test_cases):
	(a, b, q) = [int(x) for x in input().split()]
	(a, b) = (max(a, b), min(a, b))
	lcm = calc_lcm(a, b)
	ans = []
	for qr in range(q):
		(l, r) = [int(inp) for inp in input().split()]
		ans.append(r - l + 1 - same(l, r, a, b, lcm))
	print(*ans)


from math import gcd

def lcm(x, y):
	return x * y // gcd(x, y)

class Cnt:

	def __init__(self, x, y):
		self.n = max(x, y)
		self.m = lcm(x, y)

	def v(self, a):
		(p, q) = (a % self.m, a // self.m)
		return a - min(p, self.n) - q * self.n
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	cnt = Cnt(a, b)
	res = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		res.append(cnt.v(r + 1) - cnt.v(l))
	print(*res)


import math
for nt in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		if b % a == 0 or a % b == 0:
			print(0, end=' ')
			continue
		ans = r - l + 1
		m = max(a, b)
		x = l // lcm + 1
		y = r // lcm
		ans -= max(0, y - x + 1) * m
		i = l
		while i % a % b == i % b % a and i <= r:
			ans -= 1
			i += 1
		j = r + 1
		if x <= y and j % lcm != 0:
			while j % a % b == j % b % a:
				ans += 1
				j += 1
		print(ans, end=' ')
	print()


import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))
import math
from itertools import accumulate
for _ in range(int(input())):
	(a, b, q) = ilele()
	x = math.gcd(a, b)
	y = a * b // x
	A = []
	for i in range(1, y + 1):
		d = i % a % b
		e = i % b % a
		if d == e:
			A.append(0)
		else:
			A.append(1)
	T = [0] + list(accumulate(A))
	t = T[-1]
	for i in range(q):
		(xx, yy) = ilele()
		Ans = 0
		if yy <= y:
			Ans = T[yy] - T[xx - 1]
		else:
			z = yy // y
			zz = yy % y
			Ans += z * t
			Ans += T[zz]
			Ans2 = 0
			z = (xx - 1) // y
			zz = (xx - 1) % y
			Ans2 += z * t
			Ans2 += T[zz]
			Ans -= Ans2
		print(Ans, end=' ')
	print()


p = [0] * 40043

def build(a, b):
	length = a * b
	p[0] = 0
	for i in range(1, length + 1):
		p[i] = p[i - 1]
		if i % a % b != i % b % a:
			p[i] += 1

def query(l, length):
	cnt = l // length
	rem = l % length
	return p[length] * cnt + p[rem]

def query2(l, r, length):
	return query(r, length) - query(l - 1, length)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	build(a, b)
	length = a * b
	for j in range(q):
		(l, r) = map(int, input().split())
		print(query2(l, r, length))


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : [int(x) for x in rr().split()]
from math import gcd
from sys import stdin, stdout

def sol():
	(a, b, q) = rrm()
	x = a * b
	lis = [0 for i in range(x + 1)]
	tmp = 0
	for i in range(x + 1):
		if i % a % b != i % b % a:
			tmp += 1
		lis[i] = tmp
	res = 0
	for i in range(q):
		res = 0
		(l, r) = rrm()
		res += lis[x] * (r // x) + lis[r % x]
		if l != 0:
			l -= 1
			res -= lis[x] * (l // x) + lis[l % x]
		stdout.write(str(res) + ' ')
	stdout.write('\n')
T = rri()
for _ in range(T):
	sol()


def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	x = lcm(a, b)
	for i in range(q):
		count = 0
		(l, r) = map(int, input().split())
		lp = (l // x + 1) * x
		rp = r // x * x
		count += ((rp - lp) // x + 1) * max(a, b)
		if l % x < max(a, b):
			count += max(a, b) - l % x
		if r % x < max(a, b):
			count -= max(a, b) - r % x - 1
		print(int(r - l - count + 1), end=' ')
	print()


import math

def hcm(a, b):
	return a * b // math.gcd(a, b)

def hey(x, b, l, r):
	g = 0
	for i in range(b):
		if l <= x + i <= r:
			g += 1
	return g

def find_ans(a, b, l, r):
	h = hcm(a, b)
	if a > b:
		(a, b) = (b, a)
	if b % a == 0 or r < b:
		return 0
	ans = r - l + 1
	ans = ans - max(b - l, 0)
	if b >= l:
		l = b
	if r // h - l // h <= 2:
		i = max(l // h - 1, 1)
		while True:
			k = h * i
			for j in range(b):
				if l <= k + j <= r:
					ans -= 1
				elif k + j > r:
					return ans
			i += 1
	ans -= max((r // h - l // h - 1) * b, 0)
	ans = ans - (hey(l // h * h, b, l, r) + hey(r // h * h, b, l, r))
	return ans
for _ in range(int(input())):
	(a, b, t) = map(int, input().split())
	for ll in range(t):
		(l, r) = map(int, input().split())
		print(find_ans(a, b, l, r), end=' ')
	print()


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	g = gcd(a, b)
	return a * b // g
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	an = []
	c = lcm(a, b)
	mi = max(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		l = max(l, a, b)
		if l > r:
			an.append(0)
			continue
		x = l // c
		y = r // c
		ans = (y - x) * mi + max(0, mi - l % c) - max(0, mi - r % c - 1)
		temp = max(r - l - ans + 1, 0)
		an.append(temp)
	print(*an)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for T in range(int(input())):
	(a, b, q) = [int(s) for s in input().split()]
	l = a * b
	s = list(range(1, l + 1))
	cp = 0
	for (i, x) in enumerate(s):
		if x % a % b != x % b % a:
			cp += 1
		s[i] = cp

	def count(r):
		rt = r // l * cp
		if r % l:
			rt += s[r % l - 1]
		return rt
	ans = []
	for _ in range(q):
		(r1, r2) = [int(s) for s in input().split()]
		ans.append(count(r2) - count(r1 - 1))
	print(' '.join((str(num) for num in ans)))


import math

def findv(lcm, l, r, b):
	p = max(min(r, b), l)
	s = r - p + 1
	x1 = p // lcm
	x2 = r // lcm
	x1s = p % lcm
	if x1 * lcm + b > p:
		s -= b - x1s
	x1 += 1
	if x2 * lcm + b <= r:
		s -= b * (x2 - x1 + 1)
	else:
		s -= b * (x2 - x1) + r % lcm + 1
	return s
cases = int(input())
for t in range(cases):
	(a, b, q) = list(map(int, input().split()))
	(a, b) = (min(a, b), max(a, b))
	lcm = a * b // math.gcd(a, b)
	out = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		if b > r:
			out.append(0)
		else:
			out.append(findv(lcm, l, r, b))
	print(*out)


import sys
import math
from collections import defaultdict

def solve(t):
	(a, b, q) = map(int, input().split())
	x = min(a, b)
	y = max(a, b)
	(a, b) = (x, y)
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split())
		low = max(l, b)
		high = r
		res = high - low + 1
		lcm = a * b // math.gcd(a, b)
		res -= (high // lcm - (low - 1) // lcm) * b
		start = low - (low - 1) // lcm * lcm
		end = high - high // lcm * lcm + 1
		if start < b:
			res -= b - start
		if end < b:
			res += b - end
		ans.append(str(res))
	print(' '.join(ans))
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


from math import ceil, gcd

def brute(a, b, n):
	cnt = 0
	for x in range(1, n):
		if x % a % b != x % b % a:
			cnt += 1
	return cnt

def solve(a, b, n):
	l = a * b // gcd(a, b)
	return n - n // l * b + max(0, b - n % l)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	arr = []
	for i in range(q):
		(l, r) = map(int, input().split())
		ans = solve(a, b, r + 1) - solve(a, b, l)
		arr.append(ans)
	print(*arr)


def lcd(a, b):
	if b == 0:
		return a
	else:
		return lcd(b, a % b)
t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	(a, b, q) = [int(i) for i in input().split()]
	if a > b:
		(a, b) = (b, a)
	d = b % a
	lcd_res = lcd(d, a)
	(a, d) = (a // lcd_res, d // lcd_res)
	res = [0] * q
	for j in range(q):
		cnt_ok = 0
		(l_j, r_j) = [int(i) for i in input().split()]
		if d == 0:
			continue
		(start1, start2) = (l_j // b, l_j % b)
		(end1, end2) = (r_j // b, r_j % b)
		if start1 == end1:
			if start1 % a == 0:
				cnt_ok += end2 - start2 + 1
		elif start1 == end1 - 1:
			if start1 % a == 0:
				cnt_ok += b - start2
			if end1 % a == 0:
				cnt_ok += end2 + 1
		else:
			if start1 % a == 0:
				cnt_ok += b - start2
			if end1 % a == 0:
				cnt_ok += end2 + 1
			cnt_ok += ((end1 - 1) // a - start1 // a) * b
		res[j] = r_j - l_j + 1 - cnt_ok
	print(' '.join((str(j) for j in res)))


from math import gcd
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	ans = [0] * q
	lcm = a * b // gcd(a, b)
	z = min(a, b) + abs(a - b)
	for i in range(q):
		(l, r) = map(int, input().split())
		s = 0
		x = l // lcm
		y = r // lcm
		if x * lcm + z - 1 >= l:
			ans[i] += x * lcm + z - l
		ans[i] += (y - x - 1) * z
		if y * lcm + z - 1 <= r:
			ans[i] += z
		else:
			ans[i] += z - (y * lcm + z - 1 - r)
		ans[i] = abs(l - r) + 1 - ans[i]
	print(*ans)


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	t = a * b
	pre = [0] * (t + 3)
	for i in range(1, t + 1):
		pre[i] = pre[i - 1]
		if i % a % b != i % b % a:
			pre[i] += 1
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		cntl = (l - 1) // t
		rel = (l - 1) % t
		cntr = r // t
		rer = r % t
		temp = 0
		temp += pre[t] * cntr + pre[rer]
		temp -= pre[t] * cntl + pre[rel]
		ans.append(temp)
	print(*ans)


def compute_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def compute_lcm(x, y):
	lcm = x * y // compute_gcd(x, y)
	return lcm
for t in range(int(input())):
	(a, b, q) = map(int, input().split())
	mx = max(a, b)
	lcm = compute_lcm(a, b)
	a = []
	for i in range(q):
		(x, y) = map(int, input().split())
		wran = y - x + 1
		st = x // lcm
		fn = y // lcm
		ans = 0
		if st * lcm <= x <= y < st * lcm + mx or lcm == mx:
			a.append('0')
			continue
		if st * lcm <= x <= st * lcm + mx:
			ans += st * lcm + mx - x
		st += 1
		if fn * lcm <= y < fn * lcm + mx:
			ans += y - fn * lcm + 1
			fn -= 1
		ans += (fn - st + 1) * mx
		a.append(str(wran - ans))
	print(' '.join(a))


for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	prefix = [0] * (a * b + 2)
	v = []
	for i in range(1, a * b):
		if i % a % b != i % b % a:
			v.append(i)
			prefix[i] = prefix[i - 1] + 1
		else:
			prefix[i] = prefix[i - 1]
	for _ in range(q):
		(l, r) = map(int, input().split())
		l_grp = l // (a * b)
		r_grp = r // (a * b)
		ans = (r_grp - l_grp + 1) * prefix[a * b - 1]
		ans -= prefix[a * b - 1] - prefix[r % (a * b)]
		if l % (a * b) != 0:
			ans -= prefix[l % (a * b) - 1]
		print(ans, end=' ')
	print()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math

def gift():
	for _ in range(t):
		(a, b, q) = map(int, input().split())
		lcm = a * b // math.gcd(a, b)
		maxdog = max(a, b)
		for i in range(q):
			(s, e) = map(int, input().split())
			ans = e - s + 1
			(startq, startm) = (s // lcm, s % lcm)
			(endq, endm) = (e // lcm, e % lcm)
			if startq == endq:
				if startm < maxdog:
					if endm < maxdog:
						ans -= endm - startm + 1
					else:
						ans -= maxdog - startm
			else:
				if startm < maxdog:
					ans -= maxdog - startm
				ans -= (endq - startq - 1) * maxdog
				if endm < maxdog:
					ans -= endm + 1
				else:
					ans -= maxdog
			yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


t = int(input())
for u in range(t):
	(a, b, q) = list(map(int, input().split()))
	c = int(a)
	d = int(b)
	cnt = 19
	while a * b != 0:
		if a < b:
			(a, b) = (b, a)
		a = a % b
	nok = c * d // max(a, b)
	act = [0] * nok
	for i in range(1, nok + 1):
		if i % c % d == i % d % c:
			act[i - 1] = 0
		else:
			act[i - 1] = 1
	dp = [0] * (len(act) + 1)
	dp[0] = 0
	dp[1] = act[0]
	for i in range(1, len(act)):
		dp[i + 1] = dp[i] + act[i]
	res = ''
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		if r < nok:
			res += str(dp[r] - dp[l - 1]) + ' '
		else:
			if l % nok == 0:
				dpl = dp[-1] * (l // nok)
			else:
				dpl = dp[-1] * (l // nok) + dp[l % nok - 1]
			res += str(dp[-1] * (r // nok) + dp[r % nok] - dpl) + ' '
	print(res)


import sys
input = sys.stdin.readline
from math import *
t = int(input())

def bo(x):
	global a, b, l
	if x == 0:
		return 0
	h = x // l
	u = 0
	if h > 0:
		u += h * max(a, b)
		u -= 1
	if h != 0:
		u += min(max(a, b), x + 1 - h * l)
	else:
		u += min(max(a, b), x - h * l)
	if h == 0 and max(a, b) <= x - h * l:
		u -= 1
	return x - u
while t > 0:
	t -= 1
	(a, b, q) = map(int, input().split())
	g = gcd(a, b)
	l = a * b // g
	for _ in range(q):
		(x, y) = map(int, input().split())
		if g == min(a, b):
			print(0, end=' ')
		else:
			print(bo(y) - bo(x - 1), end=' ')
	print()


import math
import collections
from sys import *
T = int(stdin.readline())
for _ in range(T):
	(a, b, q) = list(map(int, stdin.readline().split()))
	gc = math.gcd(a, b)
	lc = a * b // gc
	c = [0] * (lc + 1)
	co = 0
	for i in range(1, lc + 1, 1):
		if i % a % b != i % b % a:
			co += 1
			c[i] = co
		else:
			c[i] = co
	c[-1] = co
	for g in range(q):
		(l, r) = list(map(int, stdin.readline().split()))
		ans = 0
		lm = l % lc if l % lc > 0 else lc
		rm = r % lc if r % lc > 0 else lc
		if lm != lc:
			near = (l // lc + 1) * lc
		else:
			near = l // lc * lc
		if r <= near:
			print(c[rm] - c[lm - 1], end=' ')
			continue
		ans = ans + c[lc] - c[lm - 1]
		tot = (r - near) // lc
		ans = ans + tot * co
		if rm == lc:
			print(ans, end=' ')
			continue
		ans = ans + c[rm]
		print(ans, end=' ')
	print('')


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
t = int(input())
for i in range(0, t):
	(a, b, q) = map(int, input().split())
	gatsu = max(a, b)
	gop = a * b // gcd(a, b)
	line = []
	for j in range(0, q):
		line.append(list(map(int, input().split())))
	sm = 0
	d = []
	for k in line:
		sm = 0
		(start, end) = (k[0] // gop * gop, (k[1] // gop + 1) * gop)
		sm += (end - start) // gop * gatsu
		if 0 <= k[0] % gop <= gatsu - 1:
			sm -= k[0] - start
		else:
			sm -= gatsu
		if 0 <= k[1] % gop <= gatsu - 1:
			sm -= gatsu - 1 - k[1] % gop
		else:
			sm -= 0
		d.append(k[1] - k[0] + 1 - sm)
	for kk in d:
		print(kk, end=' ')
	print()


t = int(input())

def gcd(l, r):
	if l == 0:
		return r
	return gcd(r % l, l)

def fun(x, lcm, b):
	quo = x // lcm
	sub = quo * b
	sub += min(b, x - lcm * quo)
	return x - sub
for _ in range(t):
	(a, b, q) = list(map(int, input().strip().split()))
	if a > b:
		(a, b) = (b, a)
	ans = []
	for i in range(q):
		res = 0
		(l, r) = list(map(int, input().strip().split()))
		lcm = a * b // gcd(a, b)
		res = fun(r + 1, lcm, b) - fun(l, lcm, b)
		ans.append(max(0, res))
	for i in ans:
		print(i, end=' ')
	print()


T = int(input())
for t in range(T):
	(a, b, Q) = [int(i) for i in input().split()]
	prod = a * b
	precompute = [i % a % b != i % b % a for i in range(prod + 1)]
	prefix = [0 for i in range(prod + 1)]
	prefix[0] = 0
	for i in range(1, prod + 1):
		prefix[i] = prefix[i - 1] + (1 if precompute[i] else 0)

	def total_seg(x):
		ans = x // prod * prefix[-1]
		tmp = x % prod
		if tmp > 0:
			ans += prefix[tmp]
		return ans
	final = []
	for q in range(Q):
		(l, r) = [int(i) for i in input().split()]
		final.append(total_seg(r) - total_seg(l - 1))
	print(*final)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	mod = 1000000007
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')

	def findcount(r, lcm, b):
		div = r // lcm
		prod = div * lcm
		tot = r
		if div != 0:
			tot -= min(b, r - prod + 1)
			tot -= (div - 1) * b
		return tot - min(r, b - 1)
	tc = ri()
	for _ in range(tc):
		(a, b, q) = ria()
		ansq = []
		(a, b) = (min(a, b), max(a, b))
		lcm = a * b // math.gcd(a, b)
		for i in range(q):
			(l, r) = ria()
			if lcm == b or r < b:
				ansq.append(0)
				continue
			cnt = findcount(r, lcm, b)
			if l > 1:
				cnt -= findcount(l - 1, lcm, b)
			ansq.append(cnt)
		print(*ansq)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def Gcd(n, m):
	if m == 0:
		return n
	else:
		return Gcd(m, n % m)

def Lcm(n, m):
	return int(n * m // Gcd(n, m))
t = int(input())
while t != 0:
	(a, b, q) = map(int, input().split())
	dl = Lcm(a, b)
	if a < b:
		(a, b) = (b, a)
	n = int(q)
	ar = list()
	while q != 0:
		(L, R) = map(int, input().split())
		L = max(L, a)
		num = int(0)
		if L <= R and a % b != 0:
			L = L - 1
			cut = int(L // dl)
			d = L + 1 - cut * dl
			if d <= a and cut > 0:
				L -= d
				cut = cut - 1
			L -= cut * a
			cut = int(R // dl)
			d = R + 1 - cut * dl
			if d <= a and cut > 0:
				R -= d
				cut = cut - 1
			R -= cut * a
			num = R - L
		ar.append(int(num))
		q = q - 1
	for i in range(0, n):
		print(ar[i], end=' ')
	print('\n')
	t = t - 1


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		(a, b, q) = LI()
		qa = [LI() for _ in range(q)]
		ab = a * b
		t = [0] * (ab + 1)
		for i in range(1, ab + 1):
			if i % a % b != i % b % a:
				t[i] = t[i - 1] + 1
			else:
				t[i] = t[i - 1]
		r = []
		tt = t[-1]
		for (c, d) in qa:
			c -= 1
			e = c // ab * tt + t[c % ab]
			f = d // ab * tt + t[d % ab]
			r.append(f - e)
		rr.append(JA(r, ' '))
	return JA(rr, '\n')
print(main())


class DenseBlock:

	def __init__(self, block):
		self.block = block

	def getSum(self, l, r):
		assert 0 <= l <= r <= len(self)
		return sum(self.block[l:r])

	def __len__(self):
		return len(self.block)

class SumBlock:

	def __init__(self, b1, b2):
		self.b1 = b1
		self.b2 = b2

	def getSum(self, l, r):
		assert 0 <= l <= r <= len(self)
		m = len(self.b1) - l
		if m < 0:
			return self.b2.getSum(l - len(self.b1), r - len(self.b1))
		elif r - len(self.b1) > 0:
			return self.b1.getSum(l, l + m) + self.b2.getSum(0, r - len(self.b1))
		else:
			return self.b1.getSum(l, r)

	def __len__(self):
		return len(self.b1) + len(self.b2)

class AllSameBlock:

	def __init__(self, val, count):
		self.val = val
		self.count = count

	def getSum(self, l, r):
		assert 0 <= l <= r <= len(self)
		return self.val * (r - l)

	def __len__(self):
		return self.count
s1 = SumBlock(DenseBlock([1, 2, 3, 4]), DenseBlock([5, 6]))
s2 = DenseBlock([1, 2, 3, 4, 5, 6])

class InfiniteRepeatBlock:

	def __init__(self, block):
		self.block = block

	def getSum(self, l, r):
		block_size = len(self.block)
		left_block_id = l // block_size
		right_block_id = r // block_size
		left_id = l % block_size
		right_id = r % block_size
		if left_block_id == right_block_id:
			return self.block.getSum(left_id, right_id)
		num_full_blocks = right_block_id - left_block_id - 1
		return num_full_blocks * self.block.getSum(0, len(self.block)) + self.block.getSum(left_id, len(self.block)) + self.block.getSum(0, right_id)
import math

def ans2(a, b, l, r):
	ret = 0
	for x in range(l, r + 1):
		if x % a % b != x % b % a:
			ret += 1
	return ret

def ans(a, b, l, r):
	if a > b:
		return ans(b, a, l, r)
	a = a // math.gcd(a, b)
	arr = InfiniteRepeatBlock(SumBlock(AllSameBlock(1, b), AllSameBlock(0, b * (a - 1))))
	return r - l + 1 - arr.getSum(l, r + 1)
T = int(input())
for _ in range(T):
	(a, b, q) = input().split(' ')
	a = int(a)
	b = int(b)
	q = int(q)
	res = []
	for _ in range(q):
		(l, r) = input().split(' ')
		l = int(l)
		r = int(r)
		res += [str(ans(a, b, l, r))]
	print(' '.join(res))


t = int(input())
for _ in range(t):
	(a, b, q) = list(map(int, input().split()))
	d = []
	for i in range(a * b):
		if i % a % b != i % b % a:
			d.append(1)
		else:
			d.append(0)
	sums = [0]
	for i in range(a * b):
		sums.append(sums[-1] + d[i])
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		ans = (r - l) // (a * b) * sums[-1]
		l %= a * b
		r %= a * b
		extra = sums[max(l, r) + 1] - sums[min(l, r)]
		if l <= r:
			ans += extra
		else:
			ans = ans - extra + sums[-1] + d[r] + d[l]
		print(ans, end=' ')
	print()


from math import gcd

def lcm(p, q):
	return p * q // gcd(p, q)

def solve(mod, cmax, l, r):
	(lmod, ldiv) = (l % mod, l // mod)
	(rmod, rdiv) = ((r + 1) % mod, (r + 1) // mod)
	return r - l - ((rdiv - ldiv) * cmax - min(cmax, lmod) + min(cmax, rmod)) + 1
t = int(input())
for _ in range(t):
	(a, b, q) = [int(i) for i in input().split()]
	ans = []
	for _ in range(q):
		(m, c) = (lcm(a, b), max(a, b))
		(left, right) = [int(i) for i in input().split()]
		ans.append(solve(m, c, left, right))
	print(' '.join([str(i) for i in ans]))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def gdc(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	if a < b:
		(a, b) = (b, a)
	for i in range(q):
		(l, r) = map(int, input().split())
		nok = a * b // gdc(a, b)
		left_border = l // nok * nok
		right_border = r // nok * nok
		ans = a * ((right_border - left_border) // nok + 1)
		if right_border + a - 1 > r:
			ans -= right_border + a - 1 - r
		if left_border < l:
			ans -= min(a, l - left_border)
		print(r - l + 1 - ans, end=' ')
	print()


import math

def countDivisibles(A, B, M):
	if A % M == 0:
		return B // M - A // M + 1
	return B // M - A // M

def lcm(A, B):
	lc = A * B // math.gcd(A, B)
	return lc
for i in range(int(input())):
	(a, b, q) = map(int, input().split())
	lc = lcm(a, b)
	for j in range(q):
		(l, r) = map(int, input().split())
		e = r // lc * lc
		e1 = l // lc * lc
		tm = 0
		ta = 0
		if e + max(a, b) - 1 > r:
			tm = e + max(a, b) - r - 1
		if e1 + max(a, b) - 1 >= l and e1 < l:
			ta = e1 + max(a, b) - l
		rw = countDivisibles(l, r, lc) * max(a, b)
		rw = rw - tm + ta
		print(max(0, r - l + 1 - rw))


import sys
input = sys.stdin.buffer.readline
ans = []
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	m = a * b
	ret = []
	c = [int(i % a % b != i % b % a) for i in range(m)]
	s = sum(c)
	for i in range(m - 1):
		c[i + 1] += c[i]
	for _ in range(q):
		(l, r) = map(int, input().split())
		rr = r // m * s + c[r % m]
		ll = (l - 1) // m * s + c[(l - 1) % m]
		ret.append(rr - ll)
	ans.append(' '.join(map(str, ret)))
print('\n'.join(ans))


t = int(input())

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
for xx in range(t):
	(a, b, q) = map(int, input().split())
	cf = a * b // gcd(a, b)
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split())
		if l % cf <= max(a, b):
			l = l // cf * cf + max(a, b)
		if r % cf < max(a, b):
			r = r // cf * cf - 1
		if l > r or a % b == 0 or b % a == 0:
			ans.append(0)
			continue
		l1 = (l - 1) // cf
		r1 = r // cf
		an = max(r1 - l1, 0)
		giv = r - l + 1
		giv -= an * max(a, b)
		ans.append(giv)
	lel = ' '.join([str(i) for i in ans])
	print(lel)


import math
for _ in range(int(input())):
	(a, b, n) = map(int, input().split())
	if b < a:
		(a, b) = (b, a)
	g = a * b // math.gcd(a, b)
	for tt in range(n):
		(l, r) = map(int, input().split())
		q1 = r % g
		q2 = (l - 1) % g
		q = r // g * b + min(q1 + 1, b) - (l - 1) // g * b - min(q2 + 1, b)
		tot = r - l + 1
		print(max(0, tot - q))


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	c = a * b
	s = [0] * c
	for i in range(c):
		if i % a % b != i % b % a:
			s[i] += 1
	for i in range(1, c):
		s[i] += s[i - 1]
	ans = []
	for __ in range(q):
		(l, r) = map(int, input().split())
		l -= 1
		rp = r // c
		rm = r % c
		lp = l // c
		lm = l % c
		rx = rp * s[-1]
		if rm > 0:
			rx += s[rm]
		lx = lp * s[-1]
		if lm > 0:
			lx += s[lm]
		ans.append(rx - lx)
	print(*ans)


from sys import stdin
from math import gcd
input = stdin.buffer.readline

def f(R, b, x):
	ans = 0
	if R // b % x == 0:
		ans += R % b + 1
	R -= R % b + 1
	ans += b * (R // b // x)
	return ans

def g(l, r, b, x):
	return r - l + 1 - f(r, b, x) + f(l - 1, b, x)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	x = a // gcd(a, b)
	for __ in range(q):
		(l, r) = map(int, input().split())
		print(g(l, r, b, x), end=' ')
	print()


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
t = int(input())
for test in range(t):
	(a, b, q) = list(map(int, input().split()))
	if a == b:
		for query in range(q):
			(l, r) = list(map(int, input().split()))
			print(int(0), end=' ')
		print()
	else:
		greater = max(a, b)
		smaller = min(a, b)
		lc = int(lcm(greater, smaller))
		count = 0
		valid_list = [0] * lc
		valid_count = [0] * lc
		for i in range(0, lc):
			if i % smaller % greater != i % greater % smaller:
				count += 1
				valid_list[i] = 1
			valid_count[i] = count
		for query in range(q):
			(l, r) = list(map(int, input().split()))
			ans = 0
			cycles_l = (l - 1) // lc
			rem_l = (l - 1) % lc
			ans_l = cycles_l * count + valid_count[rem_l]
			cycles_r = r // lc
			rem_r = r % lc
			ans_r = cycles_r * count + valid_count[rem_r]
			ans = ans_r - ans_l
			print(int(ans), end=' ')
		print()


from math import gcd
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def lcm(a, b):
	return abs(a * b) // gcd(a, b)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	l = []
	for i in range(q):
		l.append(list(map(int, input().split())))
	(fr1, fr2) = (lcm(a, b), max(a, b))
	for (s, e) in l:
		ans = 0
		t1 = (s // fr1 + (1 if s % fr1 != 0 else 0)) * fr1
		t2 = t1 - fr1
		if t1 < e:
			kk = max(s, fr2 + t2)
			ans += t1 - kk
			t3 = (e // fr1 + (1 if e % fr1 != 0 else 0)) * fr1
			t4 = t3 - fr1
			if e % fr1 == 0:
				e -= 1
			tt = e - (fr2 + t4)
			if tt >= 0:
				ans += tt + 1
			ans += (t4 - t1) // fr1 * (fr1 - fr2)
		else:
			if e % fr1 == 0:
				e -= 1
			kk = max(s, fr2 + t2)
			kk2 = min(e, t1 - 1)
			ans += max(kk2 - kk + 1, 0)
		print(ans, end=' ')
	print()


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	mi = min(a, b)
	ma = max(a, b)
	for j in range(q):
		(l, r) = map(int, input().split())
		l = max(l, ma)
		if a == b or mi == 1 or l > r:
			print(0, end=' ')
		else:
			ans = r - l + 1
			g = a * b // gcd(a, b)
			k = r // g
			lev = l // g
			if g * lev + ma - 1 < l:
				lev += 1
			if k >= lev:
				ans -= (k - lev) * ma
				ans -= min(r - g * k + 1, ma)
				ans += max(0, l - g * lev)
			if ans <= 0:
				ans = 0
			print(ans, end=' ')
	print()


import sys
from collections import Counter
from math import gcd
INF = 2 ** 62 - 1

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def ref(A, B, Q):
	L = 0
	for (l, r) in Q:
		L = max(L, l)
		L = max(L, r)
	s = [0] * (L + 1)
	for i in range(len(s)):
		if i % A % B != i % B % A:
			s[i] = 1
	ans = []
	for (l, r) in Q:
		ans.append(sum(s[:r + 1]) - sum(s[:l]))
	return ans

def slv(A, B, Q):
	if max(A, B) % min(A, B) == 0:
		return [0] * len(Q)
	(A, B) = (min(A, B), max(A, B))
	cycle = A * B // gcd(A, B)
	ans = []

	def q(n):
		cn = n // cycle
		r = cn * (cycle - B) + max(0, n % cycle + 1 - B)
		return r
	for (l, r) in Q:
		ans.append(q(r) - q(l - 1))
	return ans

def main():
	T = read_int()
	for _ in range(T):
		(A, B, N) = read_int_n()
		Q = [read_int_n() for _ in range(N)]
		print(*slv(A, B, Q))
main()


from math import gcd
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	ans = []
	c = max(a, b)
	d = min(a, b)
	x = c * d // gcd(c, d)
	m = 0
	z = [0]
	for i in range(1, x + 1):
		if i % c % d != i % d % c:
			m += 1
		z.append(m)
	for i in range(q):
		(l, r) = map(int, input().split())
		p = -z[l % x - 1]
		p += z[r % x]
		r -= r % x
		p += ((r - l) // x + 1) * m
		ans.append(p)
	print(*ans)


import math
t = int(input())

def check1(l, r, aa, bb):
	if l <= aa and aa <= r and (r <= bb) or (aa <= l and l <= bb and (bb <= r)):
		return True
	return False
for __ in range(t):
	(a, b, q) = list(map(int, input().split()))
	if a > b:
		(a, b) = (b, a)
	gap = b
	inter = a * b // math.gcd(a, b)
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		idx1 = l // inter
		idx2 = r // inter
		if inter * idx1 + b - 1 < l:
			idx1 += 1
		res = 0
		if idx2 - idx1 > 6:
			rng = []
			while idx1 >= 0:
				aa = inter * idx1
				bb = inter * idx1 + b - 1
				if check1(l, r, aa, bb):
					rng.append(idx1)
				else:
					break
				idx1 += 1
			while idx2 >= 0:
				aa = inter * idx2
				bb = inter * idx2 + b - 1
				if check1(l, r, aa, bb):
					rng.append(idx2)
				else:
					break
				idx2 -= 1
			res = b * (idx2 - idx1 + 1)
			for idx in rng:
				aa = inter * idx
				bb = inter * idx + b - 1
				fl = 0
				if aa == 0:
					fl = 1
					aa += 1
				if l >= aa and bb >= r:
					res += r - l + 1
				elif l <= aa and bb <= r:
					res += b - fl
				elif aa <= l and bb >= l:
					res += bb - l + 1
				elif aa <= r and bb >= r:
					res += r - aa + 1
			print(r - l + 1 - res, end=' ')
			continue
		for idx in range(idx1, idx2 + 1):
			aa = inter * idx
			bb = inter * idx + b - 1
			fl = 0
			if aa == 0:
				fl = 1
				aa += 1
			if l >= aa and bb >= r:
				res += r - l + 1
			elif l <= aa and bb <= r:
				res += b - fl
			elif aa <= l and bb >= l:
				res += bb - l + 1
			elif aa <= r and bb >= r:
				res += r - aa + 1
		print(r - l + 1 - res, end=' ')
	print()


t = int(input())
for you in range(t):
	l = input().split()
	a = int(l[0])
	b = int(l[1])
	q = int(l[2])
	arr = [0 for i in range(a * b)]
	for i in range(a * b):
		if i % b % a != i % a % b:
			arr[i] = 1
	prefsum = [0 for i in range(a * b)]
	sumnow = 0
	for i in range(a * b):
		sumnow += arr[i]
		prefsum[i] = sumnow
	full = prefsum[-1]
	arr = [0 for i in range(q)]
	for i in range(q):
		ans = 0
		l = input().split()
		L = int(l[0])
		R = int(l[1])
		ansl = full * ((L - 1) // (a * b))
		x = (L - 1) % (a * b)
		ansl = ansl + prefsum[x] - prefsum[0]
		ansr = full * (R // (a * b))
		x = R % (a * b)
		ansr = ansr + prefsum[x] - prefsum[0]
		arr[i] = ansr - ansl
	for i in arr:
		print(i, end=' ')
	print()


from sys import stdin, stdout
from math import gcd
t = int(input())
for it in range(t):
	(a, b, q) = [int(i) for i in stdin.readline().split()]
	if a > b:
		(a, b) = (b, a)
	v = a * b // gcd(a, b)
	res = []
	for it2 in range(q):
		(l, r) = [int(i) for i in stdin.readline().split()]
		sm = 0
		for m in range(b):
			(l1, r1) = (l - m, r - m)
			sm += max(0, r1 // v - (l1 + v - 1) // v + 1)
		res.append(str(r - l + 1 - sm))
	stdout.write(' '.join(res) + '\n')


t = int(input())
for _ in range(t):
	(a, b, q) = list(map(int, input().split()))
	table = []
	cumulative_sum = []
	count = 0
	for i in range(a * b):
		if i % a % b == i % b % a:
			table.append(0)
		else:
			table.append(1)
	for r in table:
		count += r
		cumulative_sum.append(count)
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		l = l - 1
		until_r = r // (a * b) * cumulative_sum[a * b - 1] + cumulative_sum[r % (a * b)]
		until_l = l // (a * b) * cumulative_sum[a * b - 1] + cumulative_sum[l % (a * b)]
		print(until_r - until_l)


t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	dp = [0]
	for j in range(1, a * b):
		if j % a % b != j % b % a:
			dp.append(dp[-1] + 1)
		else:
			dp.append(dp[-1])
	for j in range(q):
		(l, r) = map(int, input().split())
		print((r // (a * b) - (l - 1) // (a * b)) * dp[-1] + dp[r % (a * b)] - dp[(l - 1) % (a * b)], end=' ')
	print()


import sys, math, cmath, time
start_time = time.time()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s)]

def invr():
	return map(int, input().split())

def solve():
	(a, b, q) = invr()
	w = a * b // math.gcd(a, b)
	qw = []
	for i in range(w):
		if not qw:
			qw.append(0)
		qq = qw[-1]
		if (i + 1) % a % b != (i + 1) % b % a:
			qq = qq + 1
		qw.append(qq)
	qwqw = []
	for i in range(q):
		(l, r) = invr()
		l = l - 1
		lc = math.floor(l // w) * qw[-1]
		rc = math.floor(r // w) * qw[-1]
		qwqw.append(max(0, rc - lc + qw[r % w] - qw[l % w]))
	print(*qwqw)

def main():
	ONLINE_JUDGE = __debug__
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed:', time.time() - start_time, 'seconds')

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q
main()
sys.stdout.close()


import os
import sys
from io import BytesIO, IOBase

def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))

def main():
	from math import gcd

	def lcm(a, b):
		return a * b // gcd(a, b)
	T = iin()
	while T:
		T -= 1
		(a, b, q) = lin()
		if a > b:
			(a, b) = (b, a)
		lcm1 = lcm(a, b)
		ans = []
		for i in range(q):
			sol = 0
			(l, r) = lin()
			l1 = l
			r1 = r
			sol += max(min(r, b - 1) - l + 1, 0)
			l = max(lcm1, l)
			if l <= r:
				x1 = r // lcm1 - (l - 1) // lcm1
				sol += x1 * b
				x2 = r // lcm1 * lcm1
				if x1 and x2 + b > r:
					sol -= b
					sol += min(r - x2 + 1, b)
				x2 = (l - 1) // lcm1 * lcm1
				if x2 + b > l:
					sol += min(x2 + b - 1, r) - l + 1
			ans.append(r1 - l1 + 1 - sol)
		print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n_cases = int(input())
for case_num in range(n_cases):
	(a, b, q) = [int(x) for x in input().split()]
	k = a * b
	pref = [0] * k
	for i in range(1, k):
		pref[i] = pref[i - 1]
		if i % a % b != i % b % a:
			pref[i] += 1
	for (i, query) in enumerate(range(q)):
		(l, r) = [int(x) for x in input().split()]
		count = 0
		if l % k != 0:
			count += pref[k - 1] - pref[l % k - 1]
			l = (l // k + 1) * k
		if r % k != k - 1:
			count += pref[r % k]
			r = r // k * k - 1
		assert (r - l + 1) % k == 0
		count += pref[k - 1] * ((r - l + 1) // k)
		if i > 0:
			print(' ', end='')
		print(count, end='')
	print()


from math import gcd
t = int(input())
ans = []
for i in range(t):
	(a, b, q) = map(int, input().split())
	res = []
	pred = []
	gcw = a * b // gcd(a, b)
	for j in range(gcw):
		pred.append(int(j % a % b == j % b % a))
	prefixes = [0]
	suffixes = [0] * (gcw + 1)
	summ = 0
	for j in range(gcw):
		summ += pred[j]
		prefixes.append(summ)
	summ = 0
	for j in range(gcw - 1, -1, -1):
		summ += pred[j]
		suffixes[j] = summ
	for j in range(q):
		(l, r) = map(int, input().split())
		res.append(r - l + 1 - ((r // gcw - l // gcw + 1) * prefixes[-1] - prefixes[l % gcw] - suffixes[r % gcw + 1]))
	ans.append(res)
for i in ans:
	print(*i)


import math
numcases = int(input())

def lcm(a, b):
	return a * b // math.gcd(a, b)

def solve(a, b, l, r):
	cm = lcm(a, b)
	l = l - 1
	desync = cm - a
	sum1 = r // cm * desync + max(0, r % cm + 1 - a)
	sum2 = l // cm * desync + max(0, l % cm + 1 - a)
	print(sum1 - sum2, end=' ')
for i in range(numcases):
	(z, x, q) = map(int, input().split())
	for i in range(q):
		a = max(z, x)
		b = min(z, x)
		(l, r) = map(int, input().split())
		solve(a, b, l, r)
	print()


def compute_lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm

def diffsum(x, r):
	return max(0, x - r + 1)
t = int(input())
for tt in range(t):
	(a, b, q) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	lcm = compute_lcm(a, b)
	lcmsum = lcm - b
	sol = []
	for qq in range(q):
		(l, r) = map(int, input().split())
		lm = (l - 1) % lcm
		sol.append(-diffsum(lm, b))
		rm = r % lcm
		sol[-1] -= lcmsum - diffsum(rm, b)
		sol[-1] += (r + lcm - rm - l + lm + 1) // lcm * lcmsum
	print(' '.join(map(str, sol)))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	fr = [0] * (lcm + 1)
	ct = 0
	for i in range(1, lcm + 1):
		if i % a % b != i % b % a:
			ct += 1
			fr[i] = ct
		else:
			fr[i] = fr[i - 1]
	for i in range(q):
		ans = 0
		(l, r) = map(int, input().split())
		ans = r // lcm * ct + fr[r % lcm]
		ans -= l // lcm * ct + fr[l % lcm]
		if l % a % b != l % b % a:
			ans += 1
		print(ans, end=' ')
	print()


import math
t = int(input())

def solve(a, b, l, r):
	lcm = a * b // math.gcd(a, b)
	f = l // lcm
	s = r // lcm
	if s > f:
		count = (s - f - 1) * b
	else:
		count = 0
	if s != f:
		for j in range(f * lcm, min(f * lcm + b, r + 1)):
			if l <= j <= r:
				count += 1
		for j in range(s * lcm, min(s * lcm + b, r + 1)):
			if l <= j <= r:
				count += 1
	else:
		for j in range(f * lcm, min(f * lcm + b, r + 1)):
			if l <= j <= r:
				count += 1
	return r - l + 1 - count
while t:
	(a, b, q) = list(map(int, input().split()))
	for j in range(q):
		(l, r) = list(map(int, input().split()))
		if b > a:
			print(solve(a, b, l, r))
		else:
			print(solve(b, a, l, r))
	t -= 1


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		(a, b, q) = In()
		pd = a * b
		arr = [0 for i in range(pd)]
		for i in range(1, pd):
			arr[i] = arr[i - 1] + (i % a % b != i % b % a)
		ans = []
		for i in range(q):
			(l, r) = In()
			val = r // pd * arr[-1] + arr[r % pd]
			val -= (l - 1) // pd * arr[-1] + arr[(l - 1) % pd]
			ans.append(val)
		print(*ans)
main()


t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	if b > a:
		(a, b) = (b, a)
	x = 0
	for i in range(1, b + 1):
		if i * a % b == 0:
			x = i
			break

	def cnt(n):
		tmp = n // a
		if tmp % x == 0:
			return n + 1 - tmp // x * a - (n % a + 1)
		else:
			return n + 1 - (tmp // x + 1) * a
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		ans.append(cnt(r) - cnt(l - 1))
	print(*ans)


import math
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	s = []
	lcm = int(a * b / math.gcd(a, b))
	k = lcm - max(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		if max(l, r) < max(a, b):
			s.append('0')
		else:
			total1 = r // lcm * k + max(0, r % lcm - max(a, b) + 1)
			total2 = (l - 1) // lcm * k + max(0, (l - 1) % lcm - max(a, b) + 1)
			total1 -= total2
			s.append(str(total1))
	print(' '.join(s))


import math
for ii in range(int(input())):
	t = [int(i) for i in input().split()]
	a = max(t[0], t[1])
	b = min(t[0], t[1])
	q = t[2]
	size = 1
	arr = [0 for i in range(a * b)]
	y = a * b
	arr[0] = 1
	for i in range(1, a * b):
		if i % a % b == i % b % a:
			size += 1
		arr[i] = size
	ans = []
	for i in range(q):
		x = [int(i) for i in input().split()]
		l = x[0]
		r = x[1]
		ans.append(r - l + 1 - (r // y * size + arr[r % y] - (l - 1) // y * size - arr[(l - 1) % y]))
	print(*ans)


from math import gcd
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	lcm = a * b // gcd(a, b)
	ans = [0] * q
	for i in range(q):
		(l, r) = map(int, input().split())
		edge = r // lcm * lcm
		sub = r // lcm * b + min(b, r - edge + 1)
		l -= 1
		edge_l = l // lcm * lcm
		sub -= l // lcm * b + min(b, l - edge_l + 1)
		ans[i] = r - l - sub
	print(*ans)


import math
import collections
from sys import *
T = int(stdin.readline())
for _ in range(T):
	(a, b, q) = list(map(int, stdin.readline().split()))
	gc = math.gcd(a, b)
	lc = a * b // gc
	c = [0] * (lc + 1)
	co = 0
	for i in range(1, lc + 1, 1):
		if i % a % b != i % b % a:
			co += 1
			c[i] = co
		else:
			c[i] = co
	c[-1] = co
	for g in range(q):
		(l, r) = list(map(int, stdin.readline().split()))
		rsum = r // lc * co + c[r % lc]
		lsum = (l - 1) // lc * co + c[(l - 1) % lc]
		print(rsum - lsum, end=' ')
	print('')


import sys
import math

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def F(n, a, b):
	return n // lcm(a, b) * a + min(a, n % lcm(a, b))
t = int(input())
for i in range(0, t):
	(a, b, q) = map(int, input().split())
	if a < b:
		temp = a
		a = b
		b = temp
	for j in range(0, q):
		(l, r) = map(int, input().split())
		r += 1
		ans = r - F(r, a, b) - (l - F(l, a, b))
		print(ans, end=' ')
	print()


def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
T = int(input())
for tc in range(T):
	(a, b, q) = map(int, input().split())
	res = []
	maxx = max(a, b)
	mf = lcm(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		count = 0
		counte = 0
		if maxx > r:
			count = 0
		elif maxx == r:
			count = 1
		elif maxx > l:
			counte = maxx - l
			counte += (r // mf - 1) * maxx
			counte += min(maxx, r - r // mf * mf + 1)
			count = r - l + 1 - counte
		else:
			counte += (r // mf - 1) * maxx - l // mf * maxx + max(0, l // mf * mf + maxx - l)
			counte += min(maxx, r - r // mf * mf + 1)
			count = r - l + 1 - counte
		res.append(str(count))
	print(' '.join(res))


import math
for i in range(int(input())):
	(a, b, q) = list(map(int, input().split()))
	(a, b) = (min(a, b), max(a, b))
	for i in range(q):
		s = 0
		(l, r) = list(map(int, input().split()))
		if b % a == 0:
			print(0)
			continue
		j = b * a // math.gcd(a, b)
		u = l // j
		v = r // j
		s += (v - u) * b
		if l % j < b:
			s += b - l % j
		if r % j < b:
			s -= b - r % j - 1
		print(r - l + 1 - s, end=' ')
	print()


from math import gcd
import sys
input = sys.stdin.readline
from collections import defaultdict as dd
t = int(input())
while t:
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	le = max(a, b)
	re = lcm - 1
	m = le
	for i in range(q):
		(l, r) = map(int, input().split())
		res = (re - le + 1) * (r // (m + re - le + 1))
		li = m + (m + re - le + 1) * (r // (m + re - le + 1))
		if r >= li:
			res += r - li + 1
		l -= 1
		res1 = (re - le + 1) * (l // (m + re - le + 1))
		li = m + (m + re - le + 1) * (l // (m + re - le + 1))
		if l >= li:
			res1 += l - li + 1
		print(res - res1, end=' ')
	print()
	t -= 1


import math

def solve(a, b, queries):
	(a, b) = (min(a, b), max(a, b))
	period = a * b // int(math.gcd(a, b))
	(same, diff) = (b, period - b)

	def f(n):
		parts = n // period
		reminder = n % period
		return parts * diff + max(0, reminder - same)
	for (left, right) in queries:
		yield (f(right + 1) - f(left))

def main():
	T = int(input())
	for t in range(T):
		(a, b, q) = map(int, input().split())
		queries = []
		for _ in range(q):
			(left, right) = map(int, input().split())
			queries.append((left, right))
		print(' '.join(map(str, solve(a, b, queries))))
main()


from math import gcd as g
t = int(input())
while t:
	t += -1
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	a = a
	b = b
	lcm = a * b // g(a, b)
	while q:
		q += -1
		(l, r) = map(int, input().split())
		if b % a == 0:
			print(0, end=' ')
		else:
			k = r // lcm
			ans = 0
			if k != 0:
				ans += min(r, lcm * k + b - 1) - lcm * k + 1
			if k >= 1:
				ans += b * (k - 1) + b - 1
			else:
				ans += min(b - 1, r)
			l = l - 1
			k = l // lcm
			ans1 = 0
			if k != 0:
				ans1 = min(l, lcm * k + b - 1) - lcm * k + 1
			if k >= 1:
				ans1 += b * (k - 1) + b - 1
			else:
				ans1 += min(b - 1, l)
			l = l + 1
			tt = r - l + 1 - (ans - ans1)
			print(tt, end=' ')
	print()


t = int(input())

def gcd(a, b):
	if not b:
		return a
	return gcd(b, a % b)

def LCM(a, b):
	return a * b // gcd(a, b)
for _ in range(t):
	(a, b, q) = map(int, input().split())
	lcm = LCM(a, b)
	temp = [0]
	for x in range(1, lcm + 1):
		ab = x % a % b
		ba = x % b % a
		if ab != ba:
			temp.append(1)
		else:
			temp.append(0)
	for x in range(1, lcm + 1):
		temp[x] += temp[x - 1]
	while q:
		(l, r) = map(int, input().split())
		ansR = r // lcm * temp[-1] + temp[r % lcm]
		l -= 1
		ansL = l // lcm * temp[-1] + temp[l % lcm]
		print(ansR - ansL, end=' ')
		q -= 1
	print()


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b // gcd(a, b)

def solve(m, l, c):
	if l == 0:
		return 0
	r = l % m
	return l // m * c[m] + c[r]
t = int(input().strip())
for __ in range(t):
	(a, b, q) = [int(x) for x in input().strip().split()]
	m = lcm(a, b)
	c = [0]
	for i in range(1, m + 1):
		if i % a % b != i % b % a:
			c.append(c[len(c) - 1] + 1)
		else:
			c.append(c[len(c) - 1])
	ans = []
	for _ in range(q):
		(l, r) = [int(x) for x in input().strip().split()]
		ans.append(str(int(solve(m, r, c) - solve(m, l - 1, c))))
	print(' '.join(ans))


import math
t = int(input())
for _ in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	g = math.gcd(a, b)
	for _ in range(q):
		(l, r) = [int(x) for x in input().split()]
		(a, b) = (min(a, b), max(a, b))
		x = r // b
		y = l // b
		ct = 0
		A = a // g
		if x == y:
			if x % A == 0:
				ct = r - l + 1
			else:
				ct = 0
		else:
			if y % A == 0:
				ct += b * (y + 1) - l
			if x % A == 0:
				ct += r - b * x + 1
			ct += b * ((x - 1) // A - y // A)
		print(r - l + 1 - ct, end=' ')
	print()


from sys import stdin, stdout
from math import *
for __ in range(int(input())):
	(a, b, q) = map(int, input().split())
	ar = [0] * a * b
	for i in range(1, a * b):
		ar[i] = ar[i - 1]
		if i % a % b != i % b % a:
			ar[i] += 1
	rslt = []
	for i in range(q):
		(l, r) = map(int, input().split())
		ans = r // (a * b) * ar[-1] + ar[r % (a * b)]
		ans -= (l - 1) // (a * b) * ar[-1] + ar[(l - 1) % (a * b)]
		rslt.append(ans)
	print(*rslt)


def calc(arr, p, n):
	return arr[n - 1] * (p // n) + arr[p % n]
t = int(input())
for T in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	if a > b:
		(a, b) = (b, a)
	ans = [0] * q
	arr = [0] * (b * a)
	for i in range(1, a * b):
		arr[i] = arr[i - 1]
		if i % a % b != i % b % a:
			arr[i] += 1
	for Q in range(q):
		(l, r) = [int(x) for x in input().split()]
		ans[Q] = calc(arr, r, a * b) - calc(arr, l - 1, a * b)
	print(*ans)


import sys
input = sys.stdin.buffer.readline

def I():
	return list(map(int, input().split()))

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
for __ in range(int(input())):
	(a, b, q) = I()
	pref = [0] * (a * b)
	pref[0] = 1
	for i in range(1, a * b):
		if i % a % b == i % b % a:
			pref[i] = pref[i - 1] + 1
		else:
			pref[i] = pref[i - 1]
	for lol in range(q):
		(l, r) = I()
		l -= 1
		tillL = pref[-1] * (l // (a * b)) + pref[l % (a * b)]
		tillR = pref[-1] * (r // (a * b)) + pref[r % (a * b)]
		print(r - l - (tillR - tillL), end=' ')
	print()


def presum(a, b):
	res = [0 for i in range(a * b)]
	for m in range(1, a * b):
		if m % a % b != m % b % a:
			res[m] = res[m - 1] + 1
		else:
			res[m] = res[m - 1]
	return res
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	ans = []
	pre = presum(a, b)

	def counts(m):
		return m // (a * b) * pre[-1] + pre[m % (a * b)]
	for __ in range(q):
		(left, right) = map(int, input().split())
		ans.append(counts(right) - counts(left - 1))
	print(*ans)


num = int(input())
list_ = [0] * 200 * 200

def getSolve(list, n, target):
	return list[n - 1] * (target // n) + list[target % n]
for _ in range(num):
	(a, b, q) = [int(i) for i in input().split()]
	res = []
	for i in range(1, a * b):
		list_[i] = list_[i - 1]
		if i % a % b != i % b % a:
			list_[i] += 1
	for _ in range(q):
		(l, r) = [int(i) for i in input().split()]
		res.append(str(getSolve(list_, a * b, r) - getSolve(list_, a * b, l - 1)))
	print(' '.join(res))


from sys import stdin, stdout
input = stdin.readline
from math import gcd

def main():
	t = int(input())
	for i in range(t):
		(a, b, q) = map(int, input().split())
		ab = a * b // gcd(a, b)
		ar = [0] * ab
		for num in range(ab):
			ar[num] = int(num % a % b != num % b % a)
		ar2 = [0] * (ab + 1)
		for i in range(ab):
			ar2[i] = ar2[i - 1] + ar[i]
		for i in range(q):
			(l, r) = map(int, input().split())
			l2 = l % ab
			r2 = r % ab
			if r - l + 1 <= ab - l2:
				print(ar2[r2] - ar2[l2 - 1], end=' ')
			else:
				ans = (r - l + 1 - ab + l2 - r2) // ab * ar2[-2]
				ans += ar2[-2] - ar2[l2 - 1]
				ans += ar2[r2]
				print(ans, end=' ')
		print()
main()


import sys
input = sys.stdin.buffer.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def yo():
	for _ in range(int(input().strip())):
		(a, b, q) = map(int, input().split())
		bad = [0 for i in range(a * b)]
		for x in range(a * b):
			if x % a % b == x % b % a:
				bad[x] = 1
		for i in range(1, a * b):
			bad[i] += bad[i - 1]
		for _ in range(q):
			(l, r) = map(int, input().split())
			total = r - l + 1
			total_bad = 0
			if l // (a * b) == r // (a * b):
				if l % (a * b) == 0:
					total_bad += bad[r % (a * b)]
				else:
					total_bad += bad[r % (a * b)] - bad[l % (a * b) - 1]
				total -= total_bad
				print(total)
			else:
				if l % (a * b) == 0:
					total_bad += bad[-1]
				else:
					total_bad += bad[-1] - bad[l % (a * b) - 1]
				total_bad += bad[r % (a * b)]
				q = l // (a * b)
				bottom = q + 1
				top = r // (a * b)
				total_bad += (top - bottom) * bad[-1]
				total -= total_bad
				print(total)
yo()


from sys import stdin, stdout

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
for _ in range(int(stdin.readline())):
	(a, b, q) = map(int, stdin.readline().split())
	(a, b) = sorted([a, b])
	z = a * b // gcd(a, b)
	ar = [0] * (z + 1)
	for i in range(1, z + 1):
		if i % a % b != i % b % a:
			ar[i] = ar[i - 1] + 1
		else:
			ar[i] = ar[i - 1]
	for _ in range(q):
		(l, r) = map(int, stdin.readline().split())
		s = ar[z] * ((r - l) // z)
		l += z * ((r - l) // z)
		if r == l:
			if r % z == 0:
				s += ar[z] - ar[z - 1]
			else:
				s += ar[r % z] - ar[r % z - 1]
		elif r % z == 0:
			s += ar[z] - ar[l % z - 1]
		elif l % z == 0:
			s += ar[z] - ar[z - 1] + ar[r % z]
		elif l % z > r % z:
			s += ar[r % z] + ar[z] - ar[l % z - 1]
		else:
			s += ar[r % z] - ar[l % z - 1]
		stdout.write(str(s) + ' ')
	print()


import math

def call(x, lcm, b):
	return (lcm - b) * (x // lcm)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	lcm = a * b // math.gcd(a, b)
	dp = [0] * lcm
	for i in range(b, lcm):
		dp[i] += dp[i - 1] + 1
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		ans.append(call(r, lcm, b) + dp[r % lcm] - call(l - 1, lcm, b) - dp[(l - 1) % lcm])
	print(*ans)


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return int(a * b / gcd(a, b))
t = int(input())
for tt in range(t):
	(a, b, q) = [int(i) for i in str.split(input(), ' ')]
	ctr = [0]
	lcm_res = lcm(a, b)
	for i in range(1, lcm_res):
		if i % a % b != i % b % a:
			ctr.append(ctr[-1] + 1)
		else:
			ctr.append(ctr[-1])
	res = []
	for qq in range(q):
		(l, r) = [int(i) for i in str.split(input(), ' ')]
		cur_res = 0
		prev_l = l // lcm_res * lcm_res
		next_l = (1 + l // lcm_res) * lcm_res
		prev_r = r // lcm_res * lcm_res
		if prev_l == prev_r:
			if l - prev_l - 1 < 0:
				cur_res += ctr[r - prev_r]
			else:
				cur_res += ctr[r - prev_r] - ctr[l - prev_l - 1]
		elif l - prev_l - 1 < 0:
			cur_res += ctr[-1] + ctr[r - prev_r]
		else:
			cur_res += ctr[-1] - ctr[l - prev_l - 1] + ctr[r - prev_r]
		if next_l < prev_r:
			cur_res += (prev_r - next_l) // lcm_res * ctr[-1]
		res.append(int(cur_res))
	print(' '.join(map(str, res)))


from math import gcd
for i in range(int(input())):
	(a, b, q) = [int(i) for i in input().split()]
	ans = []
	for _ in range(q):
		(l, r) = [int(i) for i in input().split()]
		if a == b:
			ans += [0]
		else:
			same = 0
			if a > b:
				(a, b) = (b, a)
			c = a * b // gcd(a, b)
			k = l % c
			if k < b:
				if r - l + 1 <= b - k:
					ans += [0]
					continue
				else:
					same += b - k
			m = r // c - l // c
			same += m * b
			k = r % c
			if k < b:
				same -= b - (k + 1)
			ans += [r - l + 1 - same]
	print(*ans)


from math import *
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	d = gcd(a, b)
	p = a * b // d
	ar = []
	for i in range(q):
		(l, r) = map(int, input().split())
		if a == b:
			ar.append(0)
		else:
			if a > b:
				(a, b) = (b, a)
			if r < b:
				ar.append(0)
			else:
				c = r - l + 1
				x = r // p - (l - 1) // p
				x = x * b
				x = x + min(r % p + 1, b) - min((l - 1) % p + 1, b)
				c = c - x
				ar.append(max(c, 0))
	for i in range(q):
		print(ar[i], end=' ')
	print()


from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect
import math

def test_case():
	info = list(map(int, input().split()))
	(a, b, q) = info
	ans = []
	u = [0] * (a * b)
	for i in range(1, a * b):
		u[i] = u[i - 1] + int(i % a % b != i % b % a)
	for _ in range(q):
		(l, r) = map(int, input().split())
		count = 0
		count += r // (a * b) * u[-1] + u[r % (a * b)]
		count -= (l - 1) // (a * b) * u[-1] + u[(l - 1) % (a * b)]
		ans.append(count)
	print(*ans)
for _ in range(int(input())):
	ret = test_case()
	if ret != None:
		print(ret)


t = int(input())
import math
for _ in range(t):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	ans = []
	lr = [tuple(map(int, input().split())) for i in range(q)]
	if a == b:
		print(*[0] * q)
		continue
	for (l, r) in lr:
		if r < b:
			ans.append(0)
		else:
			x = a * b // math.gcd(a, b)
			R = r // x * (x - b)
			R += max(0, r % x - b + 1)
			L = (l - 1) // x * (x - b)
			L += max(0, (l - 1) % x - b + 1)
			ans.append(R - L)
	print(*ans)


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)

def f(R, a, b):
	complete_periods = (R + 1) // lcm(a, b)
	return complete_periods * (lcm(a, b) - b) + max(0, R + 1 - lcm(a, b) * complete_periods - b)
for _ in range(int(input())):
	(a, b, q) = [int(x) for x in input().split()]
	if a > b:
		(a, b) = (b, a)
	for __ in range(q):
		(L, R) = [int(x) for x in input().split()]
		print(f(R, a, b) - f(L - 1, a, b), end=' ')
	print()


from math import gcd
for _ in range(int(input())):
	(a, b, q) = list(map(int, input().split()))
	lcm = a * b // gcd(a, b)
	arr = [0 for i in range(lcm)]
	pref = [0 for i in range(lcm)]
	for i in range(lcm):
		v1 = i % a % b
		v2 = i % b % a
		if v1 != v2:
			arr[i] = 1
	for i in range(1, lcm):
		pref[i] += arr[i] + pref[i - 1]
	final = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		x = pref[-1]
		num = r - l + 1
		y = l % lcm
		ans = 0
		if num <= lcm - y:
			ans += pref[y + num - 1] - pref[y]
			if arr[y]:
				ans += 1
			num -= min(num, lcm - y)
		else:
			num -= lcm - y
			ans += x - pref[y]
			if arr[y]:
				ans += 1
			ans += x * (num // lcm)
			num = num % lcm
		if num > 0:
			ans += pref[num - 1]
		final.append(ans)
	print(*final)


def nok(a, b):
	x = a
	while x % b != 0:
		x += a
	return x
rw = int(input())
for weqeq in range(rw):
	(a, b, q) = map(int, input().split())
	lr = [[] for i in range(q)]
	for i in range(q):
		(x, y) = map(int, input().split())
		lr[i].append(x)
		lr[i].append(y)
	abmod = [0]
	bamod = [0]
	c = nok(a, b)
	pref = [0] * c
	for i in range(1, c):
		abmod.append(i % a % b)
		bamod.append(i % b % a)
		if abmod[i] != bamod[i]:
			pref[i] = pref[i - 1] + 1
		else:
			pref[i] = pref[i - 1]
	for i in lr:
		x = (i[0] - 1) % c
		y = i[1] % c
		xp = pref[x] + pref[c - 1] * ((i[0] - 1) // c)
		yp = pref[y] + pref[c - 1] * (i[1] // c)
		n = yp - xp
		print(n, end=' ')
	print('')


import sys
readline = sys.stdin.readline

def calc(x):
	D = len(table)
	J = table[-1]
	return x // D * J + table[x % D]
Q = int(readline())
Ans = [None] * Q
for qu in range(Q):
	(A, B, T) = map(int, readline().split())
	table = [0] * (A * B)
	for i in range(1, A * B):
		if i % A % B != i % B % A:
			table[i] = 1 + table[i - 1]
		else:
			table[i] = table[i - 1]
	ans = [None] * T
	for t in range(T):
		(l, r) = map(int, readline().split())
		l -= 1
		ans[t] = calc(r) - calc(l)
	Ans[qu] = ' '.join(map(str, ans))
print('\n'.join(map(str, Ans)))


import sys
input = sys.stdin.readline
import math

def solve(n, lc, a, b):
	x = n // lc
	ans = 0
	if x > 0:
		ans += (n // lc - 1) * max(a, b)
		ans += min(n % lc + 1, max(a, b))
	ans += min(max(a, b) - 1, n)
	return ans

def main():
	t = int(input())
	for _ in range(t):
		(A, B, Q) = [int(x) for x in input().split()]
		LR = [[int(x) for x in input().split()] for _ in range(Q)]
		ans = []
		lc = A * B // math.gcd(A, B)
		for (l, r) in LR:
			rn = solve(r, lc, A, B)
			ln = solve(l - 1, lc, A, B)
			ans.append(r - l + 1 - (rn - ln))
		print(*ans)
main()


def count(f, i, n):
	return f[-1] * (i // n) + f[i % n]
for _ in range(int(input())):
	(a, b, q) = [int(x) for x in input().split()]
	f = [0]
	n = a * b
	for i in range(1, n):
		f.append(f[i - 1])
		if i % a % b != i % b % a:
			f[i] = f[i] + 1
	res = []
	for _ in range(q):
		(l, r) = [int(x) for x in input().split()]
		res.append(count(f, r, n) - count(f, l - 1, n))
	print(' '.join([str(x) for x in res]))


import sys
input = sys.stdin.buffer.readline
import math

def lcm_base(x, y):
	return x * y // math.gcd(x, y)
from itertools import accumulate
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	L = lcm_base(a, b)
	A = [0] * L
	for x in range(1, L):
		if x % a % b != x % b % a:
			A[x - 1] = 1
	B = [0] + A
	B = list(accumulate(B))
	p = B[-1]
	ans = [-1] * q
	for i in range(q):
		(l, r) = map(int, input().split())
		(ql, rl) = divmod(l - 1, L)
		(qr, rr) = divmod(r, L)
		ans[i] = p * qr + B[rr] - (p * ql + B[rl])
	print(*ans)


from sys import stdin, stdout

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
t = int(input())
for i in range(t):
	(a, b, q) = list(map(int, stdin.readline().split()))
	d = int(lcm(a, b))
	list1 = []
	maxi = max(a, b)
	for i in range(q):
		(l, r) = list(map(int, stdin.readline().split()))
		if a == b or d == a or d == b:
			list1.append(0)
		elif r <= maxi or r - l <= maxi:
			cnt = 0
			for i in range(l, r + 1):
				if i % a % b != i % b % a:
					cnt += 1
			list1.append(cnt)
		else:
			rem1 = l % d
			quo1 = l // d
			rem2 = r % d
			quo2 = r // d
			sum1 = 0
			if rem1 <= maxi:
				sum1 = sum1 + maxi - rem1
			if rem2 < maxi:
				sum1 += (quo2 - quo1 - 1) * maxi
				sum1 += rem2 + 1
			else:
				sum1 += (quo2 - quo1) * maxi
			sum2 = r - l + 1
			list1.append(sum2 - sum1)
	for i in list1:
		print(i, end=' ')
	print()


import sys
input = sys.stdin.readline
from math import gcd
from itertools import accumulate
t = int(input())
for tests in range(t):
	(a, b, q) = map(int, input().split())
	LCM = a * b // gcd(a, b)
	LIST = [0] * LCM
	for i in range(LCM):
		if i % a % b != i % b % a:
			LIST[i] += 1
	S = [0] + list(accumulate(LIST))
	for queries in range(q):
		(l, r) = map(int, input().split())
		(lq, lr) = divmod(l, LCM)
		(rq, rr) = divmod(r, LCM)
		print(S[-1] * (rq - lq) - S[lr] + S[rr + 1], end=' ')
	print()


t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	ans = [0 for j in range(a * b + 1)]
	for j in range(1, a * b + 1):
		if j % a % b != j % b % a:
			ans[j] = 1
	for j in range(1, a * b + 1):
		ans[j] += ans[j - 1]
	for j in range(q):
		(l, r) = map(int, input().split())
		if r // (a * b) == l // (a * b):
			lll = l % (a * b)
			if lll == 0:
				lll = 1
			print(ans[r % (a * b)] - ans[lll - 1], end=' ')
			continue
		ll = (l + a * b - 1) // (a * b)
		rr = r // (a * b)
		answ = ans[-1] * max(0, rr - ll)
		lll = l % (a * b)
		if lll == 0:
			lll = a * b
		answ += ans[a * b - 1] - ans[lll - 1]
		answ += ans[r % (a * b)]
		print(answ, end=' ')
	print(' ')


import sys
from math import gcd
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	(a, b, q) = map(int, input().split())
	c = a * b // gcd(a, b)
	f = 0
	d = [0]
	for i in range(c):
		if i % a % b != i % b % a:
			f += 1
		d.append(f)
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split())
		nl = l - l % c
		nr = r + (c - r % c) - 1
		p = (nr - nl + 1) // c
		ff = 0
		tmp = p * f
		tmp -= d[l % c] + d[-1] - d[r % c + 1]
		ans.append(tmp)
	print(*ans)


import sys

def input():
	return sys.stdin.readline()[:-1]
t = int(input())
for _ in range(t):
	(a, b, q) = list(map(int, input().split()))
	ans = [0 for _ in range(q)]
	res = [0 for _ in range(a * b)]
	for i in range(1, a * b):
		if i % a % b != i % b % a:
			res[i] = res[i - 1] + 1
		else:
			res[i] = res[i - 1]

	def func(x):
		(quo, mod) = (x // (a * b), x % (a * b))
		return quo * res[-1] + res[mod]
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		ans[i] = func(r) - func(l - 1)
	print(*ans)


from __future__ import division, print_function
MOD = 998244353
mod = 10 ** 9 + 7
import sys
from itertools import repeat
from heapq import heappop, heapify, heappush
from collections import Counter, defaultdict, deque

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x * y // GCD(x, y)

def main():

	def solve(n):
		LCM = lcm(a, b)
		till = n // LCM
		ans = till * (LCM - b)
		ans += max(n - till * LCM - b + 1, 0)
		return ans
	T = int(input())
	while T:
		(a, b, q) = get_ints()
		(a, b) = (min(a, b), max(a, b))
		ans = []
		while q:
			(l, r) = get_ints()
			ans.append(solve(r) - solve(l - 1))
			q -= 1
		print(*ans)
		T -= 1
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import math

def count(sumg, n, r):
	res = sumg[n - 1] * (r // n)
	res += sumg[r % n]
	return res

def main():
	input = sys.stdin.readline
	t = int(input())
	while t > 0:
		t -= 1
		(a, b, q) = (int(i) for i in input().split())
		n = a * b
		sumg = [0] * n
		for i in range(1, n):
			sumg[i] += sumg[i - 1]
			if i % a % b != i % b % a:
				sumg[i] += 1
		while q > 0:
			q -= 1
			(l, r) = (int(i) for i in input().split())
			res = count(sumg, n, r) - count(sumg, n, l - 1)
			if q:
				print(res, end=' ')
			else:
				print(res)
main()


import math

def check(arr, a, b):
	d = []
	ll = a * b // math.gcd(a, b)
	for i in range(1, ll + 1):
		if i % a % b != i % b % a:
			d.append(1)
		else:
			d.append(0)
	for i in range(1, len(d)):
		if d[i] == 1:
			d[i] = d[i - 1] + 1
		else:
			d[i] = d[i - 1]
	result = []
	last = d[-1]
	for (l, r) in arr:
		p = 1
		q = 1
		kk = last * (r // ll - (l - 1) // ll)
		l -= 1
		r = r % ll
		if r == 0:
			p = 0
		else:
			r -= 1
		l = l % ll
		if l == 0:
			q = 0
		else:
			l -= 1
		result.append(p * d[r] - q * d[l] + kk)
	return result
t = int(input())
while t:
	(a, b, q) = list(map(int, input().split()))
	arr = []
	for i in range(q):
		arr.append(list(map(int, input().split())))
	result = check(arr, a, b)
	for i in result:
		print(i, end=' ')
	print()
	t -= 1


def check_root(n, d):
	return n * n + 2 * n + 1 >= 4 * d

def mod_expo(n, p, m):
	result = 1
	while p != 0:
		if p % 2 == 1:
			result = result * n % m
		p //= 2
		n = n * n % m
	return result

def find_product(n, m, arr):
	product = 1
	if n <= m:
		for i in range(n):
			for j in range(i + 1, n):
				product = product * abs(arr[i] - arr[j]) % m
		return product
	return 0

def find_subarrays(n, k, word):
	curr_sum = 0
	count = 0
	mp = {}
	for i in range(n):
		curr_sum += int(word[i])
		if curr_sum not in mp:
			mp[curr_sum] = 0
		mp[curr_sum] += 1
		if curr_sum == k:
			count += 1
		elif curr_sum - k in mp:
			count += mp[curr_sum - k]
	return count

def construct(a, b):
	unequal = []
	for i in range(a * b):
		unequal.append(False)
	for i in range(a * b):
		if i % a % b != i % b % a:
			unequal[i] = True
	return unequal

def __init__(a, b):
	pref_sum = []
	unequal = construct(a, b)
	pref_sum.append(int(unequal[0]))
	for i in range(1, a * b):
		pref_sum.append(pref_sum[i - 1] + int(unequal[i]))
	return pref_sum

def get_count(a, b, n, pref_sum):
	total = n // (a * b) * pref_sum[a * b - 1] + pref_sum[n % (a * b)]
	return total
t = 1
t = int(input())
while t:
	t = t - 1
	l = list(map(int, input().strip().split()))[:3]
	a = int(l[0])
	b = int(l[1])
	m = int(l[2])
	ans = []
	pref_sum = __init__(a, b)
	for i in range(m):
		(x, y) = map(int, input().split())
		ans.append(get_count(a, b, y, pref_sum) - get_count(a, b, x - 1, pref_sum))
	print(*ans, sep=' ')


from math import gcd
from itertools import accumulate
T = int(input().strip())
for t in range(T):
	(a, b, q) = list(map(int, input().split()))
	V = a * b // gcd(a, b)
	pr = [0 if x % a % b == x % b % a else 1 for x in range(V)]
	spr = list(accumulate(pr))
	totalv = sum(pr)
	rout = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		L = l // V
		R = r // V
		res = totalv * (R - L)
		res -= spr[l % V] - pr[l % V]
		res += spr[r % V]
		rout.append(res)
	print(*rout)


from sys import stdin, gettrace
from math import gcd
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		(a, b, q) = map(int, input().split())
		lcm = a * b // gcd(a, b)
		res = []
		mx = max(a, b)
		for _ in range(q):
			(l, r) = map(int, input().split())
			left = max(0, mx - l % lcm)
			right = max(0, mx - (r + 1) % lcm)
			res.append(r + 1 - l - ((r + 1) // lcm - l // lcm) * mx - left + right)
		print(' '.join(map(str, res)))
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys
import math

def getValue(x, maxm, lcm):
	ans = x // lcm * (lcm - maxm) + max(0, x % lcm - maxm + 1)
	return ans
for _ in range(int(sys.stdin.readline())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // math.gcd(a, b)
	ans = []
	maxm = max(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		t1 = getValue(r, maxm, lcm)
		t2 = getValue(l - 1, maxm, lcm)
		ans.append(t1 - t2)
	print(' '.join(map(str, ans)))


import io, os
import sys
from atexit import register
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.stdout = io.BytesIO()
register(lambda : os.write(1, sys.stdout.getvalue()))
tokens = []
tokens_next = 0

def next_str():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = input().split()
		tokens_next = 0
	tokens_next += 1
	return tokens[tokens_next - 1]

def nextInt():
	return int(next_str())

def gcd(a, b):
	while b > 0:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def calcIntersection(interval1, interval2):
	if interval1 > interval2:
		(interval1, interval2) = (interval2, interval1)
	if interval1[1] < interval2[0]:
		return 0
	first = max(interval1[0], interval2[0])
	second = min(interval1[1], interval2[1])
	return second - first + 1

def myRangeLen(r):
	return (r.stop - r.start - 1) // r.step + 1
T = nextInt()
for _ in range(T):
	a = nextInt()
	b = nextInt()
	if a > b:
		(a, b) = (b, a)
	q = nextInt()
	for i in range(q):
		l = nextInt()
		r = nextInt()
		n = lcm(a, b)
		allRelevantIntervals = range(l - l % n, r + 1, n)
		res = r - l + 1
		if allRelevantIntervals and allRelevantIntervals[0] < l:
			cur = allRelevantIntervals[0]
			res -= calcIntersection((cur, cur + b - 1), (l, r))
			allRelevantIntervals = allRelevantIntervals[1:]
		if allRelevantIntervals and allRelevantIntervals[-1] + b - 1 > r:
			cur = allRelevantIntervals[-1]
			res -= calcIntersection((cur, cur + b - 1), (l, r))
			allRelevantIntervals = allRelevantIntervals[:-1]
		if allRelevantIntervals:
			res -= myRangeLen(allRelevantIntervals) * b
		sys.stdout.write((str(res) + ' ').encode())
	sys.stdout.write('\n'.encode())


t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	count = 0
	cum = []
	for i in range(0, a * b):
		if i % a % b != i % b % a:
			count += 1
		cum.append(count)
	one_cycle_total = cum[a * b - 1]
	for _ in range(q):
		(l, r) = map(int, input().split())
		complete_cycles = r // (a * b)
		extra_right_part = r % (a * b)
		answer = one_cycle_total * complete_cycles + cum[extra_right_part]
		complete_cycles = (l - 1) // (a * b)
		extra_right_part = (l - 1) % (a * b)
		answer2 = one_cycle_total * complete_cycles + cum[extra_right_part]
		print(answer - answer2)


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b // gcd(a, b)

def find_numbers(l, r, c, a, b):
	least_multiple = l - l % c
	num = (r - least_multiple) // c
	final_multiple = least_multiple + c * num
	return max(b - l % c, 0) + (num - 1) * b + min(b, r - final_multiple + 1)
for i in range(int(input())):
	(a, b, q) = map(int, input().split())
	for j in range(q):
		(l, r) = map(int, input().split())
		if a == b:
			print('0', end=' ')
		elif r < max(a, b):
			print('0', end=' ')
		elif l < max(a, b):
			ans = find_numbers(max(a, b), r, lcm(a, b), min(a, b), max(a, b))
			print(int(r - max(a, b) + 1 - ans), end=' ')
		else:
			ans = find_numbers(l, r, lcm(a, b), min(a, b), max(a, b))
			print(int(r - l + 1 - ans), end=' ')
	print()


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s

def invr():
	return map(int, input().split())

def from_file(f):
	return f.readline

def solve(l, r, a, b, lcm, c_lcm, dp: list):
	l_count = l // lcm
	r_count = r // lcm
	tmp = (r_count - l_count) * c_lcm
	left = dp[l % lcm - 1] if l % lcm > 0 else 0
	right = dp[r % lcm]
	result = tmp - left + right
	return r - l + 1 - result

def get_lcm(a, b):

	def get_gcd(x, y):
		if y > x:
			(x, y) = (y, x)
		r = x % y
		if r == 0:
			return y
		return get_gcd(r, y)
	return a * b // get_gcd(a, b)

def count_interval(l, r, a, b, dp):
	count = 0
	while l <= r:
		if l % a % b == l % b % a:
			count += 1
		dp[l] = count
		l += 1
	return count
t = inp()
for _ in range(t):
	(a, b, q) = invr()
	result = []
	lcm = get_lcm(a, b)
	dp = [0] * lcm
	c_lcm = count_interval(0, lcm - 1, a, b, dp)
	for _ in range(q):
		(l, r) = invr()
		res = solve(l, r, a, b, lcm, c_lcm, dp)
		result.append(str(res))
	print(' '.join(result))


def query(p, div, prefix_sum):
	return p // div * prefix_sum[div] + prefix_sum[p % div]

def solve(a, b, queries):
	prefix_sum = [0]
	for i in range(1, a * b + 1):
		curr = 1 if i % a % b != i % b % a else 0
		prefix_sum.append(prefix_sum[i - 1] + curr)
	result = []
	for q in queries:
		result.append(query(q[1], a * b, prefix_sum) - query(q[0] - 1, a * b, prefix_sum))
	return result
for i in range(int(input())):
	(a, b, q) = map(int, input().split())
	queries = [[int(x) for x in input().split()] for y in range(q)]
	print(' '.join(map(str, solve(a, b, queries))))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

def main():
	for _ in range(N()):
		(a, b, q) = RL()
		rec = []

		def c(num, a, b):
			return (num % a % b, num % b % a)
		i = 0
		while i < a * b:
			now = c(i, a, b)
			rec.append(now)
			i += 1
		sm = [0]
		als = 0
		for (a, b) in rec:
			if a != b:
				als += 1
				sm.append(sm[-1] + 1)
			else:
				sm.append(sm[-1])
		sm = sm[1:]
		ret = []

		def c(x):
			le = len(sm)
			s = sm[-1]
			return x // le * s + sm[x % le]
		for _ in range(q):
			(l, r) = RL()
			ret.append(c(r) - c(l - 1))
		print(' '.join([str(i) for i in ret]))
main()


from math import factorial as fac
from collections import defaultdict
from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
sys.setrecursionlimit(10 ** 6)

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
from collections import deque, defaultdict
import heapq

def bfs(V, E):
	s = set()
	print('\n'.join(map(str, E.items())))
	cnt = 0
	for v in V:
		if v in s:
			continue
		q = deque([])
		s.add(v)
		q.append(v)
		while len(q) > 0:
			x = q.popleft()
			for e in E[x]:
				if e not in s:
					s.add(e)
					q.append(e)
			print(q)
		cnt += 1
	return cnt
from typing import List, Tuple

def regular_bs(L, x, l=None, r=None):
	l = 0 if l == None else l
	r = len(L) if r == None else r
	while r - l > 1:
		m = (r + l) // 2
		l = m if L[m] <= x else l
		r = m if L[m] > x else r
	cnt = 0
	return l

def binary_count_smaller(L, x):
	l = 0
	if x < L[0]:
		return 0
	r = len(L)
	while r - l > 1:
		mid = (l + r) // 2
		if L[mid] < x:
			l = mid
		else:
			r = mid
	return l + 1

def binary_count_larger(L, x):
	l = 0
	if x > L[-1]:
		return 0
	r = len(L)
	while r - l > 1:
		mid = (l + r) // 2
		if L[mid] <= x:
			l = mid
		else:
			r = mid
	return len(L) - (r - 1)
from itertools import product

def preprocess(A):
	dp = [0]
	for x in A:
		dp.append(dp[-1] + (1 if not x else 0))
	return dp

def solution(a, b, Q, q):
	res = []
	A = []
	m = a * b
	for i in range(m):
		A.append(i % a % b == i % b % a)
	dp = preprocess(A)
	cnt = dp[m]
	for (l, r) in Q:
		total_cnt = 0
		length = r - l + 1
		total_cnt += length // m * cnt
		remaining = length - m * (length // m)
		l_m = l % m
		first_part = m - l_m
		if remaining > first_part:
			total_cnt += dp[m] - dp[l_m]
			total_cnt += dp[remaining - first_part]
		else:
			total_cnt += dp[l_m + remaining] - dp[l_m]
		res.append(str(total_cnt))
	return ' '.join(res)

def main():
	T = read_int()
	for i in range(T):
		(a, b, q) = read_int_array()
		Q = []
		for j in range(q):
			Q.append(read_int_array())
		print(solution(a, b, Q, q))
main()


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	(a, b, q) = R()
	lm = lcm(a, b)
	m = max(a, b)
	for i in range(q):
		(l, r) = R()
		u = r // lm * lm
		d = l // lm * lm
		if u == d:
			d = u
			u += m - 1
			if u >= r:
				P(str(max(l, d) - l) + ' ')
			else:
				P(str(min(r - l + 1, r - u)) + ' ')
		else:
			d = max(l - 1, m - 1 + d)
			if u + m <= r:
				P(str(r - d - (r // lm - l // lm) * m) + ' ')
			else:
				P(str(u - 1 - d - (r // lm - l // lm - 1) * m) + ' ')
	P('\n')


import sys, math
input = sys.stdin.buffer.readline

def read():
	return map(int, input().split())

def solve(x, b, lcm):
	(d, r) = divmod(x, lcm)
	return d * (lcm - b) + max(0, r - b + 1)
for _ in range(int(input())):
	(a, b, q) = read()
	if a > b:
		(a, b) = (b, a)
	g = math.gcd(a, b)
	lcm = a * b // g
	ans = []
	for i in range(q):
		(l, r) = read()
		ans.append(solve(r, b, lcm) - solve(l - 1, b, lcm))
	print(*ans)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
from bisect import bisect
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def solve():
	for _ in range(int(input())):
		(a, b, q) = read()
		_lcm = a * b // gcd(a, b)
		b = max(a, b)
		ans = []
		for i in range(q):
			(l, r) = read()
			l -= 1
			left = l - (b - 1 + (l // _lcm - 1) * b + min(b, l % _lcm + 1))
			right = r - (b - 1 + (r // _lcm - 1) * b + min(b, r % _lcm + 1))
			if r != l:
				ans.append(right - left)
			else:
				ans.append(0)
		print(*ans)
solve()


from math import ceil, floor

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	(a, b) = (max(a, b), min(a, b))
	g = gcd(a, b)
	lcm = a * b // g
	for _ in range(q):
		(l, r) = map(int, input().split())
		totr = (r // lcm + 1) * a
		if r % lcm < a - 1:
			totr -= a - 1 - r % lcm
		l -= 1
		totl = (l // lcm + 1) * a
		if l % lcm < a - 1:
			totl -= a - 1 - l % lcm
		tot = totr - totl
		print(r - l - tot, end=' ')
	print()


from math import *
for zz in range(int(input())):
	(a, b, q) = map(int, input().split())
	x = max(a, b)
	y = min(a, b)
	a = x
	b = y
	if a == b:
		for i in range(q):
			input()
		print('0 ' * q)
	else:
		res = []
		for xx in range(q):
			(l, r) = map(int, input().split())
			ans = r - l + 1
			cb = a * b // gcd(a, b)
			lb = l // cb
			if l % cb > 0:
				lb += 1
			ub = (r - a + 1) // cb
			ans -= a * max(0, ub - lb + 1)
			ans -= max(0, (lb - 1) * cb + a - 1 - max((lb - 1) * cb, l) + 1)
			if ub - lb > -2:
				ans -= max(0, min(r, (ub + 1) * cb + a - 1) - (ub + 1) * cb + 1)
			x = max(ans, 0)
			res.append(x)
		print(*res)


def swap(a, b):
	temp = a
	a = b
	b = temp
for _ in range(int(input())):
	(a, b, q) = [int(i) for i in input().split()]
	if a > b:
		swap(a, b)
	pre = [0 for i in range(a * b + 2)]
	for i in range(1, a * b):
		if i % a % b != i % b % a:
			pre[i] = pre[i - 1] + 1
		else:
			pre[i] = pre[i - 1]
	for i in range(q):
		(l, r) = [int(i) for i in input().split()]
		l_gr = l // (a * b)
		r_gr = r // (a * b)
		ans = (r_gr - l_gr + 1) * pre[a * b - 1]
		ans = ans - pre[a * b - 1] + pre[r % (a * b)]
		if l % (a * b) != 0:
			ans = ans - pre[l % (a * b) - 1]
		print(ans)


class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	next = [1] * (a * b)
	bruh = 0
	next[0] = 0
	for i in range(1, a * b):
		if i % a % b == i % b % a:
			next[i] = 0
		else:
			bruh += 1
			continue
	for i in range(a * b):
		next += [next[i]]
	arr = [0] * q
	st = SegmentTree(next)
	for __ in range(q):
		(x, y) = map(int, input().split())
		start = x % (a * b)
		move = (y - x + 1) % (a * b)
		atleast = (y - x + 1) // (a * b)
		ans = atleast * bruh
		ans += st.query(start, start + move - 1)
		arr[__] = ans
	print(' '.join((str(k) for k in arr)))


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
t = int(input())
for _ in range(t):
	(a, b, q) = [int(i) for i in input().split()]
	if a == b:
		for s1 in range(q):
			(l, r) = [int(i) for i in input().split()]
			print(0, end=' ')
	else:
		for s1 in range(q):
			(l, r) = [int(i) for i in input().split()]
			rem = max(a, b)
			den = a * b // gcd(a, b)
			start = l // den
			end = r // den
			tot = (end - start) * rem
			tot += 1
			end_val = end * den
			end_diff = r - end_val
			tot += min(rem - 1, end_diff)
			start_val = start * den
			start_diff = l - start_val
			tot -= min(rem, start_diff)
			print(r - l + 1 - tot, end=' ')
	print()


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def getStr():
	return input().strip()

def nokAB(a, b):
	m = a * b
	while a != 0 and b != 0:
		if a > b:
			a %= b
		else:
			b %= a
	return m // (a + b)
tt = getInt()
for _ in range(tt):
	(a, b, q) = getVars()
	nok = nokAB(a, b)
	arr = []
	for i in range(q):
		(l, r) = getVars()
		res = max(l // nok * nok + max(a, b) - l, 0)
		res += (r // nok - l // nok) * max(a, b)
		res -= max(r // nok * nok + max(a, b) - r - 1, 0)
		arr.append(r - l + 1 - res)
	print(*arr)


import sys
import math
t = int(sys.stdin.readline())
for _ in range(t):
	(x, y, q) = map(int, sys.stdin.readline().split())
	a = min(x, y)
	b = max(x, y)
	lcm = a * b // math.gcd(a, b)
	res = []
	for i in range(q):
		(l, r) = map(int, sys.stdin.readline().split())
		A = l // lcm
		B = l % lcm
		left = A * b + min(b - 1, B) + 1
		C = r // lcm
		D = r % lcm
		right = C * b + min(b - 1, D) + 1
		left -= 1
		right -= 1
		if 0 <= l % lcm <= b - 1:
			ans = right - left + 1
		else:
			ans = right - left
		ans = r - l + 1 - ans
		res.append(ans)
	print(*res)


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)

def prefix(l, a, b):
	if l == 0:
		return 0
	res = max(l - max(a, b) + 1, 0)
	num_period = l // lcm(a, b)
	if num_period > 0 and lcm(a, b) * num_period + max(a, b) - 1 > l:
		res -= l - lcm(a, b) * num_period + 1
		res -= max(num_period - 1, 0) * max(a, b)
	else:
		res -= max(num_period, 0) * max(a, b)
	return res

def brute_force(l, r, a, b):
	res = 0
	for i in range(l, r + 1):
		if i % a % b != i % b % a:
			print(i)
			res += 1
	return res
t = int(input())
for _ in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	for _ in range(q):
		(l, r) = [int(x) for x in input().split()]
		print(prefix(r, a, b) - prefix(l - 1, a, b), end=' ')
	print()


import sys

def input():
	return sys.stdin.readline()[:-1]

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	count = [0] * lcm
	for i in range(lcm):
		if i % b % a != i % a % b:
			count[i] = 1
		count[i] += count[i - 1]
	out = [0] * q

	def func(x):
		(quo, mod) = (x // lcm, x % lcm)
		return quo * count[lcm - 1] + count[mod]
	for j in range(q):
		(l, r) = map(int, input().split())
		out[j] = func(r) - func(l - 1)
	print(*out)


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
from math import *
for _ in range(int(input())):
	(a, b, q) = mapi()
	lcm = a * b // gcd(a, b)
	ans = []
	b = max(a, b)
	for qe in range(q):
		(x, y) = mapi()
		res = 0
		res = y // lcm * (lcm - b)
		rem = y % lcm
		if rem >= b:
			res += rem - b + 1
		x -= 1
		tmp = x // lcm * (lcm - b)
		rem = x % lcm
		if rem >= b:
			tmp += rem - b + 1
		res -= tmp
		ans.append(res)
	print(*ans)


def lengh(l, r):
	return max(0, r - l + 1)

def gcd(a, b):
	if a < b:
		(a, b) = (b, a)
	while a % b != 0:
		(a, b) = (b, a % b)
	return b

def lcm(a, b):
	return a * b // gcd(a, b)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	y = max(a, b) - 1
	res = []
	for i in range(q):
		(l, r) = map(int, input().split())
		low = l // lcm(a, b) * lcm(a, b)
		high = r // lcm(a, b) * lcm(a, b)
		j = r - l + 1
		if low == high:
			if low < l:
				if low + y >= r:
					res.append(0)
				else:
					res.append(j - lengh(l, low + y))
			elif low + y > r:
				res.append(lengh(l, low - 1))
			else:
				res.append(j - lengh(low, low + y))
		else:
			answer = j
			if low < l:
				answer -= lengh(l, low + y)
			else:
				answer -= y + 1
			if high + y > r:
				answer -= lengh(high, r)
			else:
				answer -= y + 1
			c = (high - low) // lcm(a, b) - 1
			answer -= (y + 1) * c
			res.append(answer)
	for i in range(q):
		print(res[i], end=' ')
	print()


tc = int(input())

def build(a, b):
	m = [0] * (a * b)
	for i in range(a * b):
		if i % a % b != i % b % a:
			m[i] = 1
	for i in range(1, len(m)):
		m[i] += m[i - 1]
	return m

def solve(l, m):
	s = m[-1] * (l // len(m))
	return s + m[l % len(m)]
for _ in range(tc):
	(a, b, q) = map(int, input().split())
	m = build(a, b)
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		ans.append(solve(r, m) - solve(l - 1, m))
	print(' '.join(map(str, ans)))


from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def powmod(a, b):
	a %= mod
	if a == 0:
		return 0
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % mod
		a = a * a % mod
		b >>= 1
	return res

def func(x, y, c):
	c1 = x // y
	ans = c1 * c
	x %= y
	x += 1
	if x < c:
		ans += x - c
	return ans

def main():
	for _ in range(ii()):
		(a, b, q) = mi()
		if b > a:
			(a, b) = (b, a)
		x = a
		y = a * b // gcd(a, b)
		for i in range(q):
			(l, r) = mi()
			l -= 1
			if a == b or r < a:
				print('0', end=' ')
				continue
			ans = r - l - (func(r, y, x) - func(l, y, x))
			print(ans, end=' ')
		print()
main()


from sys import stdin, stdout
T = int(stdin.readline())
for _ in range(T):
	[a, b, q] = [int(x) for x in stdin.readline().split()]
	maxAB = max(a, b)
	aGCD = a
	bGCD = b
	while aGCD != 0 and bGCD != 0:
		if aGCD > bGCD:
			aGCD %= bGCD
		else:
			bGCD %= aGCD
	gcd = aGCD + bGCD
	nok = a * b // gcd
	batch = nok - maxAB
	resArr = []
	for __ in range(q):
		[l, r] = [int(x) for x in stdin.readline().split()]
		res = 0
		if a == 1 and b == 1:
			res = 0
		else:
			res += (r - l) // nok * batch
			if r % nok >= l % nok:
				if r % nok >= maxAB:
					if l % nok >= maxAB:
						res += r % nok - l % nok + 1
					else:
						res += r % nok - maxAB + 1
			else:
				if r % nok >= maxAB:
					res += r % nok - maxAB + 1
				res += min(batch, nok - l % nok)
		resArr.append(str(res))
	stdout.write(' '.join(resArr) + '\n')


t = int(input())
for x in range(t):
	(a, b, q) = map(int, input().split())
	pre = [0]
	for y in range(1, a * b):
		pre.append(pre[y - 1])
		if y % a % b != y % b % a:
			pre[y] += 1
	List = []
	for Q in range(q):
		(l, r) = map(int, input().split())
		low = (l - 1) // (a * b) * pre[len(pre) - 1] + pre[(l - 1) % (a * b)]
		high = r // (a * b) * pre[len(pre) - 1] + pre[r % (a * b)]
		List.append(high - low)
	print(*List)


def gcd(a, b):
	return gcd(b, a % b) if b != 0 else a
for t in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	cnt = 0
	till = [0 for _ in range(lcm + 1)]
	for x in range(lcm):
		if x % a % b != x % b % a:
			cnt += 1
		till[x] = cnt
	till[lcm] = 0
	for i in range(q):
		(l, r) = map(int, input().split())
		n = (r - l) // lcm
		r -= n * lcm
		(l, r) = (l % lcm, r % lcm)
		ans = n * cnt + till[r] - till[l - 1]
		if l > r:
			ans += cnt
		print(ans, end=' ')
	print()


from itertools import accumulate

def f(i):
	global a, b, pre
	x = i // (a * b)
	y = i % (a * b)
	return x * pre[-1] + pre[y]
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	pre = [int(i % a % b != i % b % a) for i in range(a * b)]
	pre = list(accumulate(pre))
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split())
		ans.append(str(f(r) - f(l - 1)))
	print(' '.join(map(str, ans)))


def compute_hcf(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	lc = a * b // compute_hcf(a, b)
	ans = []
	for j in range(q):
		(l, r) = map(int, input().split())
		if r % lc < max(a, b):
			yes = max(a, b) * (-1 + r // lc) + r % lc + max(a, b)
		else:
			yes = max(a, b) * (r // lc) + max(a, b) - 1
		ans_r = r - yes
		if l % lc < max(a, b):
			yes = max(a, b) * (-1 + l // lc) + l % lc + max(a, b)
		else:
			yes = max(a, b) * (l // lc) + max(a, b) - 1
		ans_l = l - yes
		if l % a % b != l % b % a:
			ans.append(ans_r - ans_l + 1)
		else:
			ans.append(ans_r - ans_l)
	for j in range(q):
		if j != q - 1:
			print(ans[j], end=' ')
		else:
			print(ans[j])


from math import gcd, ceil

def work(z, lcm, v_nums):
	m = z // lcm
	m += 1
	temp = m * lcm
	tran = temp - v_nums
	ans = (m - 1) * v_nums
	if tran <= z:
		ans += z - tran + 1
	return ans
t = int(input())
for ll in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	v_nums = lcm - max(a, b)
	for llb in range(q):
		ans = 0
		(r, z) = map(int, input().split())
		ans += work(z, lcm, v_nums)
		ans -= work(r - 1, lcm, v_nums)
		print(ans, end=' ')
	print()


from math import gcd
t = int(input())

def find(arr, x, lcm):
	q = x // lcm
	r = x % lcm
	ans = arr[lcm] * q + arr[r]
	return ans
for _ in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	lcm = a * b // gcd(a, b)
	arr = [0]
	for i in range(1, lcm + 1):
		left = i % a % b
		right = i % b % a
		arr.append(arr[i - 1] + (0 if left == right else 1))
	res = []
	for _ in range(q):
		(l, r) = [int(x) for x in input().split()]
		ans = find(arr, r, lcm) - find(arr, l - 1, lcm)
		res.append(ans)
	print(*res)


from collections import defaultdict as dc
from heapq import *
import math
import bisect
import sys
from collections import deque as dq
from heapq import heapify, heappush, heappop
mod = 10 ** 9 + 7

def inp():
	p = int(input())
	return p

def line():
	p = list(map(int, input().split()))
	return p

def read_mat():
	n = inp()
	a = []
	for i in range(n):
		a.append(line())
	return a

def digit(n):
	s = str(n)
	p = 0
	for i in s:
		p += int(i) ** 2
	return p

def check(l, r):
	ans = 0
	c = 0
	for i in range(l, r + 1):
		if i % a % b != i % b % a:
			ans += 1
		else:
			c += 1
	return ans

def solve(l, r, a, b, p):
	if r < max(a, b):
		return 0
	if l == r:
		if l % a % b == l % b % a:
			return 0
		return 1
	x = max(p * (l // p + 1), 1)
	tot = r - l + 1
	y = max(p * (r // p), l)
	extra = 0
	for i in range(l, min(l + b, x, r + 1)):
		if i % a % b == i % b % a:
			extra += 1
		else:
			break
	if l != y:
		for i in range(y, min(r + 1, y + b), 1):
			if i % a % b == i % b % a:
				extra += 1
			else:
				break
	rem = (y - x) // p * b if y - x >= 0 else 0
	return tot - (rem + extra)
for _ in range(inp()):
	(a, b, q) = line()
	ans = []
	x = min(a, b)
	y = max(a, b)
	p = a * b // math.gcd(a, b)
	for i in range(q):
		(l, r) = line()
		t = solve(l, r, x, y, p)
		ans.append(t)
	print(*ans)


from math import *
from bisect import bisect_left, bisect_right
from queue import Queue
from sys import stdin, stdout
from collections import Counter
input = lambda : stdin.readline().strip()
fast_print = stdout.write
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	prefix = []
	cnt = 0
	for i in range(a * b):
		if i % b % a != i % a % b:
			cnt += 1
		prefix.append(cnt)
	for i in range(q):
		(l, r) = map(int, input().split())
		minicnt = 0
		if l % (a * b) != 0:
			minicnt += cnt - prefix[l % (a * b) - 1]
		else:
			minicnt += cnt
		minicnt += prefix[r % (a * b)]
		cnt_front = a * b - l % (a * b)
		cnt_back = r % (a * b) + 1
		if r - l + 1 <= a * b:
			if r - l + 1 == cnt_front + cnt_back:
				fast_print(str(minicnt) + ' ')
			elif l % (a * b) != 0:
				fast_print(str(prefix[r % (a * b)] - prefix[l % (a * b) - 1]) + ' ')
			else:
				fast_print(str(prefix[r % (a * b)]) + ' ')
		else:
			leftover = r - l + 1 - (cnt_front + cnt_back)
			fast_print(str(minicnt + cnt * (leftover // (a * b))) + ' ')
	fast_print('\n')


import sys
sys.setrecursionlimit(10 ** 6)

def gcd(a, b):
	while b != 0:
		r = b
		b = a % b
		a = r
	return a

def f(a, b, s, e):
	if a == b:
		return 0
	beishu = a * b // gcd(a, b)
	if a > b:
		(a, b) = (b, a)
	if s == 1:
		s = 0
	si = s // beishu
	if s % beishu != 0:
		si += 1
	start = si * beishu
	ei = e // beishu
	end = ei * beishu
	if start > e:
		start -= beishu
		start += b
		if e < start:
			return 0
		return e - max(start, s) + 1
	if end < s:
		end += b
		if e < end:
			return 0
		return e - max(end, s) + 1
	ans = 0
	ans += (beishu - b) * (ei - si)
	ne = start - 1
	start -= beishu
	start += b
	if e < start:
		pass
	else:
		ans += ne - max(start, s) + 1
	end += b
	if e < end:
		pass
	else:
		ans += e - end + 1
	return ans

def g(a, b, s, e):
	cnt = 0
	for i in range(s, e + 1):
		if i % a % b != i % b % a:
			cnt += 1
	if f(a, b, s, e) != cnt:
		print(a, b, s, e, f(a, b, s, e), cnt)
nofkase = int(input())
for kase in range(nofkase):
	(a, b, q) = [int(i) for i in input().split()]
	for i in range(q):
		(s, e) = [int(i) for i in input().split()]
		print(f(a, b, s, e), end=' ')
	print('')


r = lambda : map(int, input().split())

def PrefixSum(a, b):
	l = [0]
	for i in range(1, a * b + 1):
		t = l[-1]
		if i % a % b != i % b % a:
			t += 1
		l.append(t)
	return l

def Query(arr, l, n):
	return arr[n] * (l // n) + arr[l % n]
for _ in range(int(input())):
	(a, b, q) = r()
	arr = PrefixSum(a, b)
	n = a * b
	for _ in range(q):
		(l, m) = r()
		print(Query(arr, m, n) - Query(arr, l - 1, n))


from sys import stdin, stdout
import math, collections
mod = 998244353
for _ in range(int(stdin.readline())):
	(a, b, q) = map(int, stdin.readline().split())
	(a, b) = (max(a, b), min(a, b))
	c = a * b // math.gcd(a, b)
	for i in range(q):
		(l, r) = map(int, stdin.readline().split())
		t1 = l - l // c * c
		ans = r - l
		print(ans - (r // c - l // c + 1) * a + min(a, l % c) + max(0, a - r % c - 1) + 1)


from sys import stdin
input = stdin.readline

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def solve(l, r):
	value = r // x - (l - 1) // x
	upper = min(r + 1, x * ((l - 1) // x) + b)
	sub = max(0, upper - l)
	if value == 0:
		return r - l + 1 - sub
	sub += (value - 1) * b
	sub += min(r % x + 1, b)
	return r - l + 1 - sub

def answer():
	for query in range(q):
		(l, r) = map(int, input().split())
		print(solve(l, r), end=' ')
for T in range(int(input())):
	(a, b, q) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	x = lcm(a, b)
	answer()
	print()


import math
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	tmp = a * b // math.gcd(a, b)
	n = max(a, b)
	for i in range(q):
		ans = 0
		(l, r) = map(int, input().split())
		ans += r // tmp * n
		ans -= l // tmp * n
		ans = ans - max(0, n - r % tmp - 1) + max(0, n - l % tmp)
		print(r - l + 1 - ans, end=' ')
	print()


from collections import Counter, OrderedDict
from itertools import permutations as perm
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')

def getVal(x, a, b):
	return 1 if x % a % b != x % b % a else 0
(t,) = gil()
for _ in range(t):
	(a, b, q) = gil()
	(a, b) = (max(a, b), min(a, b))
	n = a * b
	base = [0] * n
	base[0] = getVal(0, a, b)
	for i in range(1, n):
		base[i] += base[i - 1] + getVal(i, a, b)
	base.append(0)
	for _ in range(q):
		(l, r) = gil()
		if a == b or r < a:
			print(0, end=' ')
		else:
			ans = 0
			ans += base[r % n]
			ans -= base[l % n - 1]
			ans += base[-2] * ((r - r % n - l + l % n + 1) // n)
			print(ans, end=' ')
	print()


def gcd(a, b):
	if a == 0:
		return b
	else:
		return gcd(b % a, a)

def go(a, b, n):
	lcmm = a * b // gcd(a, b)
	ans = n // lcmm * max(a, b)
	p = n // lcmm * lcmm
	ans += min(n - p + 1, max(a, b))
	return ans
t = int(input())
while t > 0:
	t -= 1
	s = input().split()
	a = int(s[0])
	b = int(s[1])
	q = int(s[2])
	list1 = []
	for i in range(0, q):
		p = input().split()
		l = int(p[0])
		r = int(p[1])
		list1.append(r - l + 1 - (go(a, b, r) - go(a, b, l - 1)))
		list1[i] = int(list1[i])
	for i in range(0, len(list1)):
		print(int(list1[i]), end=' ')
	print('', end='\n')


import sys
from math import log2, floor, ceil, sqrt, gcd
sys.setrecursionlimit(7 * 10 ** 4)
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 998244353

def no(n, b, lcm):
	ans = n
	div = n // lcm
	last = div * lcm
	if div != 0:
		ans -= (div - 1) * b
		ans -= min(n - lcm * div + 1, b)
	return ans - min(n, b - 1)
for _ in range(int(ri())):
	(a, b, q) = Ri()
	if a > b:
		(a, b) = (b, a)
	for qq in range(q):
		(l, r) = Ri()
		if l == r:
			if l % a % b != l % b % a:
				print('1', end=' ')
			else:
				print('0', end=' ')
		elif r < b:
			print('0', end=' ')
		elif b % a == 0:
			print('0', end=' ')
		else:
			minn = l
			maxx = r
			lcm = a * b // gcd(a, b)
			left = no(maxx, b, lcm)
			right = no(minn - 1, b, lcm)
			cnt = left - right
			print(cnt, end=' ')
	print()


def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)
T = int(input())
for _ in range(T):
	(a, b, q) = map(int, input().split())
	g = gcd(a, b)
	lcm = a * b // g
	cc = []
	for u in range(lcm):
		cc.append(1 if u % a % b != u % b % a else 0)
		if len(cc) > 1:
			cc[-1] += cc[-2]

	def get_count(u):
		return cc[-1] * (u // lcm) + cc[u % lcm]
	res = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		res.append(get_count(r) - get_count(l - 1))
	print(' '.join(map(str, res)))


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)

def solve2(a, b, x):
	if x < max(a, b):
		return 0
	sol = x + 1 - max(a, b)
	ll = lcm(a, b)
	sol -= x // ll * max(a, b)
	if x // ll * ll + max(a, b) - 1 >= x:
		sol += x // ll * ll + max(a, b) - 1 - x
	return sol

def solve(a, b, l, r):
	return solve2(a, b, r) - solve2(a, b, l - 1)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	sol = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		sol.append(solve(a, b, l, r))
	print(' '.join(map(str, sol)))


import math
t = int(input())
for _ in range(t):
	(a, b, q) = list(map(int, input().split()))
	lcm = a * b // math.gcd(a, b)
	count = 0
	count += lcm - max(a, b)
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		r += 1
		result = (r // lcm - l // lcm) * count
		result -= l % lcm - min(l % lcm, max(a, b))
		result += r % lcm - min(r % lcm, max(a, b))
		print(result, end=' ')
	print()


def compute_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def compute_lcm(x, y):
	lcm = x * y // compute_gcd(x, y)
	return lcm
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	l = []
	if a > b:
		(a, b) = (b, a)
	lcm = compute_lcm(a, b)
	ans = []
	for i in range(q):
		l.append(list(map(int, input().split())))
		quo = l[i][1] // lcm
		rem = l[i][1] % lcm
		val = lcm - b
		val2 = max(0, rem - b + 1)
		x = quo * val + val2
		quo = (l[i][0] - 1) // lcm
		rem = (l[i][0] - 1) % lcm
		val = lcm - b
		val2 = max(0, rem - b + 1)
		x -= quo * val + val2
		ans.append(x)
	print(*ans)


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b // gcd(a, b)

def getAns(equalcount, r, lcmm):
	basecount = r // lcmm
	baseans = basecount * equalcount
	rem = r % lcmm
	if rem >= equalcount - 1:
		baseans += equalcount - 1
	else:
		baseans += rem
	return baseans
t = int(input())
for hh in range(0, t):
	(a, b, q) = [int(x) for x in input().split()]
	a1 = []
	b1 = []
	for i in range(0, 201):
		a1.append(i)
		b1.append(i)
	mx = max(a, b)
	equalcount = 0
	for i in range(1, mx + 1):
		a1[i] = a1[i] % a % b
		b1[i] = b1[i] % b % a
		if a1[i] == b1[i]:
			equalcount += 1
	lcmm = lcm(a, b)
	equalcount += 1
	for i in range(0, q):
		(l, r) = [int(x) for x in input().split()]
		if a == b or a % b == 0 or b % a == 0:
			ans = 0
		else:
			second = getAns(equalcount, l - 1, lcmm)
			first = getAns(equalcount, r, lcmm)
			ans = r - first - (l - 1 - second)
		print(ans, end=' ')
	print()


def gcd(aa, bb):
	if bb == 0:
		return aa
	return gcd(bb, aa % bb)

def get_ans(x):
	y = x // c
	ans = y * max(a, b)
	next_num = y * c
	z = min(x, next_num + max(a, b) - 1)
	ans += z - next_num + 1
	return ans
t = int(input())
for _ in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	c = a * b // gcd(a, b)
	for i in range(q):
		(l, r) = [int(x) for x in input().split()]
		if a == b:
			print(0, end=' ')
		else:
			p = get_ans(r) - get_ans(l - 1)
			print(r - l + 1 - p, end=' ')
	print()


import math
line = input()
t = int(line)
for _ in range(t):
	line = input()
	(a, b, q) = (int(i) for i in line.split(' '))
	g = a * b // math.gcd(a, b)
	memo = [0] * g
	for i in range(1, g):
		if i % a % b != i % b % a:
			memo[i] = memo[i - 1] + 1
		else:
			memo[i] = memo[i - 1]
	for i in range(q):
		line = input()
		(l, r) = (int(i) for i in line.split(' '))
		l -= 1
		res = r // g * memo[-1] + memo[r % g]
		res -= l // g * memo[-1] + memo[l % g]
		print(res)


import os
import sys
from io import BytesIO, IOBase
import threading
from math import floor, ceil
from bisect import bisect_right
from collections import Counter
from math import gcd
mod = 10 ** 9 + 7

def main():
	for _ in range(int(input())):
		(a, b, q) = map(int, input().split())
		arr = [0] * (a * b + 1)
		for i in range(0, a * b + 1):
			if i % a % b != i % b % a:
				arr[i] = 1
		for i in range(1, a * b + 1):
			arr[i] += arr[i - 1]
		ans = []
		for i in range(q):
			(l, r) = map(int, input().split())
			t1 = arr[-1] * (r // (a * b)) + arr[r % (a * b)]
			l -= 1
			t2 = arr[-1] * (l // (a * b)) + arr[l % (a * b)]
			ans.append(t1 - t2)
		print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def outln(var):
	sys.stdout.write(str(var) + '\n')

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	(a, b, q) = sp()
	(a, b) = (min(a, b), max(a, b))
	answer = []
	lcm = a * b // gcd(a, b)
	mul = lcm - b
	for i in range(q):
		(left, right) = sp()
		left -= 1
		(t1, t2) = (left // lcm * mul, right // lcm * mul)
		if left % lcm != 0:
			t1 += max(0, left % lcm - b + 1)
		if right % lcm != 0:
			t2 += max(0, right % lcm - b + 1)
		answer.append(t2 - t1)
	print(*answer)


import sys
from collections import defaultdict as dd
input = sys.stdin.readline
mod = 1000000007

def power(x, y):
	res = 1
	while y > 0:
		if y & 1 == 1:
			res = res * x % m
		y = y >> 1
		x = x * x % m
	return res % m

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def solve(r, mx, lc):
	mulp = 0
	ul = int(r // lc) * lc
	le = mx - 1
	if ul + mx > r:
		le += r - ul + 1
	else:
		le += mx
	mulp = int(r // lc) - 1
	le += mulp * mx
	return r - le + 1

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lc = lcm(a, b)
	mx = max(a, b)
	f = 0
	if mx % min(a, b) == 0:
		f = 1
	for gf in range(q):
		c = 0
		(l, r) = map(int, input().split())
		if mx % min(a, b) == 0:
			ans = 0
		elif r < mx:
			ans = 0
		else:
			su1 = solve(r, mx, lc)
			su2 = solve(l - 1, mx, lc)
			ans = su1 - su2
		print(ans, end=' ')
	print()


t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	xs = [0] * (a * b)
	for x in range(a * b):
		if x % a % b != x % b % a:
			xs[x] = 1
	s = [0] * (a * b + 1)
	for j in range(a * b):
		s[j + 1] = xs[j] + s[j]
	for j in range(q):
		(l, r) = map(int, input().split())
		y = (l - 1) // (a * b) * s[a * b]
		z = r // (a * b) * s[a * b]
		y += s[(l - 1) % (a * b) + 1]
		z += s[r % (a * b) + 1]
		print(z - y, end=' ')
	print()


import sys, math
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
t = inp()

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def find_left(l, lc):
	(low, high) = (0, 10 ** 18 + 1)
	while low < high:
		mid = (low + high) // 2
		if mid * lc >= l:
			high = mid
		else:
			low = mid + 1
	return low * lc

def find_right(l, lc, r):
	(low, high) = (1, 10 ** 18 + 1)
	while low < high:
		mid = (low + high) // 2
		if l + mid * lc - 1 >= r:
			high = mid
		else:
			low = mid + 1
	return (low, l + low * lc - 1)
for _ in range(t):
	(a, b, q) = invr()
	ans = []
	ma = max(a, b)
	lc = lcm(a, b)
	for each in range(q):
		(l, r) = invr()
		left = find_left(l, lc)
		if left > r:
			ol = left - lc
			if ol + ma > r:
				ans.append(0)
			else:
				ans.append(r - max(ol + ma - 1, l - 1))
		else:
			(num, right) = find_right(left, lc, r)
			lremain = left - l
			if right > r:
				num -= 1
			lr = min(lremain, lc - ma)
			rr = 0 if right == r else max(0, r - (right - lc + ma))
			result = num * (lc - ma) + lr + rr
			ans.append(result)
	print(*ans)


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		(a, b, q) = map(int, input().split())
		g = math.gcd(a, b)
		(a, b) = (min(a, b), max(a, b))
		ans = 0
		if a == b:
			for i in range(q):
				(l, r) = map(int, input().split())
			ans = [0] * q
			print(*ans)
		else:
			k = a * b // g
			a = []
			for i in range(q):
				ans = 0
				(l, r) = map(int, input().split())
				t = l - r + 1
				ll = l // k * k
				rr = r // k * k
				ans = 0
				if ll == rr:
					ans += max(0, r - max(l - 1, ll + b - 1))
					a.append(ans)
				else:
					l = max(l, ll + b)
					if rr + b > r:
						r = rr - 1
					t = r - l + 1
					ans += t + ((l - 1) // k - r // k) * b
					a.append(ans)
			print(*a)


def gcd(a, b):
	(a, b) = (max(a, b), min(a, b))
	if b == 0:
		return a
	return gcd(b, a % b)

def f(a, b, x):
	g = a * b // gcd(a, b)
	(a, b) = (min(a, b), max(a, b))
	if g == b or x < b:
		return 0
	nn = (x - b) // g + 1
	if x - ((nn - 1) * g + b) >= g - b:
		return nn * (g - b)
	else:
		return (nn - 1) * (g - b) + x - ((nn - 1) * g + b) + 1

def ff(a, b, x):
	(a, b) = (min(a, b), max(a, b))
	ans = 0
	for i in range(x + 1):
		if i % a % b != i % b % a:
			ans += 1
	return ans
gans = []
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	ansi = []
	for i in range(q):
		(L, R) = map(int, input().split())
		ansi.append(f(a, b, R) - f(a, b, L - 1))
	gans.append(' '.join(map(str, ansi)))
print('\n'.join(gans))


import math
tll = int(input())
while tll > 0:
	tll -= 1
	(a, b, q) = map(int, input().split())
	while q > 0:
		q -= 1
		(l, r) = map(int, input().split())
		if a > b:
			(a, b) = (b, a)
		if l < b and r < b:
			print(0, sep=' ')
			continue
		ans = r - l + 1
		if l < b:
			ans -= b - l
		lcm = a * b // math.gcd(a, b)
		l = max(l, b)
		n1 = l // lcm
		n2 = r // lcm
		n = n2 - n1
		if l == n1 * lcm:
			n += 1
		ans -= n * b
		if n2 * lcm + b - 1 > r:
			ans += n2 * lcm + b - 1 - r
		if (n1 - 1) * lcm + b - 1 >= l:
			ans -= (n1 - 1) * lcm + b - l
		if n1 * lcm < l and n1 * lcm + b - 1 >= l:
			ans -= n1 * lcm + b - l
		print(ans, sep=' ')
	print()


def gcd(a, b):
	while a and b:
		if a > b:
			a %= b
		else:
			b %= a
	return a + b
t = int(input())
for ii in range(t):
	(a, b, q) = map(int, input().split())
	(a, b) = (max(a, b), min(a, b))
	nok = a * b // gcd(a, b)
	for j in range(q):
		(l, r) = map(int, input().split())
		if a % b == 0:
			print(0, end=' ')
		else:
			cnt = 0
			if l % nok < a:
				cnt += min(a - l % nok, r - l + 1)
			left = (l // nok + 1) * nok
			if r >= left and r % nok < a:
				cnt += r % nok + 1
				right = r // nok * nok - 1
			else:
				right = r
			if right >= left:
				cnt += (right // nok - left // nok + 1) * a
			print(r - l + 1 - cnt, end=' ')
	print()


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys
from itertools import accumulate

def gcd(x, y):
	if x < y:
		(x, y) = (y, x)
	if y == 0:
		return x
	return gcd(y, x % y)

def lcm(x, y):
	return x * y // gcd(x, y)

def main():
	t = int(input())
	for _ in range(t):
		(a, b, q) = map(int, input().split())
		lcm_ab = lcm(a, b)
		Cycle = [None] * lcm_ab
		for i in range(lcm_ab):
			Cycle[i] = int(i % a % b != i % b % a)
		Acc = [0] + list(accumulate(Cycle))
		Ans = []
		for _ in range(q):
			(l, r) = map(int, input().split())
			l_cycle = l // lcm_ab
			l_idx = l % lcm_ab
			r_cycle = r // lcm_ab
			r_idx = r % lcm_ab
			ans = Acc[-1] * (r_cycle - l_cycle) + Acc[r_idx + 1] - Acc[l_idx]
			Ans.append(ans)
		sys.stdout.write(' '.join(map(str, Ans)) + '\n')
main()


from math import gcd

def lcount(a, lcm, ma):
	lastcm = a // lcm * lcm
	count = (lcm - ma) * (a // lcm)
	count += max(0, a - lastcm - ma)
	return count
t = int(input())
for tt in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	ma = max(a, b)
	ans = []
	for qq in range(q):
		(l, r) = map(int, input().split())
		ans.append(lcount(r + 1, lcm, ma) - lcount(l, lcm, ma))
	print(*ans)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(a, b, q) = iia()
	lcm = a * b // gcd(a, b)
	res = []
	for i in range(q):
		(l, r) = iia()
		l -= 1
		right = r - r // lcm * max(a, b) - min(max(a, b), r % lcm + 1)
		left = l - l // lcm * max(a, b) - min(max(a, b), l % lcm + 1)
		res.append(right - left)
	print(*res)


import sys
import math

def ii():
	return sys.stdin.readline().strip()

def idata():
	return [int(x) for x in ii().split()]

def solve_of_problem():
	(a, b, q) = idata()
	(a, b) = (min(a, b), max(a, b))
	ans = []
	for i in range(q):
		(l, r) = idata()
		qw = a * b // math.gcd(a, b)
		(l1, r1) = (l // qw, r // qw)
		(l1, r1) = (l1 * (qw - b), r1 * (qw - b))
		(l, r) = (max(0, l % qw - b), max(0, r % qw - b + 1))
		ans += [r1 + r - l1 - l]
	print(*ans)
	return
for ______ in range(int(ii())):
	solve_of_problem()


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)

def cnt(n, l, x):
	llim = n - n % l
	return llim // l * x + min(n - llim + 1, x)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	l = lcm(a, b)
	x = max(a, b)
	for i in range(q):
		(lt, r) = map(int, input().split())
		print(r - lt + 1 - cnt(r, l, x) + cnt(lt - 1, l, x), end=' ')
	print('')


t = int(input())
for i in range(t):
	(a1, b1, q1) = input().split(' ')
	a = int(a1)
	b = int(b1)
	q = int(q1)
	m = max(a, b)
	n = min(a, b)
	lcm = 0
	k = n // 2
	if m % n == 0:
		lcm = m
	else:
		while k >= 1:
			if m % k == 0 and n % k == 0:
				lcm = m * n // k
				break
			k = k - 1
	for i in range(q):
		(l1, r1) = input().split(' ')
		l = int(l1)
		r = int(r1)
		i = l // lcm
		j = r // lcm
		s = 0
		s = s + max(0, j - i - 1) * m
		if i != j:
			if l <= i * lcm + m - 1:
				s = s + i * lcm + m - l
			if r <= j * lcm + m - 1:
				s = s + r - j * lcm + 1
			if r > j * lcm + m - 1:
				s = s + m
		elif l > i * lcm + m - 1:
			s = s
		else:
			s = s + min(r, i * lcm + m - 1) - l + 1
		print(r - l - s + 1, end=' ')
	print()


from math import gcd

def scanf():
	return list(map(int, input().split()))

def f(p, a, b):
	lcm = a * b // gcd(a, b)
	res = p // lcm * (lcm - b) + max(0, p % lcm - b)
	return res
for t in range(int(input())):
	(a, b, q) = scanf()
	if a > b:
		(a, b) = (b, a)
	for _ in range(q):
		(l, r) = scanf()
		if b % a == 0:
			print('0')
		else:
			print(int(f(r + 1, a, b) - f(l, a, b)))


import math
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	if b > a:
		(a, b) = (b, a)
	res = []
	for i in range(q):
		(l, r) = map(int, input().split())
		temp = a * b
		temp //= math.gcd(a, b)
		x = (r + 1) // temp
		ans = x * a
		temp1 = x * temp
		ans += min(a, r - temp1 + 1)
		x = l // temp
		ans -= x * a
		temp1 = x * temp
		ans -= min(a, l - temp1)
		res.append(r - l + 1 - ans)
	print(*res)


def answer(x, y, z):
	p = x // y
	s = p * y
	ans = x
	if p != 0:
		ans -= (p - 1) * z
		ans -= min(z, x - s + 1)
	return ans - min(z - 1, x)

def gcd(a, b):
	if a == 0:
		return b
	else:
		return gcd(b % a, a)
t = int(input())
for i in range(t):
	num1 = list(map(int, input().split()))
	a = num1[0]
	b = num1[1]
	q = num1[2]
	lcm = a * b // gcd(a, b)
	m = max(a, b)
	lis = []
	for j in range(q):
		(l, r) = list(map(int, input().split()))
		lis.append(answer(r, lcm, m) - answer(l - 1, lcm, m))
	print(' '.join((str(x) for x in lis)))


for i in range(int(input())):
	(a, b, q) = map(int, input().split())
	check = [0 for _ in range(a * b)]
	for i in range(1, a * b):
		check[i] = check[i - 1]
		if i % a % b != i % b % a:
			check[i] += 1
	for _ in range(q):
		ans = 0
		(l, r) = map(int, input().split())
		ch = l % (a * b)
		if ch == 0:
			ch = b * a
		helpp = (l - 1) % (a * b)
		ch = a * b - ch
		if l + ch <= r:
			l += ch
			ans += check[a * b - 1] - check[helpp]
			ch = r - l
			ans += check[-1] * (ch // (a * b))
			ch = ch % (a * b)
			ans += check[ch]
		else:
			ans = check[r % (a * b)] - check[(l - 1) % (a * b)]
		print(ans, end=' ')
	print()


import io
import os
from collections import Counter, defaultdict, deque

def solve(A, B, Q, LR):
	cumsum = [0]
	for x in range(0, A * B):
		val = 0
		if x % A % B != x % B % A:
			val = 1
		cumsum.append(cumsum[-1] + val)
	ans = []
	for (l, r) in LR:
		(lq, lr) = divmod(l, A * B)
		(rq, rr) = divmod(r, A * B)
		count = cumsum[-1] * (rq - lq) + cumsum[rr + 1] - cumsum[lr]
		ans.append(count)
	return ' '.join(map(str, ans))
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(A, B, Q) = [int(x) for x in input().split()]
	LR = [[int(x) for x in input().split()] for i in range(Q)]
	ans = solve(A, B, Q, LR)
	print(ans)


import sys
from collections import *
from bisect import *
from math import *
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	(a, b, q) = map(int, input().split())
	fac = a * b // gcd(a, b)
	rrr = []
	for i in range(q):
		s1 = 0
		s2 = 0
		(l, r) = map(int, input().split())
		s = max(a, b)
		if l < s:
			s1 += l
		else:
			s1 += s - 1
			q1 = max(0, l // fac)
			if q1 != 0:
				s1 += max(0, q1 - 1) * s
			if q1 != 0:
				s1 += min(s, l % fac + 1)
		if r < s:
			s2 += r
		else:
			s2 += s - 1
			q2 = max(0, r // fac)
			if q2 != 0:
				s2 += max(0, q2 - 1) * s
			if q2 != 0:
				s2 += min(s, r % fac + 1)
		s2 = s2 - s1
		if l % a % b == l % b % a:
			zz = 1
		else:
			zz = 0
		rrr.append(r - l - s2 + 1 - zz)
	print(*rrr)


import sys, string
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi, log2

def input():
	return sys.stdin.buffer.readline().decode('utf-8')
from bisect import bisect_left, bisect_right
from operator import add
MOD = int(1000000000.0) + 7
INF = float('inf')

def solve():
	(a, b, q) = [int(x) for x in input().split()]
	n = a * b + 1
	m = [0] * n
	for x in range(1, n):
		if x % a % b != x % b % a:
			m[x] = 1
		m[x] += m[x - 1]
	cur = [0] * q
	for i in range(q):
		(l, r) = [int(x) for x in input().split()]
		l -= 1
		ans = r // (a * b) * m[a * b] + m[r % (a * b)]
		ans -= l // (a * b) * m[a * b] + m[l % (a * b)]
		cur[i] = ans
	print(*cur)
T = 1
T = int(input())
for case in range(1, T + 1):
	ans = solve()


import math
t = int(input())

def findcount(r, lcm, b):
	div = r // lcm
	prod = div * lcm
	tot = r
	if div != 0:
		tot -= min(b, r - prod + 1)
		tot -= (div - 1) * b
	return tot - min(r, b - 1)
while t > 0:
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	lc = a * b // math.gcd(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		if lc == b or r < b:
			print(0, ' ')
			continue
		cnt = findcount(r, lc, b)
		if l > 1:
			cnt -= findcount(l - 1, lc, b)
		print(cnt, ' ')
	print()
	t = t - 1


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	(m, n) = (max(a, b), min(a, b))
	lm = lcm(m, n)
	p = []
	while q:
		q -= 1
		(l, r) = map(int, input().split())
		l1 = l // lm + int(l % lm != 0)
		l2 = r // lm
		if l % lm == 0:
			p1 = 0
		else:
			p1 = lm - max(m, l % lm)
		p2 = max(m - 1, r % lm) - m + 1
		mid = (l2 - l1) * (lm - m)
		p.append(p1 + p2 + mid)
	print(*p)


import sys
from itertools import product
from math import gcd
strings = iter(sys.stdin.read().split())
ints = (int(x) for x in strings)
sys.setrecursionlimit(3000)

def test():
	n = 100
	for a in range(1, n + 1):
		for b in range(a, n + 1):
			s = sum((i % a % b == i % b % a for i in range(a * b // gcd(a, b))))
			s2 = sum((i % a % b == i % b % a for i in range(b)))
			print(a, b, s, s2)
			assert s == b == s2
ntc = next(ints)
for tc in range(ntc):
	(a, b, q) = (next(ints) for i in range(3))
	if a > b:
		(a, b) = (b, a)
	lcm = a * b // gcd(a, b)

	def f(x):
		(q, r) = divmod(x, lcm)
		return q * (lcm - b) + max(0, r - b)
	ans = []
	for q in range(q):
		(l, r) = (next(ints), next(ints))
		ans.append(f(r + 1) - f(l))
	print(*ans)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt

def solve():
	(a, b, q) = read()
	n = max(a, b)
	m = lcm(a, b)
	ans = []
	for i in range(q):
		(l, r) = read()
		ans.append(r - l + 1 - (n * (r // m - l // m + 1) - min(n, l % m) - max(0, n - r % m - 1)))
	print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = scanner()
for i in range(t):
	solve()


import sys
import math
import bisect

def query_sum_value(A, p):
	n = len(A)
	ans = (p + 1) // n * A[-1]
	if (p + 1) % n:
		ans += A[(p + 1) % n - 1]
	return ans

def solve(a, b, q):
	A = [0] * (a * b)
	for i in range(len(A)):
		if i % a % b != i % b % a:
			A[i] = 1
	for i in range(1, len(A)):
		A[i] += A[i - 1]
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		val = query_sum_value(A, r) - query_sum_value(A, l - 1)
		ans.append(val)
	return ans

def main():
	for _ in range(int(input())):
		(a, b, q) = map(int, input().split())
		ans = solve(a, b, q)
		print(' '.join(list((str(a) for a in ans))))
main()


t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	p = a * b
	count = [0]
	for x in range(p):
		if x % a % b != x % b % a:
			count.append(count[-1] + 1)
		else:
			count.append(count[-1])
	res = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		sol = count[r % p + 1] - count[l % p] + count[-1] * (r // p - l // p)
		res.append(sol)
	print(' '.join((str(r) for r in res)))


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)

def solve(n):
	return (lc - b) * (n // lc) + max(0, n % lc - b + 1)
t = int(input())
for tc in range(0, t):
	(aa, bb, q) = map(int, input().split())
	(a, b) = (min(aa, bb), max(aa, bb))
	lc = lcm(a, b)
	ans = []
	for i in range(0, q):
		(l, r) = map(int, input().split())
		(ans1, ans2) = (solve(r), solve(l - 1))
		ans.append(ans1 - ans2)
	print(*ans)


import math

def lcm(a, b):
	return a * b // math.gcd(a, b)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	k = lcm(a, b) // b
	nt = b
	t = (k - 1) * b
	f = k * b
	ab = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		s = 0
		lf = l // f
		rf = r // f
		cm = (lf + 1) * f
		s += min(t, cm - l)
		s += (rf - lf - 1) * t
		s += max(0, r - rf * f - nt + 1)
		ab.append(s)
	print(' '.join(map(str, ab)))


import sys
input = lambda : sys.stdin.readline().rstrip()

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def calc(b, c, x):
	ans = x
	k = x // (b * c)
	ans -= k * b
	ans -= min(x - k * b * c + 1, b)
	return ans
T = int(input())
for _ in range(T):
	(a, b, q) = map(int, input().split())
	L = []
	if a == b:
		for _ in range(q):
			(l, r) = map(int, input().split())
			L.append(0)
	else:
		if a > b:
			(a, b) = (b, a)
		c = a // gcd(a, b)
		for _ in range(q):
			(l, r) = map(int, input().split())
			L.append(calc(b, c, r) - calc(b, c, l - 1))
	print(*L)


import os
import sys

def calc_ts(a, b):
	r = [0] * (a * b)
	s = 0
	for x in range(a * b):
		s += x % a % b != x % b % a
		r[x] = s
	return r

def f(a, b, l, r, ts):
	(nr, mr) = divmod(r, a * b)
	(nl, ml) = divmod(l - 1, a * b)
	return ts[-1] * nr + ts[mr] - (ts[-1] * nl + ts[ml])

def bf(a, b, l, r):
	return sum((x % a % b != x % b % a for x in range(l, r + 1)))
if False:
	import numpy as np
	for i in range(100):
		(a, b) = np.random.choice(100, 2) + 1
		ts = calc_ts(a, b)
		(l, r) = sorted(np.random.choice(100000, 2, replace=False) + 1)
		assert f(a, b, l, r, ts) == bf(a, b, l, r)

def pp(input):
	n_test = int(input())
	for i in range(n_test):
		(a, b, q) = map(int, input().split())
		res = [0] * q
		ts = calc_ts(a, b)
		for idx in range(q):
			(l, r) = map(int, input().split())
			res[idx] = f(a, b, l, r, ts)
		print(' '.join(map(str, res)))
if 'paalto' in os.getcwd():
	from string_source import string_source
	pp(string_source('2\n4 6 5\n1 3\n1 1\n1 5\n1 7\n1 9\n7 10 2\n7 8\n100 200'))
else:
	pp(iter(sys.stdin.buffer.read().decode().splitlines()).__next__)


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	ntest = int(stdin.readline())
	for testcase in range(ntest):
		(a, b, q) = [int(x) for x in stdin.readline().split()]
		f = {-1: 0}
		for i in range(a * b + 2):
			f[i] = f[i - 1] + (0 if i % a % b == i % b % a else 1)
		F = lambda n: f[a * b] * (n // (a * b)) + f[n % (a * b)]
		for case in range(q):
			(l, r) = [int(x) for x in stdin.readline().split()]
			stdout.write('%d ' % (F(r) - F(l - 1)))
		stdout.write('\n')
	return 0
main()


from collections import deque
from collections import OrderedDict
import math
import sys
import os
from io import BytesIO
numbar = 0
for t in range(int(input())):
	(a, b, q) = map(int, input().split())
	ab = a * b
	isTrue = [0] * (ab + 1)
	preFixSum = [0] * (ab + 1)
	countO = 0
	for i in range(1, ab + 1):
		val1 = i % a % b
		val2 = i % b % a
		if val1 != val2:
			countO += 1
			isTrue[i] = 1
	for i in range(1, len(preFixSum)):
		preFixSum[i] = preFixSum[i - 1] + isTrue[i]
	for q1 in range(q):
		(l, r) = map(int, input().split())
		answer = 0
		sumL = 0
		sumR = 0
		if r - l <= ab:
			distation = ab - l % ab
			if r % ab == 0:
				answer += preFixSum[ab] - preFixSum[l % ab - 1]
			elif l % ab == 0:
				answer = preFixSum[r % ab]
			elif r - l <= distation:
				answer = preFixSum[r % ab] - preFixSum[l % ab - 1]
			else:
				answer = preFixSum[ab] - preFixSum[l % ab - 1] + preFixSum[r % ab]
			try:
				if numbar == 7266 and l < -99999:
					raise Exception
			except Exception:
				print(str(l) + '-' + str(r) + '-' + str(a) + '-' + str(b))
			numbar += 1
			print(answer)
			continue
		coutNumber = r // ab - l // ab
		if l % ab != 0:
			sumL = preFixSum[ab] - preFixSum[l % ab - 1]
			coutNumber -= 1
		if r % ab != 0:
			sumR = preFixSum[r % ab]
		if coutNumber > 0:
			answer += countO * coutNumber
		numbar += 1
		print(answer + sumL + sumR)


from math import gcd, ceil, floor
t = int(input())
for _ in range(t):
	(a, b, q) = list(map(int, input().split()))
	if a > b:
		(a, b) = (b, a)
	lcm = a * b // gcd(a, b)
	ans = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		x = r // lcm - l // lcm
		res = x * b
		if l % lcm < b:
			res += b - l % lcm
		if r % lcm < b:
			res -= b - r % lcm - 1
		ans.append(r - l + 1 - res)
	print(*ans)


import sys
t = int(input())
for q in range(t):
	(a, b, q) = map(int, input().strip().split())
	ans = []
	grid = []
	for i in range(a * b + 1):
		if i == 0:
			grid.append(0)
		else:
			grid.append(grid[i - 1] + (i % a % b != i % b % a))
	for i in range(q):
		(l, r) = map(int, input().strip().split())
		l -= 1
		ans1 = grid[a * b] * (r // (a * b)) + grid[r % (a * b)]
		ans2 = grid[a * b] * (l // (a * b)) + grid[l % (a * b)]
		ans.append(ans1 - ans2)
	print(*ans)


import sys

def gcd(a: int, b: int) -> int:
	if a < b:
		(a, b) = (b, a)
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def main():
	t = int(input())
	for _ in range(t):
		(a, b, q) = map(int, sys.stdin.readline().split())
		flag = None
		modVal = None
		if a == 1 or b == 1 or a == b:
			flag = 0
		elif max(a, b) % min(a, b) == 0:
			flag = 0
			modVal = min(a, b)
		else:
			flag = 2
			modVal = a * b // gcd(a, b)
			checkList = [0] * modVal
			loopSum = 0
			for i in range(modVal):
				if i % a % b != i % b % a:
					loopSum += 1
				checkList[i] = loopSum
		for i in range(q):
			(x, y) = map(int, sys.stdin.readline().split())
			if flag == 0:
				print(0, end=' ')
			elif flag == 1:
				ans = y - x + 1
				ans = ans - y // modVal + x // modVal
				if not x % modVal:
					ans -= 1
				print(ans, end=' ')
			else:

				def calcSum(n: int) -> int:
					return checkList[-1] * (n // modVal) + checkList[n % modVal]
				ans = calcSum(y) - calcSum(x - 1)
				print(ans, end=' ')
		print('')
	return
main()


from math import *

def main():
	for _ in range(int(input())):
		(a, b, q) = map(int, input().split())
		if a > b:
			(a, b) = (b, a)
		ma = max(a, b)
		_gcd = int(gcd(a, b))
		_lcm = int(a * b / _gcd)
		for qq in range(q):
			(l, r) = map(int, input().split())
			l1 = (l - 1) // _lcm - 1
			left = (l - 1) // _lcm * _lcm
			r1 = r // _lcm - 1
			right = r // _lcm * _lcm
			ans = r - l + 1
			ans -= (r1 - l1) * ma
			ans -= min(ma, r - right + 1)
			ans += min(ma, l - left)
			print(ans, end=' ')
		print()
main()


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	(a, b, q) = map(int, stdin.readline().split())
	ab = a * b
	counts = [0]
	for x in range(1, ab):
		counts.append(counts[-1])
		if x % a % b != x % b % a:
			counts[-1] += 1
	for __ in range(q):
		(l, r) = map(int, stdin.readline().split())
		count_r = counts[-1] * (r // ab) + counts[r % ab]
		count_l = counts[-1] * ((l - 1) // ab) + counts[(l - 1) % ab]
		count = str(count_r - count_l)
		stdout.write(count + ' ')
	stdout.write('\n')


from math import *
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lis = []
	val = a * b // gcd(a, b)
	k = max(a, b)
	for i in range(q):
		(l, r) = map(int, input().split())
		if a == b:
			lis.append(0)
		else:
			cnt = r // val * (val - k) + max(0, r % val - (k - 1))
			cnt -= (l - 1) // val * (val - k) + max(0, (l - 1) % val - (k - 1))
			lis.append(cnt)
	print(*lis)


import math
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	lcm = a * b // math.gcd(a, b)
	for qq in range(q):
		(L, R) = map(int, input().split())
		if b % a == 0:
			print(0, end=' ')
		else:
			res = R
			temp = min(R, b - 1)
			res -= temp
			res -= R // lcm * b
			temp1 = R // lcm * lcm
			if temp1:
				temp1 += b - 1
			if temp1 > R:
				res += temp1 - R
			if L > 1:
				L -= 1
				res1 = L
				temp = min(L, b - 1)
				res1 -= temp
				res1 -= L // lcm * b
				temp1 = L // lcm * lcm
				if temp1:
					temp1 += b - 1
				if temp1 > L:
					res1 += temp1 - L
				res -= res1
			print(res, end=' ')
	print()


import math
n = int(input())
for i in range(n):
	(a, b, q) = [int(x) for x in input().split()]
	(a, b) = (min(a, b), max(a, b))
	lcm = int(a * b / math.gcd(a, b))
	k = max(a, b)
	res = []
	for j in range(q):
		(l, r) = [int(x) for x in input().split()]
		(l, r) = (min(l, r), max(l, r))
		ret = 0
		idl = int(l // lcm)
		idr = int(r // lcm)
		if idr - idl > 0:
			ret += (idr - idl - 1) * b
			ret += max(0, b - l % lcm)
			ret += max(0, min(r % lcm, b - 1) + 1)
		elif idl == idr:
			ret += max(min(b - 1, r % lcm) - l % lcm + 1, 0)
		res.append(r - l + 1 - ret)
	for j in range(q - 1):
		print(res[j], end=' ')
	print(res[q - 1])


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
import sys
input = sys.stdin.buffer.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def quic():
	for _ in range(int(input().strip())):
		(a, b, q) = map(int, input().split())
		period = lcm(a, b)
		maxx = max(a, b)
		for _ in range(q):
			(l, r) = map(int, input().split())
			total = r - l + 1
			total_bad = 0
			bottom = l // period + 1
			top = r // period
			x = l % period
			y = r % period
			if top < bottom:
				total_bad += max(0, min(maxx, y + 1) - x)
				total -= total_bad
				print(total)
			else:
				total_bad += (top - bottom) * maxx
				total_bad += min(maxx, y + 1)
				total_bad += max(0, maxx - x)
				total -= total_bad
				print(total)
quic()


def wt(x, g, a):
	if x % g >= a:
		return (x // g + 1) * a
	else:
		return x // g * a + (x % g + 1)
import fractions
t = int(input())
for loop in range(t):
	(a, b, q) = map(int, input().split())
	if b < a:
		tmp = a
		a = b
		b = tmp
	g = a * b // fractions.gcd(a, b)
	ans = []
	for lp2 in range(q):
		(l, r) = map(int, input().split())
		ans.append(r - l + 1 - (wt(r, g, b) - wt(l - 1, g, b)))
	print(*ans)


import math as mt
import collections as cc
import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(a, b, q) = I()
	lcm = a * b // mt.gcd(a, b)
	mul = lcm - max(a, b)
	tf = []
	for tt in range(q):
		(l, r) = I()
		if a == b:
			tf.append(0)
		else:
			ans = r // lcm * mul + max(0, r % lcm - max(a, b) + 1)
			ans -= (l - 1) // lcm * mul + max(0, (l - 1) % lcm - max(a, b) + 1)
			tf.append(ans)
	print(*tf)


from math import gcd

def findTileCount(x, y, l, r):
	lcm = x * y // gcd(x, y)
	count1 = (l - 1) // lcm
	countr = r // lcm
	return (lcm, countr - count1)
t = int(input())
while t:
	(a, b, q) = map(int, input().split())
	for i in range(q):
		(l, r) = map(int, input().split())
		(lcm, ctr) = findTileCount(a, b, max(a, max(b, l)), r)
		temp = r - max(a, max(b, l)) + 1
		temp2 = (ctr - 1) * max(a, b)
		if temp2 < 0:
			temp2 = 0
		lastmul = r // lcm * lcm
		if lastmul <= r and lastmul >= l:
			temp2 += min(r - lastmul + 1, max(a, b))
		firstmul = l // lcm * lcm
		if l % lcm != 0 and firstmul < l and (firstmul != 0):
			temp2 += max(0, firstmul - l + max(a, b))
		print(max(0, temp - temp2), end=' ')
	print()
	t -= 1


t = int(input())

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
for p in range(0, t):
	ans = []
	(a, b, q) = map(int, input().split())
	chota = min(a, b)
	bada = max(a, b)
	for i in range(0, q):
		(l, r) = map(int, input().split())
		if r <= chota:
			ans.append(0)
		else:
			lc = int(lcm(chota, bada))
			x = l // lc
			y = r // lc
			if y == 0:
				if r < bada:
					ans.append(0)
				elif l > bada:
					ans.append(r - l + 1)
				else:
					ans.append(r - bada + 1)
			elif x * lc + bada <= l and y * lc + bada <= r:
				temp = y - x
				ans.append(r - l + 1 - temp * bada)
			else:
				temp = (y - x) * bada
				extra = 0
				if x * lc + bada > l:
					extra += x * lc + bada - l
				if y * lc + bada > r:
					extra -= y * lc + bada - r - 1
				ans.append(r - l + 1 - (temp + extra))
	print(*ans)


import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
int1 = lambda x: int(x) - 1
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
	(a, b, q) = nm()
	qu = [nl() for _ in range(q)]
	g = [int(i % a % b != i % b % a) for i in range(a * b + 1)]
	for i in range(a * b):
		g[i + 1] += g[i]

	def cnt(x):
		return x // (a * b) * g[a * b] + g[x % (a * b)]
	print(*[cnt(r) - cnt(l - 1) for (l, r) in qu])
T = ni()
for _ in range(T):
	solve()


import sys
input = sys.stdin.buffer.readline
from collections import defaultdict
from fractions import gcd
con = 10 ** 9 + 7

def getlist():
	return list(map(int, input().split()))

def main():
	t = int(input())
	for _ in range(t):
		(a, b, q) = getlist()
		GCD = gcd(a, b)
		LCM = int(a * b // GCD)
		L = [0] * (LCM + 1)
		for i in range(LCM):
			if i % a % b != i % b % a:
				L[i + 1] = 1
		for i in range(LCM):
			L[i + 1] += L[i]
		ansL = [0] * q
		for i in range(q):
			(l, r) = getlist()
			x = int(l // LCM)
			p = l % LCM
			y = int(r // LCM)
			q = r % LCM
			ans = (y - x) * L[-1] + L[q + 1] - L[p]
			ansL[i] = ans
		print(' '.join(list(map(str, ansL))))
main()


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from functools import *
from collections import deque
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	fact(x, mod)
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa.reverse()

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] > self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		seen.add(u)
		for (v, w) in graph[u]:
			if v not in d or d[v] > d[u] + w:
				d[v] = d[u] + w
				heappush(heap, (d[v], v))
	return d

def GP(it):
	return [[ch, len(list(g))] for (ch, g) in groupby(it)]

def lcm(a, b):
	return a * b // gcd(a, b)

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None

def nb(i, j):
	for (ni, nj) in [[i + 1, j], [i - 1, j], [i, j - 1], [i, j + 1]]:
		if 0 <= ni < n and 0 <= nj < m:
			yield (ni, nj)

@bootstrap
def gdfs(r, p):
	if len(g[r]) == 1 and p != -1:
		yield None
	for ch in g[r]:
		if ch != p:
			yield gdfs(ch, r)
	yield None

def topo(n):
	q = deque()
	res = []
	for i in range(1, n + 1):
		if ind[i] == 0:
			q.append(i)
			res.append(i)
	while q:
		u = q.popleft()
		for v in g[u]:
			ind[v] -= 1
			if ind[v] == 0:
				q.append(v)
				res.append(v)
	return res

def cal(x):
	(d, m) = divmod(x, p)
	ans = d * b
	if m:
		if m < b:
			ans += m
		else:
			ans += b - 1
	return ans
t = N()
for i in range(t):
	(a, b, q) = RL()
	if a > b:
		(a, b) = (b, a)
	ans = []
	p = lcm(a, b)
	if b % a == 0:
		for i in range(q):
			(l, r) = RL()
			ans.append(0)
	else:
		for i in range(q):
			(l, r) = RL()
			tmp = r - l + 1 - (cal(r) - cal(l - 1))
			ans.append(tmp)
	print(*ans)


def getSol(list_, n, target):
	return list_[n - 1] * (target // n) + list_[target % n]
list_ = [0] * 200 * 200
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	res = []
	for y in range(1, a * b):
		list_[y] = list_[y - 1]
		if y % a % b != y % b % a:
			list_[y] += 1
	for _ in range(q):
		(l, r) = map(int, input().split())
		out = getSol(list_, a * b, r) - getSol(list_, a * b, l - 1)
		res.append(str(out))
	print(' '.join(res))


pref = [0 for i in range(40005)]

def build(a, b):
	l = a * b
	for i in range(1, l + 1):
		pref[i] = pref[i - 1]
		if i % a % b != i % b % a:
			pref[i] += 1

def query(l):
	ln = a * b
	cnt = l // ln
	rem = l % ln
	return cnt * pref[ln] + pref[rem]
t = int(input())
for i in range(t):
	(a, b, q) = [int(x) for x in input().split()]
	build(a, b)
	for i in range(q):
		(l, r) = [int(x) for x in input().split()]
		print(query(r) - query(l - 1), end=' ')
	print()


from math import gcd
for _ in range(int(input())):
	(a, b, n) = map(int, input().split())
	if a > b:
		(b, a) = (a, b)
	lcm = a * b // gcd(a, b)
	ans = []
	for _ in range(n):
		(l, r) = map(int, input().split())
		l -= 1
		ans1 = l // lcm * (lcm - b) + max(0, l % lcm - b + 1)
		ans2 = r // lcm * (lcm - b) + max(0, r % lcm - b + 1)
		ans.append(ans2 - ans1)
	print(*ans)


import math
n = int(input())
for i in range(n):
	(a, b, q) = map(int, input().split())
	m = a * b // math.gcd(a, b)
	d = max(a, b)
	for j in range(q):
		(l, r) = map(int, input().split())
		print(r + 1 - l - (d * (r // m - l // m + 1) - min(d, l % m) - max(0, d - r % m - 1)))


def compress(string):
	string.append('#')
	n = len(string)
	(begin, end, cnt) = (0, 1, 1)
	ans = []
	while end < n:
		if string[begin] == string[end]:
			(end, cnt) = (end + 1, cnt + 1)
		else:
			ans.append((string[begin], cnt))
			(begin, end, cnt) = (end, end + 1, 1)
	return ans

def solve(num):
	if num <= init:
		return num
	num -= init
	tmp = init
	tmp += num // diff * res[2][1]
	num = num - num // diff * diff
	tmp += max(num - res[1][1], 0)
	return tmp
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	tmp = [False] * 10 ** 5
	for i in range(1, 10 ** 5 + 1):
		tmp[i - 1] = i % a % b == i % b % a
	res = compress(tmp)
	if len(res) == 1:
		ans = []
		for _ in range(q):
			(l, r) = map(int, input().split())
			ans.append(0)
		print(*ans)
		continue
	init = res[0][1]
	diff = res[1][1] + res[2][1]
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		ans.append(r - solve(r) - (l - 1 - solve(l - 1)))
	print(*ans)


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x // gcd(x, y) * y

def count(num, same, cycleSize):
	r = num // cycleSize * same
	num %= cycleSize
	return r + min(same - 1, num)
t = int(input())
while t:
	(a, b, q) = [int(i) for i in input().split(' ')]
	lcmab = lcm(a, b)
	if a > b:
		(a, b) = (b, a)
	while q:
		(x, y) = [int(i) for i in input().split(' ')]
		print(y - x + 1 - count(y, b, lcmab) + count(x - 1, b, lcmab))
		q -= 1
	t -= 1


import sys
import heapq
import math
import bisect

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, input().split())

def rlinput():
	return list(map(i, input().split()))

def srlinput():
	return sorted(list(map(int, input().split())))

def main():

	def pro(x):
		return len(w) * (x // (a * b)) + bisect.bisect_right(w, x % (a * b))
	(a, b, q) = rinput()
	w = []
	for i in range(a * b):
		if i % a % b != i % b % a:
			w.append(i)
	for i in range(q):
		(l, r) = rinput()
		print(pro(r) - pro(l - 1), end=' ')
	print()
for i in range(iinput()):
	main()


t = int(input())
from math import gcd
for i in range(t):
	(a, b, q) = map(int, input().split())
	ans = []
	g = gcd(a, b)
	(a, b) = (min(a, b), max(a, b))
	lcm = a * b // g
	for j in range(q):
		(l, r) = map(int, input().split())
		if a == b or r < b:
			ans.append(0)
		else:
			(t1, t2) = (0, 0)
			x = r - max(l, b) + 1
			y = r // lcm - l // lcm
			if r % lcm == 0:
				y -= 1
				t1 = -1
			elif r % lcm < b - 1 and r // lcm > 0:
				t1 = b - r % lcm - 1
			if l % lcm == 0:
				y += 1
			elif l % lcm <= b - 1 and l // lcm > 0:
				t2 = -b + l % lcm
			ans.append(x - y * b + t1 + t2)
	print(*ans)


def cont(a, b, dp, x):
	return dp[a * b - 1] * (x // (a * b)) + dp[x % (a * b)]

def main():
	(a, b, q) = map(int, input().split())
	dp = [0 for _ in range(a * b)]
	for i in range(a * b):
		if i == 0:
			dp[i] = 0
		else:
			lhs = i % a % b
			rhs = i % b % a
			if lhs != rhs:
				dp[i] = 1
	for i in range(1, a * b):
		dp[i] += dp[i - 1]
	while q:
		(l, r) = map(int, input().split())
		if a == b:
			print('0', end=' ')
		else:
			print(cont(a, b, dp, r) - cont(a, b, dp, l - 1), end=' ')
		q -= 1
n = int(input())
while n:
	main()
	n -= 1


ok = [0] * 50000
pf = [0] * 50000
for t in range(int(input())):
	(a, b, q) = map(int, input().split())
	pf[-1] = 0
	for i in range(a * b):
		ok[i] = i % a % b != i % b % a
		pf[i] = pf[i - 1] + ok[i]
	for tt in range(q):
		(l, r) = map(int, input().split())
		cl = ((l - 1) // (a * b) + 1) * (a * b)
		cr = r - r % (a * b)
		if cl > cr:
			print(pf[r % (a * b)] - (0 if l % (a * b) == 0 else pf[l % (a * b) - 1]), end=' ')
		else:
			c = (cr - cl) // (a * b)
			ans = c * pf[a * b - 1]
			tli = cl - l
			tri = r - cr
			print(ans + pf[tri] + pf[a * b - 1] - pf[a * b - 1 - tli], end=' ')
	print()


a = 6
b = 4
ans = 0

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(input())):
	(a, b, m) = map(int, input().split())
	k = lcm(a, b)
	for __ in range(m):
		(x, y) = map(int, input().split())
		king = 0
		ans = (x - 1) // k * (k - max(a, b))
		c = (x - 1) % k - (max(a, b) - 1)
		if c > 0:
			ans += c
		y += 1
		if ans < 0:
			ans = 0
		ans2 = (y - 1) // k * (k - max(a, b))
		c = (y - 1) % k - (max(a, b) - 1)
		if c >= 0:
			ans2 += c
		if ans2 < 0:
			ans2 = 0
		print(ans2 - ans, end=' ')
	print()


import sys
input = sys.stdin.readline
from collections import *
from fractions import *

def f(a, b, c):
	if a < c:
		return 0
	else:
		return (a - c) // b + 1
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	L = a * b // gcd(a, b)
	ans = []
	for _ in range(q):
		(l, r) = map(int, input().split())
		eq = 0
		for x in range(a):
			m = 0
			while a * m + x <= b - 1:
				c = a * m + x
				eq += f(r, L, c) - f(l - 1, L, c)
				m += 1
		ans.append(r - l + 1 - eq)
	print(*ans)


import sys
from math import gcd
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	data = [i % a % b != i % b % a for i in range(lcm)]
	for i in range(1, lcm):
		data[i] += data[i - 1]

	def func(num):
		s = (num + 1) // lcm
		t = (num + 1) % lcm
		if t != 0:
			return data[-1] * s + data[t - 1]
		else:
			return data[-1] * s
	ans = []
	for i in range(q):
		(l, r) = map(int, input().split())
		ans.append(func(r) - func(l - 1))
	print(*ans)


import math, sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(a, b, q) = map(int, input().split())
	if b < a:
		(a, b) = (b, a)
	c = a * b // math.gcd(a, b)
	A = [0 for i in range(c)]
	for i in range(c):
		if i >= b:
			A[i] = A[i - 1] + 1
	B = []
	for i in range(q):
		v = 0
		(l1, r1) = map(int, input().split())
		l = max(l1 % c, 1)
		r = r1 % c
		l2 = l1 // c
		r2 = r1 // c
		vc = A[r] - A[l - 1] + A[c - 1] * (r2 - l2)
		B.append(vc)
	print(*B)


import math

def lcm(a, b):
	return a * b // math.gcd(a, b)

def f(x, a, b):
	lc = lcm(a, b)
	m = max(a, b)
	return x // lc * m + min(m, x % lc + 1)

def solve(a, b, l, r):
	ll = f(l - 1, a, b)
	rr = f(r, a, b)
	return r - l + 1 - (rr - ll)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	sol = []
	for i in range(q):
		(l, r) = map(int, input().split())
		sol.append(solve(a, b, l, r))
	print(*sol)


import math

def num(a, b, x):
	l = a * b // math.gcd(a, b)
	k = l - max(a, b)
	k *= x // l
	n = x - x // l * l - max(a, b) + 1
	if n > 0:
		k += n
	return k
for _ in range(int(input())):
	(a, b, q) = map(int, input().split())
	ans = []
	for _ in range(q):
		(li, ri) = map(int, input().split())
		n = num(a, b, ri) - num(a, b, li - 1)
		ans.append(str(n))
	print(' '.join(ans))


import math
from sys import stdin
input = stdin.readline
INF = 10 ** 9 + 7
MAX = 10 ** 7 + 7
MOD = 10 ** 9 + 7

def solve(x, l, c):
	ans = x // l * c
	ans += min(x % l + 1, c)
	return x - ans + 1
for Ti in range(int(input().strip())):
	(a, b, q) = [int(x) for x in input().strip().split()]
	g = math.gcd(a, b)
	l = a * b // g
	c = max(a, b)
	for qi in range(q):
		ans = 0
		(s, e) = [int(x) for x in input().strip().split()]
		print(-solve(s - 1, l, c) + solve(e, l, c), end=' ')
	print('')


from sys import stdin, stdout
from math import ceil

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, stdin.readline().split())
	if max(a, b) % min(a, b) == 0:
		for _ in range(q):
			(l, r) = map(int, stdin.readline().split())
			print(0)
	else:
		lcm = a * b // gcd(a, b)
		m = max(a, b)
		how = lcm - m
		ans = []
		for _ in range(q):
			(l, r) = map(int, stdin.readline().split())
			x1 = (l - 1) // lcm * how
			final1 = (l - 1) % lcm
			x1 += max(0, final1 - m + 1)
			x2 = r // lcm * how
			final2 = r % lcm
			x2 += max(0, final2 - m + 1)
			ans.append(x2 - x1)
		print(*ans)


def gcd(a, b):
	while b > 0:
		(a, b) = (b, a % b)
	return a

def lcmf(a, b):
	return a * b // gcd(a, b)

def main():
	cc = int(input())
	for ccc in range(cc):
		(a, b, q) = [int(v) for v in input().split()]
		data = []
		lcm = lcmf(a, b)
		sums = []
		s = 0
		for i in range(0, lcm + 1):
			if i % a % b != i % b % a:
				data.append(1)
				s += 1
			else:
				data.append(0)
			sums.append(s)
		res = []
		sums1 = sum(data[0:lcm])
		for cccc in range(q):
			(l, r) = [int(v) for v in input().split()]
			c = 0
			start = l - l % lcm
			end = r - r % lcm
			if end != r:
				end += lcm
			c = (end - start) // lcm * sums1
			c -= sums[(l - 1) % lcm] if l % lcm > 0 else 0
			c -= sums1 - sums[r % lcm] if r % lcm != 0 else 0
			res.append(str(c))
		print(' '.join(res))
main()


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline().rstrip()
res = ['YES', 'NO']
test_case = ii()
while test_case:
	test_case -= 1
	ans = []
	(a, b, q) = mi()
	m = a * b // gcd(a, b)
	if a > b:
		(a, b) = (b, a)
	for _ in range(q):
		(l, r) = mi()
		x = l // m
		y = r // m
		l = max(l, x * m + b)
		if r < y * m + b:
			r = y * m - 1
			y -= 1
		cnt = max(0, r - l + 1)
		if cnt:
			cnt -= b * (y - x)
		ans.append(cnt)
	print(*ans)


import sys
input = sys.stdin.readline

def gcd(a, b):
	if a > b:
		(a, b) = (b, a)
	while True:
		(a, b) = (b % a, a)
		if a == 0:
			return b

def solve(l, r, a, b, lcm):
	mx = max(a, b)
	d1 = l // lcm
	d2 = r // lcm
	lr = l % lcm
	rr = r % lcm
	c = 0
	if lr > mx:
		c += lcm - lr
	else:
		c += lcm - mx
	if rr >= mx:
		c += rr - mx + 1
	c += (d2 - d1 - 1) * (lcm - mx)
	return c
for i in ' ' * int(input()):
	(a, b, q) = map(int, input().split())
	g = gcd(a, b)
	lcm = a * b // g
	mx = max(a, b)
	for j in ' ' * q:
		(l, r) = map(int, input().split())
		print(solve(l, r, a, b, lcm), end=' ')
	print()


MOD = 10 ** 9 + 7

def main():
	for _ in inputt():
		(a, b, q) = inputi()
		c = a * b // gcd(a, b)
		a = max(a, b)
		R = []
		for _ in range(q):
			(l, r) = inputi()
			m = r - l + 1
			(lp, rp) = (l // c, r // c)
			m -= (rp - lp + 1) * a
			if 0 <= l % c < a:
				m += l % c
			else:
				m += a
			if 0 <= r % c < a:
				m += a - r % c - 1
			R.append(m)
		print(*R)
from math import *
from heapq import *
from itertools import *
from functools import reduce, lru_cache, partial
from collections import Counter, defaultdict
import re, copy, operator, cmath
import sys, io, os, builtins
sys.setrecursionlimit(1000)
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(io.IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	if args:
		sys.stdout.write(str(args[0]))
		split = kwargs.pop('split', ' ')
		for arg in args[1:]:
			sys.stdout.write(split)
			sys.stdout.write(str(arg))
	sys.stdout.write(kwargs.pop('end', '\n'))

def debug(*args, **kwargs):
	print('debug', *args, **kwargs)
	sys.stdout.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip()
inputt = lambda t=0: range(t) if t else range(int(input()))
inputs = lambda : input().split()
inputi = lambda : map(int, inputs())
inputl = lambda t=0, k=lambda : list(inputi()): [k() for _ in range(t)] if t else list(k())

def len(a):
	if isinstance(a, range):
		return -((a.start - a.stop) // a.step)
	return builtins.len(a)

def bisect_left(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if key(a[mid]) < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def bisect_right(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if x < key(a[mid]):
			hi = mid
		else:
			lo = mid + 1
	return lo

def insort_left(a, x, key=None, lo=0, hi=None):
	lo = bisect_left(a, x, key, lo, hi)
	a.insert(lo, x)

def insort_right(a, x, key=None, lo=0, hi=None):
	lo = bisect_right(a, x, key, lo, hi)
	a.insert(lo, x)
do_nothing = lambda x: x
bisect = bisect_right
insort = insort_right

def index(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi and key(a[i]) == x:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_lt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_le(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_gt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_ge(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError

class Tree:

	def __init__(n):
		self._n = n
		self._conn = [[] for _ in range(n)]
		self._list = [0] * n

	def connect(a, b):
		pass

class Sieve:

	def __init__(self):
		self._n = 6
		self._list = [2, 3, 5, 7, 11, 13]

	def extend(self, n):
		pass
sieve = Sieve()

def isprime(n):
	pass

def prime(n):
	pass

def factorint(n):
	factordict = defaultdict(int)
	for i in sieve:
		if n <= i * i:
			break
		while not n % i:
			factordict[i] += 1
			n //= i
	if n != 1:
		factordict[n] += 1
	return factordict
main()


import math

def solve(x, lcm, b):
	return max(0, x // lcm * b) + min(b, x - x // lcm * lcm + 1)
for t in range(int(input())):
	(a, b, n) = map(int, input().split())
	ans = [0] * n
	lcm = a * b // math.gcd(a, b)
	b = max(a, b)
	for i in range(n):
		(l, r) = map(int, input().split())
		ans[i] = r - l + 1 - (solve(r, lcm, b) - solve(l - 1, lcm, b))
	print(*ans)


def z(a, b):
	x = a * b
	p = [0] * (x + 1)
	for i in range(1, x + 1):
		p[i] = p[i - 1]
		if i % a % b != i % b % a:
			p[i] += 1
	return p

def query(k):
	cnt = k // (a * b)
	rem = k % (a * b)
	return p[a * b] * cnt + p[rem]
t = int(input())
for _ in range(t):
	(a, b, q) = map(int, input().split())
	p = z(a, b)
	for _ in range(q):
		(l, r) = map(int, input().split())
		print(query(r) - query(l - 1), end=' ')
	print()


import math as mt
import sys, string
input = sys.stdin.readline
from heapq import heappush, heapify, heappop
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())

def lcm(a, b):
	return a * b // mt.gcd(a, b)
t = I()
for _ in range(t):
	(a, b, q) = M()
	x = lcm(a, b)
	for i in range(q):
		(l, r) = M()
		d = r - l + 1
		ans1 = r // x * max(a, b)
		r %= x
		ans1 += min(max(a, b) - 1, r)
		l -= 1
		ans2 = l // x * max(a, b)
		l %= x
		ans2 += min(max(a, b) - 1, l)
		equal = ans1 - ans2
		print(d - equal, end=' ')
	print()


def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)
t = int(input())
for i in range(t):
	(a, b, q) = map(int, input().split())
	lcm = a * b // gcd(a, b)
	if a > b:
		a += b
		b = a - b
		a -= b
	for j in range(q):
		(l, r) = map(int, input().split())
		if r < b:
			print(0, end=' ')
		else:
			n = r // lcm
			s = b - 1
			s1 = 0
			if n:
				s += (n - 1) * b + min(r - n * lcm + 1, b)
			if l > 1:
				l -= 1
				if l < b:
					s1 = l
				else:
					s1 = b - 1
					n1 = l // lcm
					if n1:
						s1 += (n1 - 1) * b + min(l - n1 * lcm + 1, b)
				l += 1
			print(r - l + 1 - s + s1, end=' ')
	print()