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 MAXD = 10;
const int MAXB = 11;
const int MAX_CNT_D = 20;
long long dp[MAXB][MAX_CNT_D][(1 << MAXB)];
long long dyn(int B, int cnt_d, int mask) {
  if (cnt_d == 0) {
    if (mask != 0) {
      return 0ll;
    }
    return 1ll;
  }
  if (dp[B][cnt_d][mask] != -1) {
    return dp[B][cnt_d][mask];
  }
  dp[B][cnt_d][mask] = 0;
  for (int d = 0; d < B; d++) {
    dp[B][cnt_d][mask] += dyn(B, cnt_d - 1, mask ^ (1 << d));
  }
  return dp[B][cnt_d][mask];
}
vector<int> get_digits(int n, int b) {
  vector<int> ds;
  ds.push_back(n % b);
  n /= b;
  while (n) {
    ds.push_back(n % b);
    n /= b;
  }
  reverse(ds.begin(), ds.end());
  return ds;
}
long long solv(long long n, int B) {
  vector<int> ds = get_digits(n, B);
  int cnt = ds.size();
  long long sol = 0;
  for (int i = 2; i < cnt; i++) {
    for (int d = 1; d < B; d++) {
      sol += dyn(B, i - 1, (1 << d));
    }
  }
  int mk = 0;
  for (int i = 0; i < ds.size(); i++) {
    int d = 1;
    if (i > 0) {
      d--;
    }
    for (; d < ds[i]; d++) {
      sol += dyn(B, ds.size() - i - 1, mk ^ (1 << d));
    }
    mk ^= (1 << ds[i]);
  }
  if (mk == 0) {
    sol++;
  }
  return sol;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (int i = 0; i < MAXB; i++) {
    for (int j = 0; j < MAX_CNT_D; j++) {
      for (int k = 0; k < (1 << MAXB); k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  int tc;
  cin >> tc;
  while (tc--) {
    long long b, l, r;
    cin >> b >> l >> r;
    cout << solv(r, b) - solv(l - 1, b) << '\n';
  }
}
