#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll mod = 1000000007;
constexpr ll lim = 2000006;
ll f(ll x) {
  ll res = 0;
  while (x > 0) {
    res += x % 10;
    x /= 10;
  }
  return res;
}
ll calcMi(ll x0, ll k) {
  ll res = 0;
  for (ll j = 0; j <= k; j++) res += f(x0 + j);
  return res;
}
ll pref(ll x) {
  vector<ll> digits;
  while (x > 10) {
    digits.push_back(9);
    x -= 9;
  }
  digits.push_back(x);
  sort(begin(digits), end(digits));
  ll res = 0;
  for (ll y : digits) {
    res *= 10;
    res += y;
  }
  return res;
}
constexpr ll inf = 0x3FFFFFFFFFFFFFFF;
ll solve(ll n, ll k) {
  ll res = inf;
  for (ll d = 0; d <= 9; d++) {
    for (ll nines = 1; nines < 10000000000000000; nines *= 10) {
      ll x0 = d + 10 * (nines - 1);
      ll mi = calcMi(x0, k);
      for (ll mul = 1; mul <= 100; mul *= 10) {
        ll c = x0 + (__int128)nines * mul * pref((n - mi) / (k + 1));
        if (c >= 0 && calcMi(c, k) == n) {
          res = min(res, c);
        }
      }
    }
  }
  for (ll d = 10; d <= 99; d++) {
    for (ll nines = 1; nines < 10000000000000000; nines *= 10) {
      ll x0 = d + 100 * (nines - 1);
      ll mi = calcMi(x0, k);
      for (ll mul = 1; mul <= 100; mul *= 10) {
        ll c = x0 + (__int128)nines * mul * pref((n - mi) / (k + 1));
        if (c >= 0 && calcMi(c, k) == n) {
          res = min(res, c);
        }
      }
    }
  }
  if (res == inf) res = -1;
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll t;
  cin >> t;
  for (ll i = 0; i < t; i++) {
    ll n, k;
    cin >> n >> k;
    cout << solve(n, k) << endl;
  }
}
