Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

You are given an integer N and a digit D. Find the minimum non-negetive integer you should add to N such that the final value of N does not contain the digit D.

------ Input Format ------ 

- The first line contains T denoting the number of test cases. Then the test cases follow.
- Each test case contains two integers N and D on a single line denoting the original number and the digit you need to avoid.

------ Output Format ------ 

For each test case, output on a single line the minimum non-negetive integer you should add to N.

------ Constraints ------ 

$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{9}$
$0 ≤ D ≤ 9$

------ subtasks ------ 

Subtask 1 (100 points): Original constraints

----- Sample Input 1 ------ 
5
21 5
8 8
100 0
5925 9
434356 3

----- Sample Output 1 ------ 
0
1
11
75
5644

----- explanation 1 ------ 
Test case $1$: $N = 21$ does not contain the digit $D = 5$. Hence there is no need to add any integers to $N$. 

Test case $2$: If $1$ is added to $N = 8$, it becomes equal to $9$, which does not contain the digit $D = 8$.

Test case $3$: The minimum integer you should add to $N = 100$ such that the final value of $N$ does not contain the digit $D = 0$ is $11$.

Test case $5$: The minimum integer which is greater than $434356$ and does not contain the digit $D = 3$ is $440000$. So we should add $440000 - 434356 = 5644$.

Difficulty: MEDIUM_HARD
Tags: Mathematics

Starter code:

Solutions:
debug = False

def debug_msg(*args, **kwargs):
	if debug:
		print(*args, **kwargs)
