Given two integers $n$ and $\textbf{r}$. In how many ways can $\textbf{r}$ items be chosen from $n$ items?

Input Format

The first line contains the number of test cases $\mathbf{T}$. Each of the next $\mathbf{T}$ lines contains two integers $n$ and $\textbf{r}$.

Constraints

$1\leq T\leq10^5$ 

$1\leq n\leq10^9$ 

$0\leq r\leq n$

Output Format

Output $\mathbf{T}$ lines, containing the required answer for the corresponding test case. Output all answers modulo $142857$.

Sample Input
4
2 1
4 0
5 2
10 3

Sample Output
2
1
10
120

Source: hackerrank | Difficulty: HARD
Tags: Number theory


Solutions:
invList = {27: [0, 1, 14, -1, 7, 11, -1, 4, 17, -1, 19, 5, -1, 25, 2, -1, 22, 8, -1, 10, 23, -1, 16, 20, -1, 13, 26], 11: [0, 1, 6, 4, 3, 9, 2, 8, 7, 5, 10], 13: [0, 1, 7, 9, 10, 8, 11, 2, 5, 3, 4, 6, 12], 37: [0, 1, 19, 25, 28, 15, 31, 16, 14, 33, 26, 27, 34, 20, 8, 5, 7, 24, 35, 2, 13, 30, 32, 29, 17, 3, 10, 11, 4, 23, 21, 6, 22, 9, 12, 18, 36]}
modu = 142857
primes = [(3, 3), (11, 1), (13, 1), (37, 1)]
tmp = [137566, 103896, 109890, 77220]

def myChineseRemainder(A):
	return sum((tmp[i] * A[i] for i in range(4))) % modu
from fractions import Fraction
nFactDict = {11: [1, 1, 2, 6, 2, 10, 5, 2, 5, 1, 10], 27: [1, 1, 2, 2, 8, 13, 13, 10, 26, 26, 17, 25, 25, 1, 14, 14, 8, 1, 1, 19, 2, 2, 17, 13, 13, 1, 26], 13: [1, 1, 2, 6, 11, 3, 5, 9, 7, 11, 6, 1, 12], 37: [1, 1, 2, 6, 24, 9, 17, 8, 27, 21, 25, 16, 7, 17, 16, 18, 29, 12, 31, 34, 14, 35, 30, 24, 21, 7, 34, 30, 26, 14, 13, 33, 20, 31, 18, 1, 36]}

