#include <bits/stdc++.h>
using namespace std;
int sum(int n) {
  int s = 0;
  while (n > 0) {
    s += n % 10;
    n /= 10;
  }
  return s;
}
int main() {
  int t;
  cin >> t;
  while (t-- > 0) {
    int n, k;
    cin >> n >> k;
    long long int x = 0;
    if (k == 0) {
      while (n > 9) {
        x *= 10;
        x += 9;
        n -= 9;
      }
      x *= 10;
      x += n;
      cout << x << endl;
      continue;
    }
    int s = k * (k + 1) / 2;
    int c = 0;
    while (true) {
      if (c > n) {
        cout << -1 << endl;
        goto A;
      }
      if (s == n) break;
      s -= c;
      x++;
      c = sum(x);
      s += sum(x + k);
    }
    cout << x << endl;
  A:;
  }
  return 0;
}
