#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * acos(0);
const double EPS = 1e-9;
const int oo = 2e9;
const long long MOD = 1e9 + 7;
const int N = 3e5 + 5;
int main() {
  int tc;
  scanf("%d", &(tc));
  while (tc--) {
    long long n;
    int sum;
    scanf("%lld%d", &n, &sum);
    int cnt = 0;
    string s = to_string(n);
    for (int i = 0; i < int((s).size()); i++) cnt += s[i] - '0';
    if (cnt > sum) {
      cnt = 0;
      int i = 0;
      string t;
      for (; i < int((s).size()) && (sum - cnt > s[i] - '0'); i++)
        cnt += s[i] - '0', t.push_back(s[i]);
      if (sum - cnt <= s[i] - '0') {
        i--;
        if (!t.empty()) t.pop_back();
        while (i >= 0 && s[i] == '9') {
          i--;
          if (!t.empty()) t.pop_back();
        }
      }
      if (i < 0)
        t.push_back('1');
      else
        t.push_back(s[i] + 1);
      t += string(int((s).size()) - 1 - i, '0');
      printf("%lld\n", stoll(t) - n);
    } else
      puts("0");
  }
  return 0;
}
