#include <bits/stdc++.h>
int w[10], c[10];
long long p10[22], tt;
long long g(int x, int a) {
  long long ans = a, i = 1;
  for (;; i++) {
    if (x < 9) {
      ans += p10[i] * (long long)x;
      break;
    } else {
      ans += p10[i] * (long long)9;
      x -= 9;
    }
  }
  return ans;
}
long long gg(int x, int y, int a) {
  long long ans = a, i = 1;
  while (y--) {
    ans += p10[i] * (long long)9;
    i++;
  }
  if (x < 8) {
    ans += p10[i] * (long long)x;
    return ans;
  }
  ans += p10[i] * (long long)8, x -= 8;
  for (i++;; i++) {
    if (x < 9) {
      ans += p10[i] * (long long)x;
      break;
    } else {
      ans += p10[i] * (long long)9;
      x -= 9;
    }
  }
  return ans;
}
int main() {
  int n, T, k, i, t, j, x, tk, tn, y, b, tb;
  long long ans;
  scanf("%d", &T);
  for (i = 1, p10[j = 0] = 1; i < 19; j = i++) {
    p10[i] = p10[j] * (long long)10;
  }
  while (T--) {
    scanf("%d%d", &n, &k);
    for (i = 0; i < 10; i++) {
      for (j = i, c[i] = w[i] = 0, t = i + k; j <= t; j++) {
        if (j < 10)
          w[i] += j;
        else
          w[i] += j - 9, c[i]++;
      }
    }
    for (i = 0, tk = k + 1, ans = -1; i < 10; i++) {
      if (!c[i]) {
        if (w[i] <= n && (n - w[i]) % tk == 0) {
          x = (n - w[i]) / tk;
          tt = g(x, i);
          if (ans == -1)
            ans = tt;
          else if (tt < ans)
            ans = tt;
        }
      } else {
        if (w[i] <= n) {
          tn = n - w[i], y = 0, b = 9 * (tk - c[i]), tb = 0;
          while (tb <= tn) {
            if ((tn - tb) % tk == 0) {
              x = (tn - tb) / tk;
              tt = gg(x, y, i);
              if (ans == -1)
                ans = tt;
              else if (tt < ans)
                ans = tt;
            }
            tb += b, y++;
          }
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
