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;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
  if (a < 0) return Gcd(-a, b);
  if (b < 0) return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
  if (a < 0) return Lcm(-a, b);
  if (b < 0) return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream second(s);
  second >> sm;
  return sm;
}
long long int dp[12][63][2][2][(1 << 10) + 2];
int a[62];
int L;
int bs;
void Make(long long int n) {
  while (n > 0) {
    a[L++] = n % bs;
    n /= bs;
  }
  reverse(a, a + L);
}
long long int lol(int bk, int ch, int st, int ms) {
  if (bk == 0) {
    if (!st) return 0;
    for (int i = 0; i < bs; i++) {
      if (ms & (1 << i)) return 0;
    }
    return 1;
  }
  long long int &res = dp[bs][bk][ch][st][ms];
  if (res != -1) return res;
  res = 0;
  int lo = 0;
  int hi = a[(L - bk)];
  if (ch) hi = bs - 1;
  for (int i = 0; i < hi + 1; i++) {
    int nms = ms;
    if (st == 0 && i == 0) {
    } else {
      nms = (nms ^ (1 << i));
    }
    res += lol(bk - 1, ch | (i < a[L - bk]), st | i, nms);
  }
  return res;
}
long long int asa(long long int n) {
  L = 0;
  if (n == 0) return 0;
  Make(n);
  return lol(L, 0, 0, 0);
}
int main() {
  memset(dp, -1, sizeof(dp));
  int q;
  q = in<int>();
  while (q--) {
    memset(dp, -1, sizeof(dp));
    long long int l, r;
    bs = in<int>();
    l = in<long long int>(), r = in<long long int>();
    long long int ans = asa(r) - asa(l - 1);
    printf("%lld\n", ans);
  }
  return 0;
}
