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;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const long long maxn = 3e2 + 10, maxm = 1e3 + 10, lg = 61, mod = 1e9 + 7,
                inf = 1e18;
long long q, dp[11][(1 << 10)][61];
string tbase(long long x, long long b) {
  if (b == 10) {
    string ret = "";
    while (x) ret += char((x % 10) + '0'), x /= 10;
    return ret;
  }
  long long xx = 1;
  while (xx <= x) xx *= b;
  xx /= b;
  string ret = "";
  while (xx >= 1) ret += char((x / xx) + '0'), x %= xx, xx /= b;
  return ret;
}
long long get(string s, long long b) {
  long long ret = 0, nask = 0;
  for (int len = 1; len < (long long)(s.size()); len++)
    for (int j = 1; j < b; j++) ret += dp[b][1 << j][len - 1];
  for (int i = 0; i < (long long)(s.size()); i++) {
    long long it = s[i] - '0';
    for (int j = 0 + (i == 0); j < it; j++)
      ret += dp[b][nask ^ (1 << j)][(long long)(s.size()) - i - 1];
    nask ^= (1 << it);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (int b = 2; b <= 10; b++) {
    dp[b][0][0] = 1;
    for (int len = 1; len <= 60; len++)
      for (int mask = 0; mask < (1 << b); mask++)
        for (int j = 0; j < b; j++)
          dp[b][mask][len] += dp[b][mask ^ (1 << j)][len - 1];
  }
  cin >> q;
  while (q--) {
    long long b, l, r;
    cin >> b >> l >> r;
    cout << get(tbase(r + 1, b), b) - get(tbase(l, b), b) << '\n';
  }
  return 0;
}
