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[11][19][1LL << (11)];
void precomp() {
  for (int base = 2; base <= 10; base++) {
    for (int mask = 0; mask < (1LL << base); mask++) {
      dp[base][0][mask] = 0;
    }
    dp[base][0][0] = 1;
    for (int len = 1; len <= 18; len++) {
      for (int mask = 0; mask < (1LL << base); mask++) {
        for (int at = 0; at < base; at++) {
          dp[base][len][mask ^ (1LL << at)] += dp[base][len - 1][mask];
        }
      }
    }
  }
}
long long calc(int base, long long val) {
  vector<int> at;
  while (val > 0) {
    at.push_back(val % base);
    val /= base;
  }
  int size = at.size();
  long long ans = 0;
  for (int i = 1; i < size; i++) {
    ans += dp[base][i][0] - dp[base][i - 1][1];
  }
  int mask = 0;
  for (int i = size - 1; i >= 0; i--) {
    for (int j = 0; j < at[i]; j++) {
      if (j == 0) continue;
      ans += dp[base][i][mask | (1LL << j)];
    }
    mask |= (1 << at[i]);
  }
  return ans;
}
int main() {
  precomp();
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int b;
    long long l, r;
    scanf("%d%lld%lld", &b, &l, &r);
    long long ans = calc(b, r) - calc(b, l - 1);
    printf("%lld\n", ans);
  }
}
