#include <bits/stdc++.h>
using namespace std;
const long long MX = 6e4, C = 10, L = 20;
long long nxt[MX][C], sz;
bitset<170> b[MX];
unordered_map<bitset<170>, long long> id;
inline long long low_bit(bitset<170> s) {
  for (long long i = 0; i < 10; i++)
    if (s.test(i)) return i;
  return -1;
}
inline long long extend(bitset<170> s) {
  if (id.count(s)) return id[s];
  id[s] = sz;
  b[sz] = s;
  return sz++;
}
void find_states() {
  bitset<170> s;
  s.reset();
  s.set(0);
  extend(s);
  for (long long i = 0; i < sz; i++) {
    for (long long j = 0; j < C; j++) {
      bitset<170> s = (b[i] << j | b[i] >> j);
      for (long long k = 0; k <= j; k++)
        if (b[i].test(k)) s.set(j - k);
      nxt[i][j] = extend(s);
    }
  }
}
long long dp[L][MX][C];
void calc_dps() {
  for (long long state = 0; state < sz; state++)
    for (long long k = 0; k < C; k++)
      dp[0][state][k] = (low_bit(b[state]) <= k);
  for (long long len = 1; len < L; len++) {
    for (long long state = 0; state < sz; state++) {
      for (long long k = 0; k < C; k++) {
        for (long long c = 0; c < C; c++) {
          dp[len][state][k] += dp[len - 1][nxt[state][c]][k];
        }
      }
    }
  }
}
long long q;
long long digit[L];
inline long long get(long long lim, long long k) {
  long long cp = lim;
  for (long long i = 0; i < L; i++, cp /= 10) {
    digit[i] = cp % 10;
  }
  long long res = 0;
  long long state = 0;
  for (long long i = L - 1; i >= 0; i--) {
    for (long long j = 0; j < digit[i]; j++) {
      res += dp[i][nxt[state][j]][k];
    }
    state = nxt[state][digit[i]];
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  find_states();
  calc_dps();
  cin >> q;
  while (q--) {
    long long l, r, k;
    cin >> l >> r >> k;
    r++;
    cout << get(r, k) - get(l, k) << '\n';
  }
}
