text: /*
   * FILE: 1130D1.cpp
   *
   * @author: Arafat Hasan Jenin <opendoor.arafat[at]gmail[dot]com>
   *
   * LINK:
   *
   * DATE CREATED: 25-02-19 21:27:24 (+06)
   * LAST MODIFIED:
   *
   * VERDICT: Almost Accepted
   *
   * DEVELOPMENT HISTORY:
   * Date         Version     Description
   * --------------------------------------------------------------------
   * 25-02-19     1.0         Deleted code is debugged code.
   *
   *               _/  _/_/_/_/  _/      _/  _/_/_/  _/      _/
   *              _/  _/        _/_/    _/    _/    _/_/    _/
   *             _/  _/_/_/    _/  _/  _/    _/    _/  _/  _/
   *      _/    _/  _/        _/    _/_/    _/    _/    _/_/
   *       _/_/    _/_/_/_/  _/      _/  _/_/_/  _/      _/
   */
  
  ///////////////////////////////////////////////////////////////////////
  
  #include <iostream>
  #include <climits>
  #include <cmath>
  #include <cstring>
  #include <cctype>
  #include <cstdio>
  #include <cstdlib>
  #include <iomanip>
  #include <utility>
  #include <sstream>
  #include <algorithm>
  #include <stack>
  #include <set>
  #include <list>
  #include <map>
  #include <unordered_map>
  #include <unordered_set>
  #include <queue>
  #include <deque>
  #include <vector>
  #include <tuple>
  #include <stdint.h> //uint32_t
  #include <functional>
  #include <bitset>
  #include <unistd.h> // unsigned int sleep(unsigned int seconds);
  
  using namespace std;
  
  typedef long long           ll;
  typedef double              lf;
  typedef long double         llf;
  typedef unsigned long long  ull;
  typedef pair<int, int>      pii;
  typedef vector<pii>         vpii;
  typedef vector<int>         vi;
  typedef vector<long long>   vl;
  
  #define _USE_MATH_DEFINES
  
  #define forr(i, a, b)   for (__typeof (a) i = (a); i <= (b); i++)
  #define rof(i, b, a)    for (__typeof (a) i = (b); i >= (a); i--)
  #define rep(i, n)       for (__typeof (n) i = 0; i < (n); i++)
  #define forit(i, s)     for (__typeof ((s).end()) i = (s).begin(); i != (s).end(); ++i)
  #define all(ar)         ar.begin(), ar.end()
  #define fill(a, val)    memset((a), (val), sizeof((a)))
  #define clr(a)          memset((a), 0, sizeof((a)))
  #define sz(a)           (int) a.size()
  
  #define pb              push_back
  
  #ifndef ONLINE_JUDGE
      #define nl              cerr << '\n'
      #define sp              cerr << ' '
      #define ckk             cerr << "###############\n"
      #define debug1(x)       cerr << #x << ": " << (x) << '\n'
      #define debug2(x, y)    cerr << #x << ": " << (x) << '\t' << #y << ": " << (y) << '\n'
      #define debug3(x, y, z) cerr << #x << ": " << (x) << '\t' << #y << ": " << (y) << '\t' << #z << ": " << (z) << '\n'
  #else
      #define nl
      #define sp
      #define ckk
      #define debug1(x)
      #define debug2(x, y)
      #define debug3(x, y, z)
  #endif
  
  #define PI              acos(-1.0)
  #define INF             0x7fffffff
  #define MOD             1000000007
  #define EPS             1e-7
  #define MAX             10000007 //1e7+7
  
  ////////////////////////// START HERE //////////////////////////
  
  vector<int> num;
  int k;
  
  int digitDP (int pos, int sum, int rem, bool flag) {
      if (pos == (int) num.size()) {
          if (sum % k == 0 and rem == 0) return 1;
  
          return 0;
      }
  
      int res = 0, limit = flag ? 9 : num[pos];
  
      for (int dgt = 0; dgt < limit; dgt++) {
          bool nFlag = dgt < limit ? 1 : flag;
          int nRem = (rem + (((dgt % k * (int) pow (10, (int)num.size() - pos - 1))\
                              % k)) % k) % k;
          res += digitDP (pos + 1, sum + dgt, nRem, nFlag);
      }
  
      return res;
  }
  
  int solve (int n) {
      num.clear();
  
      while (n > 0) {
          num.pb (n % 10);
          n /= 10;
      }
  
      reverse (all (num));
      return digitDP (0, 0, 0, 0);
  }
  
  int main() {
      ios_base::sync_with_stdio (false); cin.tie (0); cout.tie (0);
      int t, a, b, cs = 0;
      cin >> t;
  
      while (t--) {
          cin >> a >> b >> k;
          //b--;
          //cout << "Case " << ++cs << ": ";
          //cout << solve (b) - solve (a) << endl;
          debug1(solve(a));
      }
  
      return 0;
  }
  