#include <bits/stdc++.h>
#pragma GCC optimize("Ofast, unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getrnd(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }
template <typename T1, typename T2>
inline bool relax(T1& a, const T2& b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool strain(T1& a, const T2& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
void solve() {
  ll n, s;
  cin >> n >> s;
  string x = to_string(n);
  ll res1 = 1;
  for (int i = 0; i < x.size(); ++i) res1 *= 10;
  ll res = res1 - n, cur = 0;
  {
    ll temp = 0;
    for (char c : x) temp += c - '0';
    if (temp <= s) res = 0;
  }
  for (int i = 0; i < x.size(); ++i) {
    cur += x[i] - '0';
    if (cur + 1 > s) break;
    if (x[i] == '9') continue;
    string t = x;
    ++t[i];
    for (int j = i + 1; j < t.size(); ++j) t[j] = '0';
    relax(res, atoll(t.c_str()) - n);
  }
  cout << res << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  srand(time(0));
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