T = int(input())
for temp in range(T):
	(N, D) = map(int, input().split())
	new_N = N
	current_digit = 1
	debug_msg(f'N={N}, D={D}')
	while new_N >= current_digit:
		debug_msg(f'current_digit={current_digit}')
		digit = new_N // current_digit % 10
		if digit == D:
			if current_digit == 1:
				new_N += 1
				current_digit *= 10
			else:
				new_N = (new_N // current_digit + 1) * current_digit
				current_digit = 1
		else:
			current_digit *= 10
		debug_msg(f'new_N={new_N}')
	print(new_N - N)


for i in range(int(input())):
	(n, d) = map(int, input().split())
	num = n
	pos = 0
	ans = 0
	while num > 0:
		dig = num % 10
		num = num // 10
		pos += 1
		if dig == d:
			num *= 10 ** pos
			num += (d + 1) * pow(10, pos - 1)
			ans = num - n
			pos = 0
	print(ans)


def get_dgts(num):
	dgts = []
	while num:
		dgts.append(num % 10)
		num //= 10
	return dgts[::-1]

def has_dgts(num, dgt):
	return dgt in get_dgts(num)

def nextnum(num, digit):
	dgts = get_dgts(num)
	lent = len(dgts)
	for i in range(lent):
		if dgts[i] == digit:
			num += 10 ** (lent - i - 1)
			num -= num % 10 ** (lent - i - 1)
			if has_dgts(num, digit):
				return nextnum(num, digit)
			break
	return num
for i in range(int(input())):
	(num, digit) = list(map(int, input().split()))
	print(nextnum(num, digit) - num)


def counting(idx, k, num, s, count):
	for j in range(idx + 1, k):
		count = count * 10 + (num - int(s[j]))
	return count

def nine(a):
	i = a.find('9')
	if i == 0:
		return str(pow(10, len(a)))
	else:
		return a[:i - 1] + str(int(a[i - 1]) + 1) + '0' * len(a[i:len(a)])
for _ in range(int(input())):
	(n, d) = map(int, input().split())
	s = str(n)
	count = 0
	if n == d:
		print('1')
	else:
		idx = s.find(str(d))
		if idx == -1:
			print('0')
		else:
			k = len(s)
			if d == 0:
				count = counting(idx, k, 10, s, 0)
				print(count + 1)
			elif d != 9:
				count = counting(idx, k, 9, s, 0)
				print(count + 1)
			else:
				t = False
				if '9' in s:
					t = True
				dup = s[:]
				while t == True:
					dup = nine(dup)
					if '9' not in dup:
						t = False
				print(int(dup) - n)


def snek(n, d, ans):
	if str(d) == str(n):
		return 1
	elif str(d) not in str(n):
		return ans
	else:
		p = str(n)[::-1].index(str(d))
		r = n % 10 ** p
		ans += 10 ** p - r
		n += 10 ** p - r
		return snek(n, d, ans)
t = int(input())
for i in range(t):
	(n, d) = map(int, input().split())
	ans = 0
	print(snek(n, d, ans))


t = int(input())

def digit_removal(n, d, ans):
	if str(d) == str(n):
		return 1
	elif str(d) not in str(n):
		return ans
	else:
		pos = str(n)[::-1].index(str(d))
		rem = n % 10 ** pos
		ans += 10 ** pos - rem
		n += 10 ** pos - rem
		return digit_removal(n, d, ans)
for _ in range(t):
	(n, d) = map(int, input().split())
	ans = 0
	print(digit_removal(n, d, ans))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s_ = str(a)
	l = len(s_)
	s = []
	for i in range(l):
		s.append(s_[i])
	s1 = str(b)
	index = -1
	for i in range(l):
		if s[i] == s1:
			index = i
			break
	if index == -1:
		print(0)
	else:
		if s[index] == '9':
			x = index
			while True:
				if x - 1 >= 0:
					s[x - 1] = str(int(s[x - 1]) + 1)
					s[x] = '0'
					if s[x - 1] != '9':
						break
					x -= 1
				else:
					s[0] = '10'
					break
			for i in range(index + 1, l):
				s[i] = '0'
		elif s[index] == '0':
			for i in range(index, l):
				s[i] = '1'
		else:
			s[index] = str(int(s[index]) + 1)
			for i in range(index + 1, l):
				s[i] = '0'
		r = ''
		for i in s:
			r += i
		print(int(r) - a)


t = int(input())
for _ in range(t):
	(n, d) = map(int, input().split())
	m = n
	(count, ans) = (0, 0)
	while m > 0:
		a = m % 10
		m = m // 10
		count += 1
		if a == d:
			m = m * 10 ** count
			m += (d + 1) * 10 ** (count - 1)
			ans = m - n
			count = 0
	print(ans)


def fun(n, d):
	t = n
	s = t
	pos = 0
	count = 0
	while s > 0:
		r = s % 10
		count += 1
		if r == d:
			pos = count
			break
		s = s // 10
	if pos > 0:
		t = n // 10 ** pos * 10 ** pos + (r + 1) * 10 ** (pos - 1)
		return fun(t, d)
	else:
		return t
for _ in range(int(input())):
	(n, d) = map(int, input().split())
	s = fun(n, d)
	print(s - n)


t = int(input())
for i in range(t):
	(n, d) = map(int, input().split())
	q = n
	ans2 = 0
	a = str(n)
	if str(d) not in a:
		print('0')
	else:
		count = 0
		while n != 0:
			rem = n % 10
			n = n // 10
			count += 1
			if rem == d:
				n = n * 10 ** count + (d + 1) * 10 ** (count - 1)
				ans2 = n - q
				count = 0
		print(int(ans2))


import math
t = int(input())
for _ in range(t):
	(n, d) = [int(x) for x in input().split(' ')]
	New = n
	ans = 0
	c = 0
	while New > 0:
		rem = New % 10
		New = New // 10
		c = c + 1
		if rem == d:
			New = New * math.pow(10, c) + (rem + 1) * math.pow(10, c - 1)
			ans = New - n
			c = 0
	print(int(ans))


def check(l, d):
	if d in l:
		ind = l.index(d)
		if ind == 0:
			l[ind] = str(int(l[ind]) + 1)
		elif int(l[ind]) + 1 > 9:
			l[ind] = str((int(l[ind]) + 1) % 10)
			l[ind - 1] = str(int(l[ind - 1]) + 1)
		else:
			l[ind] = str(int(l[ind]) + 1)
		if len(l[ind + 1:]) > 0:
			for i in range(len(l[ind + 1:])):
				l[ind + 1 + i] = '0'
		return check(l, d)
	else:
		num = int(('{}' * len(l)).format(*l))
		return num
for _ in range(int(input())):
	(n, d) = input().split()
	l = list(n)
	c = check(l, d)
	mini = c - int(n)
	print(mini)


import re

def test(n, d):
	if n[0] == '0':
		n = re.sub('0+(.+)', '\\1', n)
	new_n = ''
	if len(n) == 1:
		if n == d:
			new_n = str(int(n) + 1)
		else:
			new_n = n
	elif d == '9':
		idx = n.find('9')
		if idx > 0:
			new_n = n[:idx - 1] + str(int(n[idx - 1]) + 1) + '0' * (len(n) - idx)
		if idx == 0:
			new_n = '1' + '0' * len(n)
	else:
		idx = n.find(d)
		r = '0'
		if d == '0':
			r = '1'
		if idx >= 0:
			new_n = n[:idx] + str(int(n[idx]) + 1) + r * (len(n) - idx - 1)
	if new_n == '':
		return 0
	elif d in new_n:
		return int(new_n) - int(n) + test(new_n, d)
	else:
		return int(new_n) - int(n)
for i in range(int(input())):
	(n, d) = input().split()
	print(test(n, d))


a = int(input())
for i in range(a):
	(b, c) = map(int, input().split())
	f = 0
	e = 0
	g = b
	u = 0
	while b != 0:
		d = b % 10
		b = b // 10
		f += 1
		if d == c:
			b = b * pow(10, f) + (d + 1) * pow(10, f - 1)
			u = b - g
			f = 0
	print(u)


t = int(input())
for i in range(t):
	(n, d) = list(map(int, input().split()))
	num = n
	pos = 0
	add = 0
	while num > 0:
		digit = num % 10
		num = num // 10
		pos += 1
		if digit == d:
			num = num * pow(10, pos)
			num += (digit + 1) * pow(10, pos - 1)
			add = num - n
			pos = 0
	print(add)


import sys
import math
for i in range(int(sys.stdin.readline())):
	(x, y) = map(int, sys.stdin.readline().split())
	pos = 0
	add = 0
	num = x
	while num > 0:
		digit = num % 10
		num = num // 10
		pos += 1
		if digit == y:
			num = num * int(math.pow(10, pos))
			num += (digit + 1) * int(math.pow(10, pos - 1))
			add = num - x
			pos = 0
	print(add)


import sys
import math
for i in range(int(sys.stdin.readline())):
	(x, y) = map(int, sys.stdin.readline().split())
	pos = 0
	add = 0
	num = x
	while num > 0:
		digit = num % 10
		num = num // 10
		pos += 1
		if digit == y:
			num = num * int(math.pow(10, pos))
			num += (digit + 1) * int(math.pow(10, pos - 1))
			add = num - x
			pos = 0
	sys.stdout.write(str(add) + '\n')


def getTheFill(n, x, start):
	temp = n
	track8s = False
	if x == 9:
		track8s = True
		paddingFor8 = 0
	for y in reversed(range(start)):
		power = pow(10, y)
		num = temp // power
		if num == x:
			return ((paddingFor8 if track8s else 0) + power - temp % power, y)
		else:
			temp = temp - num * power
		if track8s:
			if num == 8:
				paddingFor8 += power
			else:
				paddingFor8 = 0
	return (None, None)

def coverTheZeros(i):
	fill = 0
	for x in range(i):
		fill += pow(10, x)
	return fill

def test(ipt):
	a = ipt.split()
	n = a[0]
	n = n.lstrip('0')
	if n == '':
		n = '0'
	l = len(n)
	n = int(n)
	x = int(a[1])
	(fill, i) = getTheFill(n, x, l)
	if i != None:
		if x == 0:
			fill += coverTheZeros(i)
			return fill
		else:
			return fill
	else:
		return 0
t = int(input())
for _ in range(t):
	print(test(input()))


T = int(input())
for i in range(0, T):
	(N, D) = map(int, input().split())
	n = N
	pos = 0
	add = 0
	while n > 0:
		d = n % 10
		n //= 10
		pos += 1
		if d == D:
			n = n * pow(10, pos)
			n += (d + 1) * pow(10, pos - 1)
			add = n - N
			pos = 0
	print(add)


mod = 10 ** 9 + 7
for _ in range(int(input())):
	(n, d) = map(int, input().split())
	res = 0
	num = n
	digit = 0
	count = 0
	while num > 0:
		digit = num % 10
		num //= 10
		count += 1
		if digit == d:
			num = num * pow(10, count, mod) + (digit + 1) * pow(10, count - 1, mod)
			res = num - n
			count = 0
	print(res)


tests = int(input())
for t0 in range(tests):
	[n, d] = [int(i) for i in input().split()]
	ns = str(n)
	ds = str(d)
	if ds not in ns:
		print(0)
	else:
		l = len(ns)
		if d != 0 and d != 9:
			i = ns.index(ds)
			if l - i >= 1:
				ans = ns[:i] + str(int(ns[i]) + 1) + '0' * (l - i - 1)
			else:
				ans = ns[:i] + str(int(ns[i]) + 1)
		elif d == 0:
			i = ns.index(ds)
			if l - i >= 1:
				ans = ns[:i] + str(int(ns[i]) + 1) + '1' * (l - i - 1)
			else:
				ans = ns[:i] + str(int(ns[i]) + 1)
		elif d == 9:
			i = ns.index('9')
			if i == 0:
				ans = '10' + '0' * (l - 1)
			elif ns[i - 1] != '8':
				ans = ns[:i - 1] + str(int(ns[i - 1]) + 1) + '0' * (l - i)
			else:
				j = i - 1
				while j >= 0:
					if ns[j] != '8':
						break
					j -= 1
				j += 1
				if j == 0:
					ans = '1' + '0' * l
				else:
					ans = ns[:j - 1] + str(int(ns[j - 1]) + 1) + '0' * (l - j)
		print(int(ans) - n)


for _ in range(0, int(input())):
	[n, d] = input().split(' ')
	nC = n
	while n.count(d) > 0:
		firstIndex = n.index(d)
		firstHalf = n[0:firstIndex + 1]
		lastHalf = len(n) - firstIndex - 1
		if lastHalf < 0:
			lastHalf = 0
		firstHalf = str(int(firstHalf) + 1)
		if int(d) > 0:
			lastHalf = '0' * lastHalf
		else:
			lastHalf = '1' * lastHalf
		n = firstHalf + lastHalf
	print(int(n) - int(nC))


T = int(input())
for _ in range(T):
	(n, d) = input().split(' ')
	res = ''
	con = 0
	f = n[::-1].rfind(d)
	for (idx, c) in enumerate(n[::-1]):
		if idx == f:
			_t = int(c) + 1
		elif idx > f:
			_t = int(c)
		elif d == '0':
			if c == '0':
				_t = 1
			else:
				_t = 11
		else:
			_t = 10
		_res = _t - con - int(c)
		if _res < 0:
			if con + int(c) == int(d):
				_res = 1
			else:
				_res = 0
		elif _res == 10:
			_res = 0
		res += str(_res)
		con = 1 if _res + int(c) + con >= 10 else 0
	print(int(res[::-1]))


def get_update_arr(n, d):
	t = []
	_t = False
	for c in n:
		if not _t:
			if c == d:
				t.append(int(c) + 1)
				_t = True
			else:
				t.append(int(c))
		elif d == '0':
			if c == '0':
				t.append(1)
			else:
				t.append(11)
		else:
			t.append(10)
	return t
T = int(input())
for _ in range(T):
	(n, d) = input().split(' ')
	t = get_update_arr(n, d)
	res = ''
	con = 0
	for (c, _t) in zip(n[::-1], t[::-1]):
		_res = _t - con - int(c)
		if _res < 0:
			if con + int(c) == int(d):
				_res = 1
			else:
				_res = 0
		elif _res == 10:
			_res = 0
		res += str(_res)
		con = 1 if _res + int(c) + con >= 10 else 0
	print(int(res[::-1]))


for _ in range(int(input())):
	(N, D) = list(map(int, input().split()))
	pos = 0
	add = 0
	n = N
	while n > 0:
		rem = n % 10
		n = n // 10
		pos += 1
		if rem == D:
			n = n * 10 ** pos
			n += (rem + 1) * 10 ** (pos - 1)
			add = n - N
			pos = 0
	print(add)


for i in range(int(input())):
	(n, d) = list(map(int, input().split()))
	num = n
	pos = 0
	add = 0
	while num > 0:
		dig = num % 10
		num = num // 10
		pos += 1
		if dig == d:
			num *= 10 ** pos
			num += (dig + 1) * 10 ** (pos - 1)
			add = num - n
			pos = 0
	print(add)


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	if str(d) not in str(n):
		print('0')
		continue
	num = n
	pos = 0
	added_num = 0
	while num > 0:
		digit = num % 10
		num = num // 10
		pos += 1
		if digit == d:
			num = num * 10 ** pos
			num = num + (digit + 1) * 10 ** (pos - 1)
			added_num = num
			pos = 0
	print(abs(n - added_num))


t = int(input())
while t != 0:
	(n, d) = map(int, input().split())
	num = n
	pos = 0
	ans = 0
	while num > 0:
		dig = num % 10
		num = num // 10
		pos += 1
		if dig == d:
			num = num * pow(10, pos)
			num += (d + 1) * pow(10, pos - 1)
			ans = num - n
			pos = 0
	print(ans)
	t -= 1


import math

def giveMeResult(copyOfNum, digit):
	(maxLen, digits, answer, lenOfNum) = (0, [], 0, 0)
	while copyOfNum:
		lenOfNum += 1
		temp = copyOfNum % 10
		if temp == digit:
			maxLen = lenOfNum
		digits.append(temp)
		copyOfNum = copyOfNum // 10
	digits.reverse()
	if maxLen:
		powerFactor = maxLen - 2
		if digit == 0:
			for i in range(maxLen - 1, -1, -1):
				answer += pow(10, i)
		else:
			answer = pow(10, maxLen - 1)
		for i in range(lenOfNum - maxLen + 1, lenOfNum):
			answer -= digits[i] * pow(10, powerFactor)
			powerFactor -= 1
		return int(answer)
	else:
		return 0
for testCase in range(int(input())):
	(number, digit) = map(int, input().split())
	d = math.floor(math.log10(number) + 1)
	testResult = giveMeResult(number, digit)
	for i in range(d - 1):
		testResult += giveMeResult(testResult + number, digit)
	print(testResult)


def checkrem(n, d):
	c = 0
	n1 = n
	ans = 0
	while n1 > 0:
		r = n1 % 10
		c += 1
		n1 = int(n1 / 10)
		if r == d:
			n1 = n1 * 10 ** c + (r + 1) * 10 ** (c - 1)
			ans = n1 - n
			c = 0
	return ans
for i in range(int(input())):
	(n, d) = [int(x) for x in input().split()]
	print(checkrem(n, d))


def calculateMinDigits(N, D):
	temp = N
	count = 0
	j = 0
	while temp > 0:
		remainder = temp % 10
		temp = temp // 10
		j = j + 1
		if remainder == D:
			temp = temp * pow(10, j) + (remainder + 1) * pow(10, j - 1)
			count = temp - N
			j = 0
	return int(count)
t = int(input())
for i in range(t):
	(N, D) = map(int, input().strip().split())
	minimumDigitRequired = calculateMinDigits(N, D)
	print(minimumDigitRequired)


for _ in range(int(input())):
	(n, d) = list(map(str, input().split()))
	a = [x for x in n]
	if a.count(d) == 0:
		print(0)
	elif len(a) == 1 and a.count(d) == 1:
		if len(a) == 1:
			print(1)
	elif d == '0':
		l = []
		b = a.index('0')
		for i in range(b, len(a)):
			a[i] = '1'
		b = ''.join(a)
		print(int(b) - int(n))
	elif d == '9':
		while True:
			b = a.index('9')
			if b == 0:
				a.insert(0, '1')
				for i in range(b + 1, len(a)):
					a[i] = '0'
			else:
				a[b - 1] = str(int(a[b - 1]) + 1)
				for i in range(b, len(a)):
					a[i] = '0'
			c = ''.join(a)
			z = list(c.split())
			if c.count(d) == 0:
				print(int(c) - int(n))
				break
			else:
				continue
	else:
		s = ''
		b = a.index(d)
		a[b] = str(int(a[b]) + 1)
		s = n[:b] + a[b]
		for i in range(len(a) - len(s)):
			s += '0'
		c = ''.join(s)
		print(int(c) - int(n))


from sys import stdin
inp = stdin.readline
T = int(inp())
for tx in range(T):
	(N, D) = inp().split()
	sp = N.split(D, 1)
	if len(sp) == 1:
		print(0)
	else:
		(hi, lo) = sp
		base = len(lo)
		add = 10 ** base - int('0' + lo)
		if D == '0':
			add += (10 ** base - 1) // 9
		elif D == '9':
			for c in reversed(hi):
				if c != '8':
					break
				base += 1
				add += 10 ** base
		print(add)


try:
	for i in range(int(input())):
		(num, k) = [int(itm) for itm in input().split()]
		n = num
		(add, p) = (0, 0)
		while n > 0:
			dig = n % 10
			n = n // 10
			p += 1
			if dig == k:
				n = n * 10 ** p
				n += (k + 1) * 10 ** (p - 1)
				add = n - num
				p = 0
		print(add)
except EOFError:
	exit()


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	act_n = n
	sn = str(n)
	sd = str(d)
	while sd in sn:
		x = sn.index(sd)
		m = 10 ** (len(sn) - 1 - x)
		if x != len(sn) - 1:
			val = sn[x + 1:]
			val = int(val)
			n += m - val
			sn = str(n)
		else:
			n = n + m
			sn = str(n)
	print(int(n) - act_n)


def check9(i, l, n):
	while i > -n and l[i] == '9':
		i -= 1
	return i
t = int(input())
while t:
	t -= 1
	(num, x) = map(int, input().split())
	l = list(str(num))
	s = 0
	if str(x) not in l:
		print(s)
	else:
		n = len(l)
		for i in range(-1, -n - 1, -1):
			if l[i] == str(x):
				var = l[i]
				if var != '9' and var != '0':
					minus = ''.join(map(str, l[i:]))
					rem = int(int(var) + 1) * pow(10, -i - 1) - int(minus)
					s += rem
					l = l[:i] + list(str(int(int(var) + 1) * pow(10, -i - 1)))
				elif var == '0':
					minus = ''.join(map(str, l[i:]))
					if i == -1:
						s += 1
						rem = 0
						l = l[:i] + list('1')
					else:
						rem = int(str(1) * -i) - int(minus)
						l = l[:i] + list(str(1) * -i)
						s += rem
				else:
					i = check9(i, l, n)
					try:
						minus = ''.join(map(str, l[i:]))
						rem = int(int(l[i]) + 1) * pow(10, -i - 1) - int(minus)
						s += rem
						l = l[:i] + list(str(int(int(l[i]) + 1) * pow(10, -i - 1)))
					except IndexError:
						rem = 10 * pow(10, -i - 1) - int(''.join(map(str, l[i:])))
						s += rem
						break
		print(s)


def check(a, d):
	n = a
	c = 0
	ans = 0
	while n:
		r = n % 10
		c += 1
		n = n // 10
		if r == d:
			n = n * 10 ** c + (r + 1) * 10 ** (c - 1)
			c = 0
			ans = n - a
	print(ans)
n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	check(a, b)


def digitr(N, D):
	str1 = str(N)
	str2 = str(D)
	for i in range(len(str1) - 1, -1, -1):
		if str1[i] == str2:
			mul = 10 ** (len(str1) - i - 1)
			add = mul - (N - N // mul * mul)
			N += add
			return digitr(N, D)
	return N
for _ in range(int(input())):
	(N, D) = map(int, input().split())
	print(digitr(N, D) - N)


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	x = n
	res = 0
	c = 0
	while x > 0:
		r = x % 10
		x = x // 10
		c += 1
		if r == d:
			x = x * pow(10, c) + (r + 1) * pow(10, c - 1)
			res = x - n
			c = 0
	print(res)


def get9(x, y):
	l = int(x)
	N = len(x)
	flag = '0'
	for (i, e) in enumerate(x):
		wkanda()
		if e == '9':
			f = N - i
			a = str(int(flag) + 1)
			next = int(a + f * '0')
			wkanda()
			if '9' in str(next):
				wkanda()
				return next + getV(str(next), '9') - l
			return next - l
		flag = flag + e

def get0(x, y):
	l = int(x)
	N = len(x)
	flag = ''
	wkanda()
	for (i, e) in enumerate(x):
		if e == '0':
			f = N - i
			return int(flag + f * '1') - l
		flag = flag + e

def getV(x, y):
	l = int(x)
	N = len(x)
	if y not in x:
		return 0
	wkanda()
	if y == '9':
		return get9(x, y)
	wkanda()
	if y == '0':
		return get0(x, y)
	else:
		wkanda()
		flag = ' '
		for (i, e) in enumerate(x):
			wkanda()
			if e == y:
				a = str(int(e) + 1)
				b = N - i - 1
				return int(flag + a + '0' * b) - l
			flag = flag + e

def wkanda():
	return
T = int(input())
while T:
	(x, y) = input().strip().split()
	print(getV(x, y))
	T -= 1


for t in range(int(input())):
	(n, d) = map(int, input().split())
	p = str(n)
	q = str(d)
	if q not in p:
		print(0)
	else:
		a = 0
		while q in p:
			x = p.index(q)
			y = p[x + 1:]
			if len(y) > 0:
				z = int(y)
			else:
				z = int(0)
			k = 10 ** len(y) - z
			a += k
			n += k
			p = str(n)
		print(a)


t = int(input())
for tt in range(t):
	(n, d) = input().split()
	a = n.find(d)
	if a == -1:
		print(0)
		continue
	if d != '9' and d != '0':
		y = (int(d) + 1) * 10 ** (len(n) - 1 - a)
		z = int(n[a:])
		print(y - z)
	elif d == '0':
		print(int('1' * (len(n) - a)) - int(n[a:]))
	elif a != 0 and n[a - 1] == '8':
		while n[a - 1] == '8' and a - 1 >= 0:
			a -= 1
		y = 10 ** (len(n) - a)
		z = int(n[a:])
		print(y - z)
	else:
		y = (int(d) + 1) * 10 ** (len(n) - 1 - a)
		z = int(n[a:])
		print(y - z)


def sn(s, d):
	n = ''
	f = 0
	for i in s:
		if i == d and f == 0:
			n += i
			n = str(int(n) + 1)
			f = 1
		elif f == 1:
			n += '0'
		if f == 0:
			n += i
	return n
for _ in range(int(input())):
	(n, d) = input().split()
	p = n
	while d in n:
		n = sn(n, d)
	print(int(n) - int(p))


t = int(input())
while t > 0:
	t -= 1
	(n, d) = map(int, input().split())
	N = n
	(c, res) = (0, 0)
	while N > 0:
		rem = N % 10
		N = N // 10
		c += 1
		if rem == d:
			N = N * pow(10, c) + (rem + 1) * pow(10, c - 1)
			res = N - n
			c = 0
	print(res)