#include <bits/stdc++.h>
using namespace std;
const int B = 172, N = 44005;
using ll = long long;
using Bit = bitset<B>;
unordered_map<Bit, int> mp;
Bit aa, a[N];
bool vis[20][N];
int cnt = 1, nxt[N][10];
ll dp[20][10][N];
int b[20];
ll solve(ll n, int kk) {
  if (!n) return 1;
  int m = 0, pos = 1;
  while (n) b[m++] = n % 10, n /= 10;
  ll ret = 0;
  for (int i = m - 1; i >= 0; --i) {
    for (int j = 0; j < b[i] + !i; j++) ret += dp[i][kk][nxt[pos][j]];
    pos = nxt[pos][b[i]];
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  aa[0] = 1;
  a[mp[aa] = 1] = aa;
  vis[0][1] = 1;
  for (int i = 1; i < 20; i++)
    for (int p = 1; p <= cnt; p++)
      if (vis[i - 1][p])
        for (int j = 0; j < 10; j++) {
          if (!nxt[p][j]) {
            if (!j)
              nxt[p][j] = p;
            else {
              aa.reset();
              for (int k = j; k < B; k++)
                if (a[p][k]) aa[k - j] = 1;
              for (int k = 0; k < B - j; k++)
                if (a[p][k]) aa[k + j] = 1;
              for (int k = 1; k < j; k++)
                if (a[p][k]) aa[j - k] = 1;
              if (mp.count(aa))
                nxt[p][j] = mp[aa];
              else
                a[nxt[p][j] = mp[aa] = ++cnt] = aa;
            }
          }
          vis[i][nxt[p][j]] = 1;
        }
  for (int j = 1; j <= cnt; j++) {
    int k = 0;
    while (k < 10)
      if (!a[j][k])
        ++k;
      else
        break;
    while (k < 10) dp[0][k++][j] = 1;
  }
  for (int j = 1; j < 19; j++)
    for (int k = 1; k <= cnt; k++)
      if (vis[19 - j][k])
        for (int i = 0; i < 10; i++)
          for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
  int T;
  cin >> T;
  while (T--) {
    ll l, r;
    cin >> l >> r;
    int kk;
    cin >> kk;
    cout << solve(r, kk) - solve(l - 1, kk) << endl;
  }
  return 0;
}
