#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
long long power(long long a, long long b, long long m = MOD) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) ans = ((ans % m) * (a % m)) % m;
    a = ((a % m) * (a % m)) % m;
    b >>= 1;
  }
  return ans;
}
long long dir[] = {-1, 0, 1, 0, -1};
long long dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
long long dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long N = 1e5 + 1;
void precomp() {}
long long helper(long long key) {
  if (key < 8) return key;
  key -= 8;
  long long prefix = key % 9;
  key -= key % 9;
  while (key) {
    prefix *= 10;
    prefix += min(key, (long long)9);
    key -= min(key, (long long)9);
  }
  prefix = prefix * 10 + 8;
  return prefix;
}
void solve() {
  long long i, j, n, k, m;
  cin >> n >> k;
  long long nines, d, ans = INF, p = 1;
  for (nines = 0; nines < 17; nines++) {
    p = p * 10;
    for (d = 0; d < 10; d++) {
      long long type1 = min(k + 1, 10 - d);
      long long num = n - nines * 9 * type1 - (k + 1 - type1);
      for (i = 0; i <= k; i++) num = num - (d + i) % 10;
      if (num >= 0 and num % (k + 1) == 0) {
        long long key = num / (k + 1);
        ans = min(ans, p * helper(key) + p - 10 + d);
      }
    }
  }
  if (ans == INF)
    cout << -1 << "\n";
  else
    cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  cin >> t;
  precomp();
  while (t--) solve();
}
