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;
long long int dp[12][65][2070];
long long int func(long long int n, long long int b) {
  long long int d = 0;
  long long int xx = 1;
  if (n == 0) {
    return 0;
  }
  long long int power[70] = {0};
  power[0] = 1;
  while (xx <= n) {
    if (xx == 1000000000000000000) {
      d++;
      break;
    }
    xx *= b;
    d++;
  }
  for (long long int i = 1; i <= d - 1; i++) {
    power[i] = power[i - 1];
    long long int zz = b;
    power[i] *= zz;
  }
  long long int ans = 0;
  long long int noo = n;
  for (long long int i = d; i >= d; i--) {
    long long int curr = 0;
    long long int s = noo;
    for (long long int j = i; j >= 1; j--) {
      long long int dig = s / power[j - 1];
      for (long long int l = 0; l < dig; l++) {
        if (l == 0) {
          if (j == i) {
            continue;
          }
        }
        curr ^= (1 << l);
        ans += dp[b][j - 1][curr];
        curr ^= (1 << l);
      }
      s -= dig * power[j - 1];
      curr ^= (1 << dig);
    }
    if (curr == 0) {
      ans++;
    }
    noo -= power[i - 1] * (noo / power[i - 1]);
  }
  for (long long int i = d - 1; i >= 1; i--) {
    for (long long int l = 1; l <= b - 1; l++) {
      ans += dp[b][i - 1][1 << l];
    }
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int q;
  cin >> q;
  for (long long int b = 2; b <= 10; b++) {
    dp[b][0][0] = 1;
    for (long long int j = 0; j <= 59; j++) {
      for (long long int k = 0; k < (1 << b); k++) {
        for (long long int l = 0; l < b; l++) {
          dp[b][j + 1][k ^ (1 << l)] += dp[b][j][k];
        }
      }
    }
  }
  while (q--) {
    long long int b, l, r;
    cin >> b >> l >> r;
    cout << func(r, b) - func(l - 1, b) << "\n";
  }
}
