10
4 108 114
5 30 155
8 193 197
9 71 169
2 163 166
8 120 144
8 22 151
4 21 166
2 46 127
8 38 51
 20
9 142 172
7 132 256
8 245 315
9 496 496
3 345 362
8 13 162
5 342 470
9 16 488
9 467 482
4 471 478
10 92 224
6 228 261
9 54 167
2 402 409
5 9 10
7 225 360
3 438 483
6 252 342
3 491 493
6 296 367
 1
8 1234567890123 123456789012345

0
3
0
1
3
0
5
16
9
1
 0
0
0
0
4
6
13
7
0
0
1
2
3
0
0
2
12
9
1
4
 774752389568

#include <bits/stdc++.h>
using namespace std;
const int len = 64;
long long int dp[len + 1][1024][10];
string convert10tob(long long int x, long long int y) {
  string s;
  while (x != 0) {
    long long int c = (x % y);
    x = (x / y);
    s += (c + '0');
  }
  return s;
}
long long int getNum(string num, int base) {
  long long int res = 0;
  int bigMask = 0;
  for (int len = num.length(); len >= 1; len--) {
    int dec = num[num.length() - len] - '0';
    if (len == num.length()) {
      for (int first = dec - 1; first > 0; first--) {
        res += dp[len - 1][bigMask ^ (1 << first)][base - 1];
      }
    } else {
      for (int first = dec - 1; first >= 0; first--) {
        res += dp[len - 1][bigMask ^ (1 << first)][base - 1];
      }
    }
    if (len < num.length()) {
      for (int first = base - 1; first > 0; first--) {
        res += dp[len - 1][1 << first][base - 1];
      }
    }
    bigMask ^= (1 << dec);
  }
  return res + (bigMask == 0 ? 1 : 0);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int b = 0; b < 10; b++) {
    dp[0][0][b] = 1;
  }
  for (int base = 1; base <= 10; base++)
    for (int pos = 1; pos <= len; pos++)
      for (int mask = 0; mask < (1 << 10); mask++) {
        long long int &res = dp[pos][mask][base - 1];
        for (int num = 0; num < base; num++) {
          res = (res + dp[pos - 1][mask ^ (1 << num)][base - 1]) % (1000000007);
        }
      }
  int q;
  cin >> q;
  while (q--) {
    int b;
    string l, r;
    cin >> b >> l >> r;
    string lS = convert10tob(stoll(l) - 1, b);
    if (lS == "") lS = "0";
    reverse(lS.begin(), lS.end());
    string rS = convert10tob(stoll(r), b);
    if (rS == "") rS = "0";
    reverse(rS.begin(), rS.end());
    long long int R = getNum(rS, b);
    long long int L = getNum(lS, b);
    cout << R - L << '\n';
  }
  return 0;
}
