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 void read(T& x) {
  char c;
  bool f = 0;
  while ((c = getchar()) < '0' || '9' < c) f |= (c == '-');
  for (x = (c ^ 48); '0' <= (c = getchar()) && c <= '9';
       x = (x << 1) + (x << 3) + (c ^ 48))
    ;
  if (f) x = -x;
}
template <class T>
inline T read(const T sample) {
  T x = 0;
  char c;
  bool f = 0;
  while ((c = getchar()) < '0' || '9' < c) f |= (c == '-');
  for (x = (c ^ 48); '0' <= (c = getchar()) && c <= '9';
       x = (x << 1) + (x << 3) + (c ^ 48))
    ;
  return f ? -x : x;
}
template <class T>
void fwrit(const T x) {
  if (x < 0) return (void)(putchar('-'), fwrit(-x));
  if (x > 9) fwrit(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;
}
inline int gcd(const int a, const int b) { return b ? gcd(b, a % b) : a; }
inline void getInv(int inv[], const int lim, const int MOD) {
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= lim; ++i)
    inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD;
}
inline long long mulMod(const long long a, const long long b,
                        const long long mod) {
  return ((a * b - (long long)((long double)a / mod * b + 1e-8) * mod) % mod +
          mod) %
         mod;
}
const int maxk = 10;
const int maxl = 60;
const int maxsize = 1 << 10;
int b;
long long l, r;
inline void Init() { b = read(1), l = read(1ll), r = read(1ll); }
int a[maxsize + 5], len;
int f[maxk + 5][maxl + 5][maxsize + 5][2][2];
int Dfs(const int k, const int pos, const int s, const int pz, const int rl) {
  if (pos == 0) return s == 0;
  if (~f[k][pos][s][pz][rl]) return f[k][pos][s][pz][rl];
  int& ret = f[k][pos][s][pz][rl];
  ret = 0;
  int up = rl ? (a[pos]) : (k - 1), nst;
  for (signed i = (0), i_end_ = (up); i <= i_end_; ++i) {
    nst = (pz && (i == 0)) ? s : s ^ (1 << i);
    ret += Dfs(k, pos - 1, nst, pz && (i == 0), rl && (i == up));
  }
  return ret;
}
inline int calc(const int k, const long long x) {
  len = 0;
  long long tmp = x;
  memset(a, 0, sizeof a);
  while (tmp) {
    a[++len] = tmp % k;
    tmp /= k;
  }
  return Dfs(k, len, 0, 1, 1);
}
signed main() {
  memset(f, -1, sizeof f);
  for (signed i = (1), i_end_ = (read(1)); i <= i_end_; ++i) {
    Init();
    fwrit(calc(b, r) - calc(b, l - 1)), putchar('\n');
  }
  return 0;
}
