#include <bits/stdc++.h>
using namespace std;
int sum(int a) {
  int temp = a;
  int result = 0;
  while (temp >= 10) {
    result += (temp % 10);
    temp /= 10;
  }
  return result + temp;
}
int peso[1000020];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 1000020; i++) {
    peso[i] = sum(i);
  }
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    cin >> n >> k;
    int result = -1;
    if (k >= 2) {
      int lasti = -1;
      for (int i = 0; i < 1000006; i++) {
        int current = 0;
        if (lasti != -1) {
          current = lasti - peso[i - 1] + peso[i + k];
        } else {
          for (int j = i; j <= i + k; j++) {
            current += peso[j];
          }
        }
        lasti = current;
        if (current == n) {
          result = i;
          break;
        }
      }
      cout << result << '\n';
    } else if (k == 0) {
      string result = "";
      while (n >= 9) {
        result += "9";
        n -= 9;
      }
      if (n != 0) result += ('0' + n);
      reverse(result.begin(), result.end());
      cout << result << '\n';
    } else {
      map<int, int> mini;
      mini[1] = 0;
      mini[3] = 1;
      mini[5] = 2;
      mini[7] = 3;
      mini[9] = 4;
      mini[10] = 9;
      mini[11] = 5;
      mini[12] = 19;
      mini[13] = 6;
      mini[14] = 29;
      mini[15] = 7;
      mini[16] = 39;
      mini[17] = 8;
      mini[18] = 49;
      mini[19] = 18;
      mini[20] = 59;
      mini[21] = 28;
      mini[22] = 69;
      mini[23] = 38;
      mini[24] = 79;
      mini[25] = 48;
      mini[26] = 89;
      mini[27] = 58;
      mini[28] = 189;
      mini[29] = 68;
      mini[30] = 289;
      mini[31] = 78;
      mini[32] = 389;
      mini[33] = 88;
      mini[34] = 489;
      mini[35] = 98;
      mini[36] = 589;
      mini[37] = 198;
      mini[38] = 689;
      mini[39] = 298;
      mini[40] = 789;
      mini[41] = 398;
      mini[42] = 889;
      mini[43] = 498;
      mini[44] = 989;
      mini[45] = 598;
      mini[46] = 1989;
      mini[47] = 698;
      mini[48] = 2989;
      mini[49] = 798;
      mini[50] = 3989;
      mini[51] = 898;
      mini[52] = 4989;
      mini[53] = 998;
      mini[54] = 5989;
      mini[55] = 1998;
      mini[56] = 6989;
      mini[57] = 2998;
      mini[58] = 7989;
      mini[59] = 3998;
      mini[60] = 8989;
      mini[61] = 4998;
      mini[62] = 9989;
      mini[63] = 5998;
      mini[64] = 19989;
      mini[65] = 6998;
      mini[66] = 29989;
      mini[67] = 7998;
      mini[68] = 39989;
      mini[69] = 8998;
      mini[70] = 49989;
      mini[71] = 9998;
      mini[72] = 59989;
      mini[73] = 19998;
      mini[74] = 69989;
      mini[75] = 29998;
      mini[76] = 79989;
      mini[77] = 39998;
      mini[78] = 89989;
      mini[79] = 49998;
      mini[80] = 99989;
      mini[81] = 59998;
      mini[82] = 199989;
      mini[83] = 69998;
      mini[84] = 299989;
      mini[85] = 79998;
      mini[86] = 399989;
      mini[87] = 89998;
      mini[88] = 499989;
      mini[89] = 99998;
      mini[90] = 599989;
      mini[91] = 199998;
      mini[92] = 699989;
      mini[93] = 299998;
      mini[94] = 799989;
      mini[95] = 399998;
      mini[96] = 899989;
      mini[97] = 499998;
      mini[98] = 999989;
      mini[99] = 599998;
      mini[100] = 1999989;
      mini[101] = 699998;
      mini[102] = 2999989;
      mini[103] = 799998;
      mini[104] = 3999989;
      mini[105] = 899998;
      mini[106] = 4999989;
      mini[107] = 999998;
      mini[108] = 5999989;
      mini[109] = 1999998;
      mini[110] = 6999989;
      mini[111] = 2999998;
      mini[112] = 7999989;
      mini[113] = 3999998;
      mini[114] = 8999989;
      mini[115] = 4999998;
      mini[116] = 9999989;
      mini[117] = 5999998;
      mini[118] = 19999989;
      mini[119] = 6999998;
      mini[120] = 29999989;
      mini[121] = 7999998;
      mini[122] = 39999989;
      mini[123] = 8999998;
      mini[124] = 49999989;
      mini[125] = 9999998;
      mini[126] = 59999989;
      mini[127] = 19999998;
      mini[128] = 69999989;
      mini[129] = 29999998;
      mini[130] = 79999989;
      mini[131] = 39999998;
      mini[132] = 89999989;
      mini[133] = 49999998;
      mini[134] = 99999989;
      mini[135] = 59999998;
      mini[136] = 199999989;
      mini[137] = 69999998;
      mini[138] = 299999989;
      mini[139] = 79999998;
      mini[140] = 399999989;
      mini[141] = 89999998;
      mini[142] = 499999989;
      mini[143] = 99999998;
      mini[144] = 599999989;
      mini[145] = 199999998;
      mini[146] = 699999989;
      mini[147] = 299999998;
      mini[148] = 799999989;
      mini[149] = 399999998;
      mini[150] = 899999989;
      if (mini[n] != 0 || n == 1) {
        cout << mini[n] << '\n';
      } else {
        cout << -1 << '\n';
      }
    }
  }
  return 0;
}
