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 unsigned long long int N = 70, M = 11, MA = 1030;
unsigned long long int dp[M][N][M][MA], dps[M][N][MA], dpt[M][N], n, q, b, l, r,
    x;
vector<unsigned long long int> v;
unsigned long long int get(unsigned long long int a, unsigned long long int b) {
  unsigned long long int x = a, ans, mask = 0, l;
  v.resize(0);
  while (x > 0) {
    v.push_back(x % b);
    x /= b;
  }
  l = (unsigned long long int)v.size();
  ans = dpt[b][l];
  for (auto y : v) {
    for (int j = b - 1; j > y; j--) ans -= dp[b][l][j][mask];
    mask ^= (1 << y);
    l--;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  for (unsigned long long int bi = 2; bi <= 10; bi++) {
    for (int l = 2; l <= 65; l++) {
      for (int c = 0; c < bi; c++) {
        dp[bi][1][c][(1ll << c)] = 1;
        for (int mask = 0; mask < (1ll << bi); mask++)
          dps[bi][1][mask] += dp[bi][1][c][mask];
      }
      dpt[bi][1] = 0;
    }
  }
  for (unsigned long long int bi = 2; bi <= 10; bi++) {
    for (int l = 2; l <= 65; l++) {
      dpt[bi][l] = dpt[bi][l - 1];
      for (int c = 0; c < bi; c++) {
        for (int i = 0; i < bi; i++)
          for (int mask = 0; mask < (1ll << bi); mask++) {
            x = mask, x ^= (1ll << c);
            dp[bi][l][c][mask] += dp[bi][l - 1][i][x];
          }
        for (int mask = 0; mask < (1ll << bi); mask++)
          dps[bi][l][mask] += dp[bi][l][c][mask];
        if (c != 0) dpt[bi][l] += dp[bi][l][c][0];
      }
    }
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> b >> l >> r;
    l--;
    cout << get(r, b) - get(l, b) << '\n';
  }
}
