#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) {
  long long z = y;
  while (x % y != 0) {
    z = x % y;
    x = y;
    y = z;
  }
  return z;
}
const int B = 165;
int nxt[41005][10], cnt;
bool vis[20][41005];
unordered_map<bitset<B>, int> id;
bitset<B> rid[41005];
long long dp[20][10][41005];
void init() {
  bitset<B> bs;
  bs.reset();
  bs[0] = 1;
  id[bs] = ++cnt;
  rid[cnt] = bs;
  vis[0][1] = 1;
  for (int(i) = 1; (i) <= (int)(19); (i)++) {
    for (int u = 1; u <= cnt; u++)
      if (vis[i - 1][u]) {
        for (int(j) = 0; (j) < (int)(10); (j)++) {
          if (nxt[u][j] == 0) {
            if (j == 0)
              nxt[u][j] = u;
            else {
              bs.reset();
              bs |= rid[u] >> j;
              bs |= rid[u] << j;
              for (int k = 1; k < j; k++)
                if (rid[u][k]) bs[j - k] = 1;
              if (id.count(bs))
                nxt[u][j] = id[bs];
              else {
                id[bs] = ++cnt;
                rid[cnt] = bs;
                nxt[u][j] = cnt;
              }
            }
          }
          vis[i][nxt[u][j]] = 1;
        }
      }
  }
  for (int(i) = 1; (i) <= (int)(cnt); (i)++) {
    int j = 0;
    for (; j < 10; j++)
      if (rid[i][j] == 1) break;
    for (; j < 10; j++) dp[0][j][i] = 1;
  }
  for (int(i) = 1; (i) <= (int)(19); (i)++) {
    for (int(k) = 1; (k) <= (int)(cnt); (k)++)
      if (vis[19 - i][k]) {
        for (int(j) = 0; (j) < (int)(10); (j)++)
          for (int(l) = 0; (l) < (int)(10); (l)++) {
            dp[i][l][k] += dp[i - 1][l][nxt[k][j]];
          }
      }
  }
}
long long cal(long long x, int k) {
  if (x == 0) return 1;
  vector<int> bs;
  while (x) {
    bs.push_back(x % 10);
    x /= 10;
  }
  long long ans = 0;
  int u = 1;
  for (int i = (int)bs.size() - 1; ~i; i--) {
    for (int j = 0; j < bs[i] + (i == 0); j++) ans += dp[i][k][nxt[u][j]];
    u = nxt[u][bs[i]];
  }
  return ans;
}
void fmain(int ID) {
  init();
  scanf("%d", &n);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    long long l, r;
    scanf("%lld%lld%d", &l, &r, &k);
    printf("%lld\n", cal(r, k) - cal(l - 1, k));
  }
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
