#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1e9 + 7;
string s;
int total;
void solve() {
  cin >> s >> total;
  int sum = 0, n = s.size();
  for (int i = 0; i < n; i++) {
    sum += s[i] - '0';
  }
  if (sum <= total) {
    cout << 0;
    return;
  }
  string res = "";
  int carry = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (sum + carry <= total) break;
    if (carry) {
      sum -= (s[i] - '0');
      if (s[i] == '9') {
        res += '0';
        continue;
      }
      s[i] = ((s[i] - '0') + 1) + '0';
      sum += s[i] - '0';
      carry--;
    }
    if (sum + carry <= total) break;
    if (s[i] != '0') {
      sum -= (s[i] - '0');
      char ch = (10 - (s[i] - '0')) % 10 + '0';
      res += ch;
      carry++;
    } else {
      res += '0';
    }
  }
  reverse(res.begin(), res.end());
  int k = 0;
  while (k < res.size() && res[k] == '0') k++;
  cout << res.substr(k, res.size() - k);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
