#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const long long inf = 1e18;
int T, n, k;
long long ans;
inline void ckMin(register long long &p, const long long q) {
  p = (p < q ? p : q);
}
inline long long F(register long long x) {
  register int ans = 0;
  while (x) ans += x % 10, x /= 10;
  return ans;
}
inline void ck(const long long x) {
  if (x < 0) return;
  register int s = 0;
  for (register long long i = x; i <= x + k; ++i) s += F(i);
  if (s == n) ckMin(ans, x);
}
int main() {
  scanf("%d", &T);
  register int i;
  while (T--) {
    scanf("%d%d", &n, &k), ans = inf;
    for (i = 0; i <= 100; ++i) ck(i);
    for (register long long now = 9; now < inf; now = now * 10ll + 9)
      for (i = 0; i < 9; ++i)
        for (register long long x = (now + 1) * i + now, j = x - 20;
             j <= x + 20; ++j)
          ck(j);
    ans == inf ? puts("-1") : printf("%lld\n", ans);
  }
  return 0;
}
