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>
template <class T>
inline T read(const T sample) {
  T x = 0;
  int f = 1;
  char s;
  while ((s = getchar()) > '9' || s < '0')
    if (s == '-') f = -1;
  while (s >= '0' && s <= '9')
    x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
  return x * f;
}
template <class T>
inline void write(const T x) {
  if (x < 0) return (void)(putchar('-'), write(-x));
  if (x > 9) write(x / 10);
  putchar(x % 10 ^ 48);
}
template <class T>
inline T Max(const T x, const T y) {
  return x > y ? x : y;
}
template <class T>
inline T Min(const T x, const T y) {
  return x < y ? x : y;
}
template <class T>
inline T fab(const T x) {
  return x > 0 ? x : -x;
}
template <class T>
inline T Gcd(const T x, const T y) {
  return y ? Gcd(y, x % y) : x;
}
int b, l[66], r[66];
long long f[66][2205][66];
long long DP(const int p, const int s, const bool lim, const bool zero) {
  if (!p) return s == 0;
  if (!lim && (~f[p][s][b])) return f[p][s][b];
  int up = lim ? r[p] : b - 1;
  long long r = 0;
  for (register signed i = (0), _end = (up); i <= _end; ++i) {
    if (zero && i == 0)
      r += DP(p - 1, s, lim && i == up, 1);
    else
      r += DP(p - 1, s ^ (1 << i), lim && i == up, 0);
  }
  if (!lim) f[p][s][b] = r;
  return r;
}
int main() {
  long long x, y, ans;
  memset(f, -1, sizeof f);
  for (int t = read(9); t; --t) {
    b = read(9), x = read(9ll) - 1, y = read(9ll);
    for (register signed i = (1), _end = (64); i <= _end; ++i) {
      l[i] = x % b;
      x /= b;
      r[i] = y % b;
      y /= b;
    }
    ans = DP(64, 0, 1, 1);
    for (register signed i = (1), _end = (64); i <= _end; ++i) r[i] = l[i];
    write(ans - DP(64, 0, 1, 1)), putchar('\n');
  }
  return 0;
}
