#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b % 2 != 0) {
      ans = (ans * a) % 1000000007;
    }
    a = ((a % 1000000007) * (a % 1000000007)) % 1000000007;
    b >>= 1;
  }
  return ans;
}
void sieve(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p]) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++) {
    if (prime[p]) cout << p << " ";
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int test = 1;
  cin >> test;
  while (test--) {
    string s;
    int sum = 0;
    cin >> s >> sum;
    int curSum = 0;
    for (auto i : s) {
      curSum += i - '0';
    }
    if (curSum <= sum) {
      cout << 0 << '\n';
      continue;
    }
    long long la = 1;
    reverse(s.begin(), s.end());
    int sa = 0;
    string z;
    for (auto i : s) {
      sa += i - '0';
      z += i;
      la *= 10;
      if (curSum - sa + 1 <= sum) {
        reverse(z.begin(), z.end());
        long long ha = stoll(z);
        cout << la - ha << '\n';
        break;
      }
    }
  }
  return 0;
}
