You are given a positive integer $n$. In one move, you can increase $n$ by one (i.e. make $n := n + 1$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $n$ be less than or equal to $s$.

You have to answer $t$ independent test cases.


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow.

The only line of the test case contains two integers $n$ and $s$ ($1 \le n \le 10^{18}$; $1 \le s \le 162$).


-----Output-----

For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $n$ be less than or equal to $s$.


-----Example-----
Input
5
2 1
1 1
500 4
217871987498122 10
100000000000000001 1

Output
8
0
500
2128012501878
899999999999999999
[{"code": "import sys\n\n\ndef read_int():\n    return int(sys.stdin.readline())\n\n\ndef read_ints():\n    return list(map(int, sys.stdin.readline().split(' ')))\n\n\nt = read_int()\nfor case_num in range(t):\n    n, s = read_ints()\n    a = [0] + [int(i) for i in str(n)]\n    ds = sum(a)\n    cost = 0\n    idx = len(a) - 1\n    radix = 1\n    while ds > s:\n        if a[idx] > 0:\n            cost += (10 - a[idx]) * radix\n            ds -= a[idx]\n            a[idx] = 0\n            ds += 1\n            a[idx - 1] += 1\n            i = idx - 1\n            while a[i] >= 10:\n                a[i - 1] += 1\n                a[i] -= 10\n                ds -= 9\n                i -= 1\n        radix *= 10\n        idx -= 1\n    print(cost)\n", "passed": true, "time": 0.15, "memory": 14172.0, "status": "done"}, {"code": "tx=int(input())\nfor i in range(tx):\n    a,b=[int(i) for i in input().split()]\n    k=a\n    t=1\n    t2=0\n    s=str(a)\n    for i in s:\n        t2+=ord(i)-ord('0')\n    while t<=pow(10,18):\n        if t>1:a+=t-(a % t)\n        t2=0\n        s=str(a)\n        for i in s:\n            t2+=ord(i)-ord('0')\n        if t2<=b:\n            print(a-k)\n            break\n        t*=10", "passed": true, "time": 0.16, "memory": 14096.0, "status": "done"}, {"code": "# d-decrease-the-sum-of-digits.yml\n\ndef dsum(x):\n    res = 0\n    while x:\n        res += x % 10\n        x //= 10\n    return res\n\nfor _ in range(int(input())):\n    n, s = list(map(int, input().split()))\n    # print(f'init: n={n}, s={s}')\n\n    p = 1\n    ans = 0\n    csum = dsum(n)\n    # its=10\n    while csum > s:\n        # print(f'n={n}')\n        add = 10 * p - n % (10 * p)\n        if add == 10 * p: add = 0\n        ans += add\n        n += add\n        csum = dsum(n)\n        p *= 10\n        # print(f'n={n},p={p},csum={csum},add={add}')\n\n        # its-=1\n        # assert its\n\n    print(ans)\n", "passed": true, "time": 0.15, "memory": 14304.0, "status": "done"}, {"code": "for _ in range(int(input())):\n    s,k=list(map(str,input().split()))\n    k=int(k)\n    su=0\n    for i in range(len(s)):\n        su+=int(s[i])\n        if su>=k:\n            ans=int(s[i:])    \n            ans=int(\"1\"+(len(s)-i)*\"0\")-ans\n            # print(ans)\n            break\n    su=0    \n    for i in range(len(s)):\n        su+=int(s[i])\n    if su<=k:\n        print(0)\n    else:\n        print(ans)\n", "passed": true, "time": 0.24, "memory": 14132.0, "status": "done"}, {"code": "# Fast IO (be careful about bytestring)\n\n# import os,io\n# input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline\n\nfor _ in range(int(input())):\n    n,s = list(map(int,input().split()))\n    lenN = len(str(n))\n    sumDigit = 0\n    nCpy = n\n    for j in range(lenN):\n        sumDigit += nCpy % 10\n        nCpy //= 10\n    if sumDigit <= s:\n        print(0)\n    else:\n        for j in range(lenN):\n            newN = (n // (10 ** (j+1)) + 1) * 10 ** (j + 1)\n            nCpy = newN\n            sumDigit = 0\n            for j in range(lenN):\n                sumDigit += nCpy % 10\n                nCpy //= 10\n            if sumDigit <= s:\n                print(newN - n)\n                break\n", "passed": true, "time": 0.82, "memory": 14180.0, "status": "done"}, {"code": "def main():\n    t = int(input())\n    for _ in range(t):\n        n, s = input().split(\" \")\n        s = int(s)\n        total = 0\n        for nn in n:\n            total += int(nn)\n        ans = 0\n        p = 1\n        ten = 1\n        carry = False\n        while total > s:\n            num = int(n[-p])\n            if carry:\n                num += 1\n            if num == 0:\n                carry = False\n            else:\n                ans += ten * (10 - num)\n                total -= num - 1\n                carry = True\n            ten *= 10\n            p += 1\n        print(ans)\n          \n        \nmain()", "passed": true, "time": 0.14, "memory": 14256.0, "status": "done"}, {"code": "for _ in range(int(input())):\n    a,b=map(int,input().split())\n    bb=b\n    can=0\n    d=10000000000000000000\n    while(d>0):\n        bb=bb-(a//d)%10\n        d=d//10\n    if(bb>=0):\n        can=1\n    d=10000000000000000000\n    t=0\n    while(d>0 and b-(a//d)%10>=1):\n        b=b-(a//d)%10\n        t=(a//d+1)*d\n        d=d//10\n    if(can):print(0)\n    else:print(t-a)\n    t=0", "passed": true, "time": 0.15, "memory": 14388.0, "status": "done"}, {"code": "import sys\n\ninput = sys.stdin.readline\n\n\n############ ---- Input Functions ---- ############\ndef inp():\n    return (int(input()))\n\n\ndef inlt():\n    return (list(map(int, input().split())))\n\n\ndef insr():\n    s = input()\n    return (list(s[:len(s) - 1]))\n\n\ndef invr():\n    return (list(map(int, input().split())))\n\ndef digitSum(x):\n    c = 0\n    while x:\n        c += x%10\n        x //= 10\n    return c\n\ndef solve(x,y):\n    if digitSum(x) <= y:\n        return 0\n    # Try next power of 10\n    xStr = str(x)\n    attempt = 10 ** len(xStr) - x\n\n    for i in range(len(xStr)):\n        newNumber = int(xStr[:i+1]) + 1\n        newNumber *= 10 ** (len(xStr) - i-1)\n        if digitSum(newNumber) <= y:\n            attempt = newNumber - x\n    return attempt\n\nlines = inp()\nfor i in range(lines):\n    v = inlt()\n    print(solve(*v))\n", "passed": true, "time": 0.15, "memory": 14148.0, "status": "done"}, {"code": "for _ in range(int(input())):\n    n,s=map(int,input().split())\n    savn=n\n    cd=1\n    while cd<=n:\n        if sum(map(int,str(n)))<=s:break\n        cc=n//cd%10\n        if cc==0:cd*=10;continue\n        n+=(10-cc)*cd\n        cd*=10\n    print(n-savn)", "passed": true, "time": 0.16, "memory": 14080.0, "status": "done"}, {"code": "z = int(input())\n\ndef sm(x):\n    ans = 0\n    while x>0:\n        ans += x%10\n        x //= 10\n    return ans\n\nfor _ in range(z):\n    n, s = list(map(int, input().split()))\n    ans = 10000000000000000000000000000\n    if sm(n) <= s:\n        print(0)\n        continue\n    goal = n\n    for i in range(len(str(n))+1):\n        goal = int(str(n)[:len(str(n))-i] + '0'*i) + 10**i\n        if sm(goal) <= s:\n            ans = min(ans, goal-n)\n    print(ans)\n", "passed": true, "time": 0.15, "memory": 14384.0, "status": "done"}, {"code": "def ssum(s):\n    return sum(map(int, list(s)))\nfor _ in range(int(input())):\n    n, s = list(map(int, input().split()))\n    ns = str(n)\n    if ssum(ns) <= s:\n        print(0)\n        continue\n\n    ans = float(\"inf\")\n    for i in range(0, len(ns)):\n        x = 1\n        if i > 0:\n            x = int(ns[:i])+1\n        acc = 0\n        while x > 0:\n            acc += x % 10\n            x //= 10\n        if acc > s:\n            continue\n        y = 10**(len(ns)-i) - int(ns[i:])\n        ans = min(ans, y)\n    print(ans)\n", "passed": true, "time": 0.15, "memory": 14296.0, "status": "done"}, {"code": "import sys\nfrom math import ceil\ninput = sys.stdin.readline\nt = int(input())\ndef f(n):\n    r = 0\n    while n:\n        r += n%10\n        n //= 10\n    return r\nfor tc in range(t):\n    n, s = list(map(int, input().strip().split()))\n    if f(n) <= s:\n        print(0)\n        continue\n    b = 1\n    ans = int(1e20)\n    while b <= n*10:\n        rn = n//b*b+b\n        # print(rn, f(rn))\n        if f(rn) <= s: ans = min(ans, rn-n)\n        b *= 10\n    print(ans)\n", "passed": true, "time": 0.14, "memory": 14244.0, "status": "done"}, {"code": "import sys, math\nimport io, os\n#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline\nfrom bisect import bisect_left as bl, bisect_right as br, insort\nfrom heapq import heapify, heappush, heappop\nfrom collections import defaultdict as dd, deque, Counter\n# from itertools import permutations,combinations\ndef data(): return sys.stdin.readline().strip()\ndef mdata(): return list(map(int, data().split()))\ndef outl(var): sys.stdout.write(' '.join(map(str, var)) + '\\n')\ndef out(var): sys.stdout.write(str(var) + '\\n')\nfrom decimal import Decimal\n# from fractions import Fraction\n# sys.setrecursionlimit(100000)\nmod = int(1e9) + 7\nINF=2**32\n\ndef cal(n):\n    ans=0\n    while n:\n        ans+=n%10\n        n//=10\n    return ans\n\nfor t in range(int(data())):\n   n,s=mdata()\n   ans=0\n   t=1\n   while cal(n)>s:\n       last=n%10\n       n//=10\n       if last!=0:\n           ans+=t*(10-last)\n           n+=1\n       t*=10\n   out(ans)", "passed": true, "time": 0.15, "memory": 14360.0, "status": "done"}, {"code": "'''\nAuthor: AsilenceBTF\nBlog: asilencebtf.top\nDate: 2020-05-28 13:02:12\nLastEditTime: 2020-09-04 23:11:19\n'''\n\ndef f(n):\n    res = 0\n    while n > 0 : \n        res = res + 1\n        n = n // 10\n    return res\ndef check(n):\n    res = 0\n    while n > 0:\n        res = res + (n%10)\n        n = n // 10\n    return res\nfor _ in range(int(input())):\n    n, s = list(map(int, input().split()))\n    len = f(n)\n    ans = pow(10, 20)\n    if check(n) <= s:\n        ans = 0 \n    for i in range(0, len):\n        p = n // pow(10, i + 1)\n        p = p * pow(10, i + 1)\n        p = p + pow(10, i + 1)\n        if check(p) <= s:\n            if ans > (p - n):\n                ans = p - n \n    print(ans)\n    \n\n", "passed": true, "time": 0.14, "memory": 14340.0, "status": "done"}, {"code": "import bisect\nimport copy\nimport fractions\nimport functools\nimport heapq\nimport math\nimport random\nimport sys\n\n\ndef __starting_point():\n\n    T = int(input())\n\n    for t in range(T):\n        N, S = tuple(map(int, input().split()))\n\n        if sum(int(dig) for dig in str(N)) <= S:\n            print('0')\n            continue\n\n        # So we start counting digits from left to right\n        # The first digits where the sum is greater than or equal to S and every digit following\n        # needs to be set to 0, and the last digit we safely counted gets incremented\n        # Our answer is the difference between this number and N\n\n        str_N = '0' + str(N)\n\n        total = 0\n        i = 0\n        while total < S:\n            total += int(str_N[i])\n            i += 1\n\n        target = (int(str_N[:i-1]) + 1) * (10 ** (len(str_N) - i + 1))\n        print(str(target - N))\n\n__starting_point()", "passed": true, "time": 0.26, "memory": 14144.0, "status": "done"}, {"code": "for t in range(int(input())):\n    # n,s = map(int,input().split())\n    n,s = input().split()\n    s = int(s)\n\n    nn =['0','0','0'] + list(n)\n\n    su = 0\n    for i in n:\n        su += int(i)\n\n    ptr = 1\n    # cost = 0\n    while su>s:\n        su-=int(nn[-ptr])\n        su+=1\n        nn[-ptr]='0'\n        nn[-ptr-1]=str(1+int(nn[-ptr-1]))\n\n        itr = 1\n        while int(nn[-ptr-itr]) >=10:\n            su-=10\n            su+=int(nn[-ptr-itr])//10\n            nn[-ptr-itr-1]=str(int(nn[-ptr-itr-1])+int(nn[-ptr-itr])//10)\n            nn[-ptr-itr]=str(int(nn[-ptr-1])%10)\n            itr+=1\n        # if int(nn[-ptr-1])>=10:\n            \n            \n            \n            \n        ptr+=1\n\n    print(int(''.join(nn))-int(n))\n\n    # nn = n\n    # ctr = 0\n    # cost = []\n    # su = 0\n    # while nn:\n    #     su += nn%10\n    #     cost.append((10-nn%10)*10**ctr)\n    #     nn//=10\n    #     ctr+=1\n\n    # ptr = 1\n    # while su>s:\n    #     su-=n%10**ptr\n    #     su+=1\n    #     n\n", "passed": true, "time": 0.15, "memory": 14256.0, "status": "done"}, {"code": "def solve(n, s):\n    dns = [ord(x)-ord('0') for x in str(n)]\n    if sum(dns) <= s:\n        return 0\n    m = len(dns)\n    for i, d in enumerate(dns):\n        if d >= s:\n            return 10**(m-i) - (n % (10**(m-i)))\n        s -= d\n    return 42\n\n\nfor _ in range(int(input())):\n    n, s = [int(x) for x in input().split()]\n    print(solve(n, s))\n", "passed": true, "time": 0.14, "memory": 14148.0, "status": "done"}, {"code": "import sys\nimport math\ndef II():\n\treturn int(sys.stdin.readline())\n\ndef LI():\n\treturn list(map(int, sys.stdin.readline().split()))\n\ndef MI():\n\treturn list(map(int, sys.stdin.readline().split()))\n\ndef SI():\n\treturn sys.stdin.readline().strip()\ndef findSum(n,s):\n\tsu = 0\n\tind = -1\n\tfor i in range(len(n)):\n\t\tsu+=int(n[i])\n\t\tif su>s:\n\t\t\tind = i\n\t\t\tbreak\n\treturn ind\nt = II()\nfor q in range(t):\n\tn,s = MI()\n\tn = str(n)\n\tans = 0\n\tind = findSum(n,s)\n\twhile ind!=-1:\n\t\tf = n[:ind]\n\t\tif f == \"\":\n\t\t\tf = \"0\"\n\t\tse = \"0\"+\"0\"*(len(n)-ind-1)\n\t\tf = str(int(f)+1)+se\n\t\tans+=int(f)-int(n)\n\t\tn = f\n\t\tind = findSum(n,s)\n\tprint(ans)\n\n\t\n", "passed": true, "time": 0.14, "memory": 14260.0, "status": "done"}, {"code": "import math\n\ndef checksum(n):\n    return sum([int(x) for x in str(n)])\n\nfor _ in range(int(input())):\n    n,s = [int(p) for p in input().split()]\n    moves = 0\n    if checksum(n)<=s:\n        print(0)\n    else:\n        pow = 10\n        while True:\n            # print(n)\n            closest = pow*((n+pow-1)//pow)\n            # print(closest)\n            moves += (closest-n)\n            n = closest\n            if checksum(n)<=s:\n                break\n            pow*=10\n        print(moves)", "passed": true, "time": 0.14, "memory": 14104.0, "status": "done"}, {"code": "for tc in range(int(input())):\n    \n    n, s = [int(x) for x in input().split()]\n    \n    sumOf = sum(int(x) for x in str(n))\n    \n    cost = 0\n    \n    carry = False\n    \n    for pos, char in enumerate(reversed('0' + str(n))):\n        if sumOf <= s: break\n        \n        digit = int(char)\n        \n        if carry: digit += 1\n        \n        if digit != 0: \n            carry = True\n        \n            cost += (10 - digit) * (10 ** pos)\n        \n            sumOf -= digit - 1\n        \n    \n    print(cost) ", "passed": true, "time": 0.14, "memory": 14240.0, "status": "done"}, {"code": "from math import ceil\nfrom collections import deque\n\ndef sm(n):\n\tret = 0\n\twhile n>0:\n\t\tret += n%10\n\t\tn //= 10\n\treturn ret\n\nfor _ in range(int(input())):\n\tn, s = [int(i) for i in input().split()]\n\ttemp = n\n\twhile sm(n)>s:\n\t\tj = 10\n\t\twhile n%j == 0:\n\t\t\tj *= 10\n\t\tn += j-n%j\n\tprint(n - temp)", "passed": true, "time": 0.15, "memory": 14084.0, "status": "done"}, {"code": "\nt = int(input())\ndef dgt_sum(num):\n    return sum([int(x) for x in str(num)])\n\ndef clps(val, left=0):\n    if len(str(val)) == 1:\n        return 1, left + 1\n\n\n    return int(str(val)[:-1]) + 1, left + 1\n\n\ndef solve(n, s):\n    n_str =str(n)\n    _s = 0\n    target = n\n    for i, d in enumerate(n_str):\n        d = int(d)\n        _s+=d\n        if _s > s:\n            cl = int(n_str[:i+1]), 0\n            while dgt_sum(cl[0]) > s:\n                cl = clps(*cl)\n\n            target = cl[0] * pow(10, cl[1] + len(n_str) -i-1)\n            break\n\n    return target - n\n\n\n\nfor _ in range(t):\n    n,s = [int(x) for x in input().split(' ')]\n    print(solve(n,s))", "passed": true, "time": 0.14, "memory": 14104.0, "status": "done"}, {"code": "#!/usr/bin/env pypy3\n\ndef ans(n, s):\n\tn = str(n)\n\n\tif sum(map(int, n)) <= s:\n\t\treturn 0\n\n\tcandidates = set()\n\tfor i in range(1,len(n)):\n\t\tna, nb = n[:i], n[i:]\n\t\tna = str(int(na)+1)\n\t\tnb = \"0\"*len(nb)\n\t\tif sum(map(int, na)) + sum(map(int, nb)) <= s:\n\t\t\tcandidates.add(int(na+nb) - int(n))\n\n\tbig = \"1\" + \"0\"*len(n)\n\tcandidates.add(int(big) - int(n))\n\n\tsmall = \"1\" + \"0\"*(len(n)-1)\n\tif int(small) - int(n) >= 0:\n\t\tcandidates.add(int(small) - int(n))\n\n\treturn min(candidates)\n\ndef ans_slow(n, s):\n\tfor i in range(0, 10**200):\n\t\tbig = n+i\n\t\tbig = str(big)\n\t\tif sum(map(int, big)) <= s:\n\t\t\treturn i\n\nimport sys\n\n# print(ans(2,2))\n# print(ans_slow(2,2))\n\n# return\n\n# for s in range(1, 162):\n# \tfor n in range(1, 10000):\n# \t\tif ans(n,s) != ans_slow(n, s):\n# \t\t\tprint(n, s)\n\n# return\n\nT = int(input())\nfor t in range(T):\n\tn,s = input().split()\n\tn = int(n)\n\ts = int(s)\n\n\tprint(ans(n, s))", "passed": true, "time": 0.14, "memory": 14144.0, "status": "done"}]