def carryList(k, r, p):
	(kl, rl) = ([], [])
	while k > 0 or r > 0:
		kl.append(k % p)
		rl.append(r % p)
		k = k // p
		r = r // p
	carLi = [(kl[0] + rl[0]) // p]
	for i in range(1, len(kl)):
		carLi.append((carLi[-1] + kl[i] + rl[i]) // p)
	return carLi

def genLucas(n, r, p, q):
	k = n - r
	e = carryList(k, r, p)
	toCarry = sum(e)
	if q <= toCarry:
		return 0
	else:
		toCarryq = sum(e[q - 1:])
		rt = 1
		pr = pow(p, q)
		while n > 0 or r > 0 or k > 0:
			rt *= nFactDict[pr][n % pr] * invList[pr][nFactDict[pr][r % pr]] * invList[pr][nFactDict[pr][k % pr]]
			(n, r, k) = (n // p, r // p, k // p)
		rt *= p ** toCarry * pow(-1, toCarryq)
		return rt % pr

def solve(n, r):
	if n < r:
		return 0
	if n - r < r:
		r = n - r
	if n == 0:
		return 1
	rt = []
	for (p, q) in primes:
		rt.append(genLucas(n, r, p, q))
	return myChineseRemainder(rt)

def hackerRank():
	for t in range(int(input())):
		(n, r) = [int(i) for i in input().split()]
		print(solve(n, r))
hackerRank()


import functools

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(input())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in input().split()]
	print(solver.solution(n, r))


globalFacts = dict()

def compute(n, m, big):
	y = decompose(big)
	remainders = []
	for powerPrime in y:
		remainders.append(binomialModulo(n, m, powerPrime[0], powerPrime[1]))
	sol = chaineseRemainderTheorem([pow(i[0], i[1]) for i in y], remainders)
	return sol

def chaineseRemainderTheorem(num, rem):
	prod = 1
	for i in num:
		prod *= i
	result = 0
	for i in range(len(num)):
		pp = prod // num[i]
		result += rem[i] * inverse(pp, num[i]) * pp
	return result % prod

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 inverse(a, m):
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m

def binomialModulo(n, m, prime, powerPrime):
	power = int(pow(prime, powerPrime))
	(nArray, mArray, rArray, NArray, MArray, RArray, e0, eq) = allAtOnce(n, m, prime, powerPrime)
	result = 1
	denominator = 1
	result = pow(-1, eq) * pow(prime, e0) % power
	if result == 0:
		return 0
	for i in range(len(nArray)):
		result *= fact(NArray[i], prime, powerPrime)
		denominator *= fact(MArray[i], prime, powerPrime) * fact(RArray[i], prime, powerPrime)
		result = result % power
		denominator = denominator % power
	result *= inverse(denominator % power, power)
	return result % power

def fact(k, prime, powerPrime):
	power = pow(prime, powerPrime)
	if (k, power) in globalFacts:
		return globalFacts[k, power]
	factorial = 1
	for i in range(1, k + 1):
		if i % prime != 0:
			factorial = factorial * i % power
	globalFacts[k, power] = factorial
	return factorial

def allAtOnce(n, m, prime, powerPrime):
	nArray = []
	mArray = []
	rArray = []
	NArray = []
	MArray = []
	RArray = []
	r = n - m
	e0 = 0
	eq = 0
	j = 0
	power = int(pow(prime, powerPrime))
	while n > 0:
		nArray.append(n % prime)
		mArray.append(m % prime)
		rArray.append(r % prime)
		NArray.append(n % power)
		MArray.append(m % power)
		RArray.append(r % power)
		n = n // prime
		r = r // prime
		m = m // prime
		j = j + 1
		if n != m + r:
			e0 = e0 + 1
			if j >= powerPrime:
				eq = eq + 1
	return (nArray, mArray, rArray, NArray, MArray, RArray, e0, eq)

def decompose(big):
	return [(11, 1), (13, 1), (37, 1), (3, 3)]
t = int(input().strip())
for i in range(t):
	arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
	n = arr[0]
	m = arr[1]
	result = compute(n, m, 14857)
	print(result)


from operator import add, mul

def memo(f):
	cache = {}

	def _f(*args):
		try:
			return cache[args]
		except KeyError:
			result = cache[args] = f(*args)
			return result
		except TypeError:
			return f(*args)
	return _f

def mod(a, b):
	return (a % b + b) % b

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

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

@memo
def modinv(a, m):
	if gcd(a, m) is not 1:
		raise Exception('Not coprime')
	(_, x, y) = exgcd(a, m)
	return (m + x % m) % m

def sieve(m):
	(ret, judge) = ([], [True] * (m + 1))
	judge[0] = judge[1] = False
	ret.append(2)
	for i in range(4, m + 1, 2):
		judge[i] = False
	for i in range(3, m + 1, 2):
		if judge[i]:
			ret.append(i)
			for j in range(i * i, m + 1, i):
				judge[j] = False
	return ret
MAXN = 1000
primes = sieve(MAXN)
primes_set = set(primes)

def factor(n):
	if n is 0:
		raise Exception('Should be nonzero number')
	(ret, i) = ([], 0)
	while n is not 1 and i < len(primes):
		if n % primes[i] == 0:
			c = 0
			while n % primes[i] == 0:
				c += 1
				n //= primes[i]
			ret.append((primes[i], c))
		i += 1
	if n is not 1:
		ret.append((n, 1))
	return ret

def euler_phi(n):
	facts = factor(n)
	return reduce(lambda acc, x: acc * (x[0] - 1) // x[0], facts, n)

def euler_phi2(n):
	ret = [i for i in range(n + 1)]
	for i in range(2, n + 1):
		if ret[i] == i:
			for j in range(i, n + 1, i):
				ret[j] = ret[j] // i * (i - 1)
	return ret

def gen_fact_mod_prime(p):
	ret = [1] * p
	for i in range(2, p):
		ret[i] = ret[i - 1] * i % p
	return ret

def fact_mod(n, p, facts):
	if n == 0:
		return (1, 0)
	(a, e) = fact_mod(n // p, p, facts)
	e += n // p
	if n // p % 2 != 0:
		return (a * (p - facts[n % p]) % p, e)
	return (a * facts[n % p] % p, e)

def comb_mod(n, k, p):
	if n < 0 or k < 0 or n < k:
		return 0
	facts = gen_fact_mod_prime(p)
	(a1, e1) = fact_mod(n, p, facts)
	(a2, e2) = fact_mod(k, p, facts)
	(a3, e3) = fact_mod(n - k, p, facts)
	if e1 > e2 + e3:
		return 0
	else:
		return a1 * modinv(a2 * a3 % p, p) % p

def chinese_remainder_theory_for2(x, a, y, b):
	(g, s, t) = exgcd(x, y)
	if a % g != b % g:
		return (0, -1)
	else:
		return (mod(s * b * x + t * a * y, x * y) // g, x * y // g)

def chinese_remainder_theory(xs, ass):
	ret = (ass[0], xs[0])
	for i in range(1, len(xs)):
		ret = chinese_remainder_theory_for2(ret[1], ret[0], xs[i], ass[i])
		if ret[1] == -1:
			break
	return ret

def comb_mod2(n, r, m, pa, facts1):
	(p, a) = pa

	def n_fact_fact(n):
		if n is 0 or n is 1:
			return 1
		elif n < m:
			return facts1[n] * n_fact_fact(n // p) % m
		else:
			a = facts1[m - 1]
			b = facts1[n % m]
			c = n_fact_fact(n // p)
			return pow(a, n // m, m) * b * c % m

	def get_power(n, p):
		ret = 0
		while n > 0:
			ret += n // p
			n //= p
		return ret
	b = get_power(n, p) - get_power(r, p) - get_power(n - r, p)
	if b >= a:
		return 0
	m1 = n_fact_fact(n)
	m2 = n_fact_fact(r)
	m3 = n_fact_fact(n - r)
	return p ** b * m1 * modinv_table[m2, m] * modinv_table[m3, m] % m

def solve(n, r, fs1, fs2, xss):
	xs = [27, 11, 13, 37]
	ass = [comb_mod2(n, r, xs[i], xss[i], fs1[i]) for i in range(4)]
	return chinese_remainder_theory(xs, ass)

def init(xs):
	(ret1, ret2) = ([], [])
	for i in range(len(xs)):
		(p, a) = xs[i]
		m = p ** a
		(t1, t2) = ([1], [1])
		for i in range(1, p):
			t2.append(t2[-1] * i % p)
		for i in range(1, m):
			if gcd(i, m) == 1:
				t1.append(t1[-1] * i % m)
			else:
				t1.append(t1[-1])
		ret1.append(t1)
		ret2.append(t2)
	return (ret1, ret2)
modinv_table = {}
modulo = 142857
for x in {27, 11, 13, 37}:
	for y in range(1, x):
		if gcd(x, y) == 1:
			modinv_table[y, x] = modinv(y, x)

def main():
	n = int(input())
	xss = [(3, 3), (11, 1), (13, 1), (37, 1)]
	(facts1, facts2) = init(xss)
	for _ in range(n):
		(n, r) = map(int, input().strip().split())
		print(solve(n, r, facts1, facts2, xss)[0])
main()


globalFacts = dict()

def compute(n, m, big, elements):
	remainders = []
	for powerPrime in elements:
		remainders.append(binomialModulo(n, m, powerPrime[0], powerPrime[1]))
	sol = chaineseRemainderTheorem([pow(i[0], i[1]) for i in elements], remainders)
	return sol

def chaineseRemainderTheorem(num, rem):
	prod = 1
	for i in num:
		prod *= i
	resultch = 0
	for i in range(len(num)):
		pp = prod // num[i]
		resultch += rem[i] * inverse(pp, num[i]) * pp
	return resultch % prod

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 inverse(a, m):
	(g, x, y) = egcd(a, m)
	return x % m

def binomialModulo(n, m, prime, powerPrime):
	power = int(pow(prime, powerPrime))
	(nArray, mArray, rArray, NArray, MArray, RArray, e0, eq) = allAtOnce(n, m, prime, powerPrime)
	resultb = 1
	denominator = 1
	resultb = pow(-1, eq) * pow(prime, e0) % power
	if resultb == 0:
		return 0
	for i in range(len(nArray)):
		resultb *= fact(NArray[i], prime, powerPrime)
		denominator *= fact(MArray[i], prime, powerPrime) * fact(RArray[i], prime, powerPrime)
		resultb = resultb % power
		denominator = denominator % power
	resultb *= inverse(denominator % power, power)
	return resultb % power

def fact(k, prime, powerPrime):
	power = pow(prime, powerPrime)
	if (k, power) in globalFacts:
		return globalFacts[k, power]
	factorial = 1
	for i in range(1, k + 1):
		if i % prime != 0:
			factorial = factorial * i % power
	globalFacts[k, power] = factorial
	return factorial

def allAtOnce(n, m, prime, powerPrime):
	nArray = []
	mArray = []
	rArray = []
	NArray = []
	MArray = []
	RArray = []
	r = n - m
	e0 = 0
	eq = 0
	j = 0
	power = int(pow(prime, powerPrime))
	while n > 0:
		nArray.append(n % prime)
		mArray.append(m % prime)
		rArray.append(r % prime)
		NArray.append(n % power)
		MArray.append(m % power)
		RArray.append(r % power)
		n = n // prime
		r = r // prime
		m = m // prime
		j = j + 1
		if n != m + r:
			e0 = e0 + 1
			if j >= powerPrime:
				eq = eq + 1
	return (nArray, mArray, rArray, NArray, MArray, RArray, e0, eq)
elements = [[0 for x in range(2)] for y in range(4)]
elements[0][0] = 3
elements[0][1] = 3
elements[1][0] = 11
elements[1][1] = 1
elements[2][0] = 13
elements[2][1] = 1
elements[3][0] = 37
elements[3][1] = 1
t = int(input().strip())
for ii in range(t):
	cadena = input()
	arr = [int(s) for s in cadena.split()]
	nn = arr[0]
	kk = arr[1]
	result = compute(nn, kk, 142857, elements)
	print(result)


from operator import add, mul

def memo(f):
	cache = {}

	def _f(*args):
		try:
			return cache[args]
		except KeyError:
			result = cache[args] = f(*args)
			return result
		except TypeError:
			return f(*args)
	return _f

def mod(a, b):
	return (a % b + b) % b

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

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

@memo
def modinv(a, m):
	if gcd(a, m) is not 1:
		raise Exception('Not coprime')
	(_, x, y) = exgcd(a, m)
	return (m + x % m) % m

def sieve(m):
	(ret, judge) = ([], [True] * (m + 1))
	judge[0] = judge[1] = False
	ret.append(2)
	for i in range(4, m + 1, 2):
		judge[i] = False
	for i in range(3, m + 1, 2):
		if judge[i]:
			ret.append(i)
			for j in range(i * i, m + 1, i):
				judge[j] = False
	return ret
MAXN = 1000
primes = sieve(MAXN)
primes_set = set(primes)

def factor(n):
	if n is 0:
		raise Exception('Should be nonzero number')
	(ret, i) = ([], 0)
	while n is not 1 and i < len(primes):
		if n % primes[i] == 0:
			c = 0
			while n % primes[i] == 0:
				c += 1
				n //= primes[i]
			ret.append((primes[i], c))
		i += 1
	if n is not 1:
		ret.append((n, 1))
	return ret

def euler_phi(n):
	facts = factor(n)
	return reduce(lambda acc, x: acc * (x[0] - 1) // x[0], facts, n)

def euler_phi2(n):
	ret = [i for i in range(n + 1)]
	for i in range(2, n + 1):
		if ret[i] == i:
			for j in range(i, n + 1, i):
				ret[j] = ret[j] // i * (i - 1)
	return ret

def gen_fact_mod_prime(p):
	ret = [1] * p
	for i in range(2, p):
		ret[i] = ret[i - 1] * i % p
	return ret

def fact_mod(n, p, facts):
	if n == 0:
		return (1, 0)
	(a, e) = fact_mod(n // p, p, facts)
	e += n // p
	if n // p % 2 != 0:
		return (a * (p - facts[n % p]) % p, e)
	return (a * facts[n % p] % p, e)

def comb_mod(n, k, p):
	if n < 0 or k < 0 or n < k:
		return 0
	facts = gen_fact_mod_prime(p)
	(a1, e1) = fact_mod(n, p, facts)
	(a2, e2) = fact_mod(k, p, facts)
	(a3, e3) = fact_mod(n - k, p, facts)
	if e1 > e2 + e3:
		return 0
	else:
		return a1 * modinv(a2 * a3 % p, p) % p

def chinese_remainder_theory_for2(x, a, y, b):
	(g, s, t) = exgcd(x, y)
	if a % g != b % g:
		return (0, -1)
	else:
		return (mod(s * b * x + t * a * y, x * y) // g, x * y // g)

def chinese_remainder_theory(xs, ass):
	ret = (ass[0], xs[0])
	for i in range(1, len(xs)):
		ret = chinese_remainder_theory_for2(ret[1], ret[0], xs[i], ass[i])
		if ret[1] == -1:
			break
	return ret

def comb_mod2(n, r, m, pa, facts1):
	(p, a) = pa

	def n_fact_fact(n):
		if n is 0 or n is 1:
			return 1
		elif n < m:
			return facts1[n] * n_fact_fact(n // p) % m
		else:
			a = facts1[m - 1]
			b = facts1[n % m]
			c = n_fact_fact(n // p)
			return pow(a, n // m, m) * b * c % m

	def get_power(n, p):
		ret = 0
		while n > 0:
			ret += n // p
			n //= p
		return ret
	b = get_power(n, p) - get_power(r, p) - get_power(n - r, p)
	if b >= a:
		return 0
	m1 = n_fact_fact(n)
	m2 = n_fact_fact(r)
	m3 = n_fact_fact(n - r)
	return p ** b * m1 * modinv_table[m2, m] * modinv_table[m3, m] % m

def solve(n, r, fs1, fs2, xss):
	xs = [27, 11, 13, 37]
	ass = [comb_mod2(n, r, xs[i], xss[i], fs1[i]) for i in range(4)]
	return chinese_remainder_theory(xs, ass)

def init(xs):
	(ret1, ret2) = ([], [])
	for i in range(len(xs)):
		(p, a) = xs[i]
		m = p ** a
		(t1, t2) = ([1], [1])
		for i in range(1, p):
			t2.append(t2[-1] * i % p)
		for i in range(1, m):
			if gcd(i, m) == 1:
				t1.append(t1[-1] * i % m)
			else:
				t1.append(t1[-1])
		ret1.append(t1)
		ret2.append(t2)
	return (ret1, ret2)
modinv_table = {}
modulo = 142857
for x in {27, 11, 13, 37}:
	for y in range(1, x):
		if gcd(x, y) == 1:
			modinv_table[y, x] = modinv(y, x)

def main():
	n = int(input())
	xss = [(3, 3), (11, 1), (13, 1), (37, 1)]
	(facts1, facts2) = init(xss)
	for _ in range(n):
		(n, r) = map(int, input().strip().split())
		print(solve(n, r, facts1, facts2, xss)[0])
main()


mod_27 = [1] * 27
for i in range(1, 27):
	n = i
	if n % 3 == 0:
		n = 1
	mod_27[i] = mod_27[i - 1] * n % 27

def res_27(n):
	res27 = 1
	while n != 0:
		res27 = res27 * (-1) ** (n // 27 % 2) * mod_27[n % 27] % 27
		n = n // 3
	return res27

def gen_tab_mod(x):
	mod_tab = [1] * x
	for i in range(1, x):
		mod_tab[i] = mod_tab[i - 1] * i % x
	return mod_tab
tab_11 = gen_tab_mod(11)
tab_13 = gen_tab_mod(13)
tab_37 = gen_tab_mod(37)

def calcule_mod(n, mod):
	if mod == 11:
		mod_tab = tab_11
	elif mod == 13:
		mod_tab = tab_13
	elif mod == 37:
		mod_tab = tab_37
	res = 1
	while n != 0:
		res = res * (-1) ** (n // mod % 2) * mod_tab[n % mod] % mod
		n = n // mod
	return res

def inv_mod(a, n):
	t = n
	x = 1
	xx = 0
	while n != 0:
		q = a // n
		(a, n) = (n, a % n)
		(xx, x) = (x - q * xx, xx)
	if a != 1:
		return 0
	return x % t

def chineese_rem(reste, modolu):
	if len(reste) == 1:
		return reste[0] % modolu[0]
	k = (reste[0] + modolu[0] * inv_mod(modolu[0], modolu[1]) * (reste[1] - reste[0])) % (modolu[0] * modolu[1])
	if len(reste) == 2:
		return k
	modolu[1] *= modolu[0]
	reste[1] = k
	return chineese_rem(reste[1:], modolu[1:])

def puiss_max(nb, p):
	s = 0
	n = nb // p
	while n != 0:
		s = s + n
		n = n // p
	return s

def nCr(n, r):
	r1 = res_27(n) * inv_mod(res_27(r) * res_27(n - r), 27) % 27
	max_3 = puiss_max(n, 3) - puiss_max(n - r, 3) - puiss_max(r, 3)
	r1 = r1 * pow(3, max_3, 27) % 27
	r2 = calcule_mod(n, 11) * inv_mod(calcule_mod(n - r, 11) * calcule_mod(r, 11), 11) % 11
	max_11 = puiss_max(n, 11) - puiss_max(n - r, 11) - puiss_max(r, 11)
	r2 = r2 * pow(11, max_11, 11) % 11
	r3 = calcule_mod(n, 13) * inv_mod(calcule_mod(n - r, 13) * calcule_mod(r, 13), 13) % 13
	max_13 = puiss_max(n, 13) - puiss_max(n - r, 13) - puiss_max(r, 13)
	r3 = r3 * pow(13, max_13, 13) % 13
	r4 = calcule_mod(n, 37) * inv_mod(calcule_mod(n - r, 37) * calcule_mod(r, 37), 37) % 37
	max_37 = puiss_max(n, 37) - puiss_max(n - r, 37) - puiss_max(r, 37)
	r4 = r4 * pow(37, max_37, 37) % 37
	a = [r1, r2, r3, r4]
	b = [27, 11, 13, 37]
	return chineese_rem(a, b)
t = int(input())
for _ in range(t):
	(n, r) = map(int, input().split())
	print(nCr(n, r))


invList = {27: [0, 1, 14, -1, 7, 11, -1, 4, 17, -1, 19, 5, -1, 25, 2, -1, 22, 8, -1, 10, 23, -1, 16, 20, -1, 13, 26], 11: [0, 1, 6, 4, 3, 9, 2, 8, 7, 5, 10], 13: [0, 1, 7, 9, 10, 8, 11, 2, 5, 3, 4, 6, 12], 37: [0, 1, 19, 25, 28, 15, 31, 16, 14, 33, 26, 27, 34, 20, 8, 5, 7, 24, 35, 2, 13, 30, 32, 29, 17, 3, 10, 11, 4, 23, 21, 6, 22, 9, 12, 18, 36]}
modu = 142857
primes = [(3, 3), (11, 1), (13, 1), (37, 1)]
tmp = [137566, 103896, 109890, 77220]

def myChineseRemainder(A):
	return sum((tmp[i] * A[i] for i in range(4))) % modu
from fractions import Fraction
nFactDict = {11: [1, 1, 2, 6, 2, 10, 5, 2, 5, 1, 10], 27: [1, 1, 2, 2, 8, 13, 13, 10, 26, 26, 17, 25, 25, 1, 14, 14, 8, 1, 1, 19, 2, 2, 17, 13, 13, 1, 26], 13: [1, 1, 2, 6, 11, 3, 5, 9, 7, 11, 6, 1, 12], 37: [1, 1, 2, 6, 24, 9, 17, 8, 27, 21, 25, 16, 7, 17, 16, 18, 29, 12, 31, 34, 14, 35, 30, 24, 21, 7, 34, 30, 26, 14, 13, 33, 20, 31, 18, 1, 36]}

def carryList(k, r, p):
	(kl, rl) = ([], [])
	while k > 0 or r > 0:
		kl.append(k % p)
		rl.append(r % p)
		k = k // p
		r = r // p
	carLi = [(kl[0] + rl[0]) // p]
	for i in range(1, len(kl)):
		carLi.append((carLi[-1] + kl[i] + rl[i]) // p)
	return carLi

def genLucas(n, r, p, q):
	k = n - r
	e = carryList(k, r, p)
	toCarry = sum(e)
	if q <= toCarry:
		return 0
	else:
		toCarryq = sum(e[q - 1:])
		rt = 1
		pr = pow(p, q)
		while n > 0 or r > 0 or k > 0:
			rt *= nFactDict[pr][n % pr] * invList[pr][nFactDict[pr][r % pr]] * invList[pr][nFactDict[pr][k % pr]]
			(n, r, k) = (n // p, r // p, k // p)
		rt *= p ** toCarry * pow(-1, toCarryq)
		return rt % pr

def solve(n, r):
	if n < r:
		return 0
	if n - r < r:
		r = n - r
	if n == 0:
		return 1
	rt = []
	for (p, q) in primes:
		rt.append(genLucas(n, r, p, q))
	return myChineseRemainder(rt)

def hackerRank():
	for t in range(int(input())):
		(n, r) = [int(i) for i in input().split()]
		print(solve(n, r))
hackerRank()


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import math
import os
import random
import re
import sys
import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


invList = {27: [0, 1, 14, -1, 7, 11, -1, 4, 17, -1, 19, 5, -1, 25, 2, -1, 22, 8, -1, 10, 23, -1, 16, 20, -1, 13, 26], 11: [0, 1, 6, 4, 3, 9, 2, 8, 7, 5, 10], 13: [0, 1, 7, 9, 10, 8, 11, 2, 5, 3, 4, 6, 12], 37: [0, 1, 19, 25, 28, 15, 31, 16, 14, 33, 26, 27, 34, 20, 8, 5, 7, 24, 35, 2, 13, 30, 32, 29, 17, 3, 10, 11, 4, 23, 21, 6, 22, 9, 12, 18, 36]}
modu = 142857
primes = [(3, 3), (11, 1), (13, 1), (37, 1)]
tmp = [137566, 103896, 109890, 77220]

def myChineseRemainder(A):
	return sum((tmp[i] * A[i] for i in range(4))) % modu
from fractions import Fraction
nFactDict = {11: [1, 1, 2, 6, 2, 10, 5, 2, 5, 1, 10], 27: [1, 1, 2, 2, 8, 13, 13, 10, 26, 26, 17, 25, 25, 1, 14, 14, 8, 1, 1, 19, 2, 2, 17, 13, 13, 1, 26], 13: [1, 1, 2, 6, 11, 3, 5, 9, 7, 11, 6, 1, 12], 37: [1, 1, 2, 6, 24, 9, 17, 8, 27, 21, 25, 16, 7, 17, 16, 18, 29, 12, 31, 34, 14, 35, 30, 24, 21, 7, 34, 30, 26, 14, 13, 33, 20, 31, 18, 1, 36]}

def carryList(k, r, p):
	(kl, rl) = ([], [])
	while k > 0 or r > 0:
		kl.append(k % p)
		rl.append(r % p)
		k = k // p
		r = r // p
	carLi = [(kl[0] + rl[0]) // p]
	for i in range(1, len(kl)):
		carLi.append((carLi[-1] + kl[i] + rl[i]) // p)
	return carLi

def genLucas(n, r, p, q):
	k = n - r
	e = carryList(k, r, p)
	toCarry = sum(e)
	if q <= toCarry:
		return 0
	else:
		toCarryq = sum(e[q - 1:])
		rt = 1
		pr = pow(p, q)
		while n > 0 or r > 0 or k > 0:
			rt *= nFactDict[pr][n % pr] * invList[pr][nFactDict[pr][r % pr]] * invList[pr][nFactDict[pr][k % pr]]
			(n, r, k) = (n // p, r // p, k // p)
		rt *= p ** toCarry * pow(-1, toCarryq)
		return rt % pr

def solve(n, r):
	if n < r:
		return 0
	if n - r < r:
		r = n - r
	if n == 0:
		return 1
	rt = []
	for (p, q) in primes:
		rt.append(genLucas(n, r, p, q))
	return myChineseRemainder(rt)

def hackerRank():
	for t in range(int(input())):
		(n, r) = [int(i) for i in input().split()]
		print(solve(n, r))
hackerRank()


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))


import functools
import sys

class MathEngine:

	def __init__(self):
		self.pe = None
		self._cache_factorial = {}

	def binomial(self, n, m):
		if m == 0:
			if n == 0:
				return 0
			else:
				return 1
		b = self.factor_binomial(n, m)
		result = self.factors_to_integer(b)
		return result

	def binomial_prime_power(self, n, m, p):
		result = self._number_of_carries(n - m, m, p)
		return result

	def factor_binomial(self, n, m):
		result = {}
		for p in self.primes(n + 1):
			power = self.binomial_prime_power(n, m, p)
			if power > 0:
				result[p] = power
		return result

	def factor_integer(self, n):
		result = {}
		for p in self.primes(n + 1):
			power = 0
			while n % p == 0 and n != 1:
				n = n // p
				power += 1
			if power > 0:
				result[p] = power
			if n == 1:
				break
		return result

	def factor_factorial(self, n):
		result = {}
		for prime in self.primes(n + 1):
			k = n
			result[prime] = 0
			while k > 0:
				k = k // prime
				result[prime] += k
		return result

	def factorial(self, n, p=0, mod=0):
		_cache_key = (n, p, mod)
		if _cache_key in self._cache_factorial:
			return self._cache_factorial[_cache_key]
		result = 1
		if p == 0:
			if mod == 0:
				for k in range(1, n + 1):
					result *= k
			else:
				for k in range(1, n + 1):
					result = result * k % mod
		elif mod == 0:
			for k in range(1, n + 1):
				if k % p != 0:
					result *= k
		else:
			for k in range(1, n + 1):
				if k % p != 0:
					result = result * k % mod
		self._cache_factorial[_cache_key] = result
		return result

	def factors_to_integer(self, n):
		result = 1
		for (prime, power) in n.items():
			result *= prime ** power
		return result

	def gcd_extended(self, a, b):
		(s, s_old) = (0, 1)
		(t, t_old) = (1, 0)
		(r, r_old) = (b, a)
		while r != 0:
			k = r_old // r
			(r_old, r) = (r, r_old - k * r)
			(s_old, s) = (s, s_old - k * s)
			(t_old, t) = (t, t_old - k * t)
		return (r_old, s_old, t_old)

	def integer_digits(self, n, p):
		result = []
		while n > 0:
			result.append(n % p)
			n //= p
		return result

	def integer_digits_len(self, n, p):
		result = 1
		while n > 0:
			result += 1
			n //= p
		return result

	def mod_binomial(self, n, m, mod):
		binomial_factors = self.factor_binomial(n, m)
		result = 1
		for (prime, exp) in binomial_factors.items():
			result = result * pow(prime, exp, mod) % mod
		return result

	def mod_binomial_lucas(self, n, m, p, q=1):
		e0 = self.binomial_prime_power(n, m, p)
		qq = q - e0
		if qq < 0:
			return 0
		(p_e0, p_q, p_qq) = (p ** e0, p ** q, p ** qq)
		r = n - m
		d = self.integer_digits_len(n, p)
		(_ni, _mi, _ri) = (n, m, r)
		_least_positive_residue_n = []
		_least_positive_residue_m = []
		_least_positive_residue_r = []
		for i in range(0, d):
			_least_positive_residue_n.append(_ni % p_qq)
			_least_positive_residue_m.append(_mi % p_qq)
			_least_positive_residue_r.append(_ri % p_qq)
			_ni //= p
			_mi //= p
			_ri //= p
		_factorial = [1]
		curr = 1
		for x in range(1, p_qq):
			if x % p != 0:
				curr = curr * x % p_qq
			_factorial.append(curr)
		(result, i) = (1, 0)
		(aaa, bbb, ccc) = (1, 1, 1)
		for i in range(0, d):
			aa = _least_positive_residue_n[i]
			bb = _least_positive_residue_m[i]
			cc = _least_positive_residue_r[i]
			a = _factorial[aa]
			b = _factorial[bb]
			c = _factorial[cc]
			aaa = aaa * a % p_qq
			bbb = bbb * b % p_qq
			ccc = ccc * c % p_qq
			i += 1
		result = aaa * self.mod_inverse(bbb * ccc % p_qq, p_qq) % p_qq
		if p != 2 or qq < 3:
			result *= (-1) ** self._number_of_carries(m, r, p, qq - 1)
		result = result * p_e0 % p_q
		return result

	def _least_positive_residue(self, n, j, p, q):
		result = n // p ** j % p ** q
		return result

	def _number_of_carries(self, m, r, p, j=0):
		(result, carry, jj) = (0, 0, 0)
		while m > 0 or r > 0:
			if m % p + r % p + carry >= p:
				carry = 1
			else:
				carry = 0
			if jj >= j:
				result += carry
			(m, r) = (m // p, r // p)
			jj += 1
		return result

	def mod_chinese(self, rs):
		n = functools.reduce(lambda a, b: a * b, rs.keys(), 1)
		result = 0
		for (ni, ai) in rs.items():
			nn = n // ni
			(_, ri, si) = self.gcd_extended(ni, nn)
			ei = si * nn
			result += ai * ei
		result = result % n
		return result

	def mod_inverse(self, n, mod):
		(r, s, t) = self.gcd_extended(n, mod)
		if r != 1:
			return None
		return s

	def primes(self, n):
		return self.pe.primes(n)

class Solver(object):

	def __init__(self):
		self.me = MathEngine()
		self.mod = 142857

	def solution(self, n, r):
		mod_factors = {3: 3, 11: 1, 13: 1, 37: 1}
		mods = {p ** q: self.me.mod_binomial_lucas(n, r, p, q) for (p, q) in mod_factors.items()}
		result = self.me.mod_chinese(mods)
		return result
T = int(sys.stdin.readline())
solver = Solver()
for i in range(T):
	(n, r) = [int(num) for num in sys.stdin.readline().split()]
	print(solver.solution(n, r))