t = int(input())
for _ in range(t):
n_s, m_s = input().split(' ')
n_i, m_i = int(n_s), int(m_s)
n_i = max(n_i, m_i - 1)
n_s = str(n_i)
res = n_i + m_i - n_i % m_i
if str(n_i + 1).find(m_s) != -1:
print(min(n_i + 1, res))
continue
len_n = len(n_s)
len_m = len(m_s)
l, r = len_n - len_m, len_n
for __ in range(len_n - len_m + 1):
sub_n_s = n_s[l:r]
if l == len_n - len_m:
if int(sub_n_s) < m_i:
res = min(res, int((n_s[0:l] + m_s).ljust(len_n, '0')))
break
else:
if int(sub_n_s) <= m_i:
res = min(res, int((n_s[0:l] + m_s).ljust(len_n, '0')))
break
l -= 1
r -= 1
print(res)