#include <bits/stdc++.h>
using namespace std;
int f(string s) {
  int i, sum = 0;
  for (i = 0; i < s.length(); i++) {
    sum = sum + (s[i] - 48);
  }
  return sum;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string a;
    cin >> a;
    long long i, s, k, n = a.length(), sum = f(a), carry = 0, move = 0;
    cin >> s;
    if (sum <= s) {
      move = 0;
    } else {
      for (i = n - 1; i >= 0; i--) {
        if ((a[i] + carry - 48) % 10 != 0 && sum > s) {
          k = (10 - (a[i] + carry - 48) % 10) * pow(10, (n - i - 1));
          move = move + k;
          sum = sum - (a[i] + carry - 48) % 10 + 1;
          carry = 1;
        } else {
          if ((a[i] - 48) == 9) {
            sum = sum - 9;
          }
        }
      }
    }
    cout << move << "\n";
  }
  return 0;
}
