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;
int len, A[65], base;
long long l, r, F[11][65][2050], C[65][65];
inline int daobit(int a, int b) { return (a ^ (1 << b)); }
void precal() {
  memset(F, 0, sizeof(F));
  for (int p = 2; p <= 10; p++) {
    for (int i = 0; i <= p - 1; i++) F[p][1][daobit(0, i)] = 1;
    int cur = (1 << p) - 1;
    F[p][0][cur] = 1;
    for (int i = 2; i <= 65 - 1; i++)
      for (int j = 0; j <= cur; j++) {
        F[p][i][j] = 0;
        for (int k = 0; k <= p - 1; k++)
          F[p][i][j] += F[p][i - 1][daobit(j, k)];
      }
  }
  memset(C, 0, sizeof(C));
  C[0][0] = 1;
  for (int i = 1; i <= 65 - 1; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j <= i - 1; j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
}
void baseconvert(long long x, int base) {
  len = 0;
  while (x) {
    int r = x % base;
    A[++len] = r;
    x /= base;
  }
  reverse(A + 1, A + 1 + len);
}
long long cal(long long a) {
  if (!a) return 0;
  baseconvert(a, base);
  long long res = 0;
  for (int i = 2; i <= len - 1; i++)
    if (!(i % 2)) res += (F[base][i][0] - F[base][i - 1][1]);
  int tmp = 0;
  for (int i = 1; i <= len; i++) {
    for (int j = 0; j <= A[i] - 1; j++) {
      if ((i == 1 && !j)) continue;
      tmp = daobit(tmp, j);
      res += F[base][len - i][~tmp];
      tmp = daobit(tmp, j);
    }
    tmp = daobit(tmp, A[i]);
  }
  if (!tmp) return res + 1;
  return res;
}
int main() {
  int q;
  scanf("%d", &q);
  precal();
  while (q--) {
    scanf("%d%lld%lld", &base, &l, &r);
    long long t1 = cal(l - 1), t2 = cal(r);
    printf("%lld\n", t2 - t1);
  }
  return 0;
}
