#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int x, long long int y) {
  if (x == 0) return y;
  return gcd(y % x, x);
}
long long int lcm(long long int x, long long int y) {
  return ((x * y) / gcd(x, y));
}
bool isPrime(long long int n) {
  if (n <= 1) return false;
  for (long long int i = 2; i * i < n + 1; i++)
    if (n % i == 0) return false;
  return true;
}
long long int fact(long long int n);
long long int nCr(long long int n, long long int r) {
  return fact(n) / (fact(r) * fact(n - r));
}
long long int fact(long long int n) {
  long long int res = 1;
  for (long long int i = 2; i <= n; i++) res = res * i;
  return res;
}
int powermod(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int modInverse(int n, int p) { return powermod(n, p - 2, p); }
int nCrModPFermat(int n, int r, int p) {
  if (r == 0) return 1;
  int fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p;
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
vector<long long int> all_prime(long long int n) {
  vector<long long int> sieve(n + 1, 0);
  for (long long int i = 2; i < n + 1; i++) {
    if (sieve[i]) continue;
    for (long long int j = i * i; j < n + 1; j += i) {
      if (sieve[j] == 0) {
        sieve[j] = i;
      }
    }
  }
  return sieve;
}
vector<long long int> factors(long long int n) {
  vector<long long int> f;
  for (long long int x = 2; x * x <= n; x++) {
    while (n % x == 0) {
      f.push_back(x);
      n /= x;
    }
  }
  if (n > 1) f.push_back(n);
  return f;
}
void solve() {
  string s;
  cin >> s;
  long long int mi;
  cin >> mi;
  long long int n = s.size(), sum = 0;
  for (long long int i = 0; i < n; i++) {
    s[i] -= '0';
    sum += s[i];
  }
  long long int cnt = 0;
  long long int pntr = 10;
  long long int p = 0;
  if (sum <= mi) {
    cout << 0 << endl;
    return;
  }
  for (long long int i = n - 1; i > -1; i--) {
    p += s[i] * pntr / 10;
    cnt = pntr - p;
    sum -= s[i];
    if (sum + 1 <= mi) {
      cout << cnt << endl;
      return;
    }
    pntr *= 10;
  }
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
