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 dp[10][10][62][(1 << 10)][2][2];
long long func(long long b, long long cur, long long pos, long long bit,
               long long fl, long long fl1, vector<long long> num) {
  if (num.size() == 0) return 0;
  if (pos == -1) return (bit == 0);
  if (dp[b][cur][pos][bit][fl][fl1] != -1) return dp[b][cur][pos][bit][fl][fl1];
  long long ma;
  if (fl == 0)
    ma = b - 1;
  else
    ma = cur;
  dp[b][cur][pos][bit][fl][fl1] = 0;
  for (long long i = 0; i <= ma; i++) {
    long long ff = 0, cur1, ff1 = fl1, bit1 = (bit ^ (1 << i));
    if (fl1 == 0 && i > 0) ff1 = 1;
    if (ff1 == 0) bit1 = 0;
    if (fl == 1 && i == ma) ff = 1;
    if (pos == 0)
      cur1 = 0;
    else if (ff == 1)
      cur1 = num[pos - 1];
    else
      cur1 = 0;
    dp[b][cur][pos][bit][fl][fl1] += func(b, cur1, pos - 1, bit1, ff, ff1, num);
  }
  return dp[b][cur][pos][bit][fl][fl1];
}
void solve() {
  memset(dp, -1, sizeof(dp));
  long long q;
  cin >> q;
  while (q--) {
    long long l1, r1, b;
    cin >> b >> l1 >> r1;
    l1--;
    vector<long long> l, r;
    long long c = 1;
    while (l1 > 0 || r1 > 0) {
      if (l1 > 0) l.push_back(l1 % b);
      if (r1 > 0) r.push_back(r1 % b);
      l1 /= b;
      r1 /= b;
      c *= b;
    }
    long long c1 = 0, c2 = 0;
    if (r.size() > 0) c1 = func(b, r.back(), r.size() - 1, 0, 1, 0, r);
    if (l.size() > 0)
      c2 = func(b, l.back(), l.size() - 1, 0, 1, 0, l);
    else
      c1--;
    cout << c1 - c2 << '\n';
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  double t1 = clock();
  long long t;
  t = 1;
  for (long long i = 1; i <= t; i++) {
    solve();
  }
  cerr << setprecision(10) << (clock() - t1) / CLOCKS_PER_SEC << "\n";
  return 0;
}
