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;
const int MOD = (int)1e9 + 7;
const int MOD2 = 1007681537;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
  long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
long long dp[100][11][2][1 << 10];
long long calc(int k, int b, int fa, int msk) {
  if (!k) return msk == 0 && fa;
  long long& res = dp[k][b][fa][msk];
  if (~res) return res;
  res = 0;
  for (int i = (0); i < (b); i++) {
    int nfa = fa | (i > 0);
    if (nfa) {
      res += calc(k - 1, b, nfa, msk ^ (1 << i));
    } else {
      res += calc(k - 1, b, nfa, msk);
    }
  }
  return res;
}
int tot;
inline int getmsk(long long x, int b) {
  tot++;
  int res = 0;
  while (x) {
    res ^= 1 << (x % b);
    x /= b;
  }
  return res;
}
long long calc(int b, long long l) {
  if (!l) return 0;
  vector<int> vals;
  while (l) {
    vals.push_back(l % b);
    l /= b;
  }
  reverse((vals).begin(), (vals).end());
  long long res = 0;
  long long cur = 0;
  int msk = 0;
  for (int i = (0); i < (int((vals).size())); i++) {
    for (int j = (0); j < (vals[i]); j++) {
      long long ncur = cur * b + j;
      res += calc(int((vals).size()) - i - 1, b, ncur > 0,
                  ncur ? msk | (1 << j) : msk);
    }
    msk |= 1 << vals[i];
    cur = cur * b + vals[i];
  }
  if (!getmsk(cur, b)) res++;
  return res;
}
void solve() {
  memset(dp, -1, sizeof(dp));
  int q;
  cin >> q;
  while (q--) {
    int b;
    long long l, r;
    cin >> b >> l >> r;
    cout << calc(b, r) - calc(b, l - 1) << "\n";
  }
}
int main() {
  int JUDGE_ONLINE = 1;
  if (fopen("in.txt", "r")) {
    JUDGE_ONLINE = 0;
    assert(freopen("in.txt", "r", stdin));
  } else {
    ios_base::sync_with_stdio(0), cin.tie(0);
  }
  solve();
  if (!JUDGE_ONLINE) {
  }
  return 0;
}
