#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int t;
int n, s;
string str;
char n1[N];
char n2[N];
int a[N], b[N], c[N];
int main() {
  cin >> t;
  while (t--) {
    cin >> str >> s;
    int sum = 0, k = -1, pos = -1, flag = 1;
    for (int i = 0; i < str.size(); i++) {
      sum += str[i] - '0';
      if (sum == s && flag)
        pos = i, flag = 0;
      else if (sum > s) {
        if (pos != -1)
          k = pos;
        else
          k = i;
        break;
      }
    }
    if (sum <= s || (pos != -1 && k == -1)) {
      cout << 0 << endl;
      continue;
    }
    memset(c, 0, sizeof c);
    memset(n2, 0, sizeof n2);
    memset(n1, 0, sizeof n1);
    memset(a, 0, sizeof a);
    memset(b, 0, sizeof b);
    for (int i = k; i < str.size(); i++) n2[i - k] = str[i];
    n1[0] = '1';
    for (int i = 1; i <= strlen(n2); i++) n1[i] = '0';
    int lena = strlen(n1);
    int lenb = strlen(n2);
    for (int i = 0; i <= lena - 1; i++) a[lena - i] = int(n1[i] - '0');
    for (int i = 0; i <= lenb - 1; i++) b[lenb - i] = int(n2[i] - '0');
    int i = 1;
    while (i <= lena || i <= lenb) {
      if (a[i] < b[i]) {
        a[i] += 10;
        a[i + 1]--;
      }
      c[i] = a[i] - b[i];
      i++;
    }
    int lenc = i;
    while ((c[lenc] == 0) && (lenc > 1)) lenc--;
    for (i = lenc; i >= 1; i--) cout << c[i];
    cout << endl;
  }
  return 0;
}
