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 N = 10;
const int A = 12;
long long ways[N][A];
string to_base(long long val, int base) {
  string ret = "";
  while (val > 0) {
    ret += ('0' + val % base);
    val /= base;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
inline long long nPr(long long n, long long r) {
  long long ret = 1;
  for (long long i = n; i > n - r; --i) ret *= i;
  return ret;
}
inline long long nCr(long long n, long long r) {
  long long ret = nPr(n, r);
  for (long long i = 1; i <= r; ++i) ret /= i;
  return ret;
}
int mask[N];
long long dp[N];
void preCal() {
  ways[0][0] = 1;
  for (int a = 1; a < A; ++a) {
    for (int f = 0; f < N; ++f) {
      ways[f][a] = ways[f][a - 1];
      for (int c = 2; c <= f; c += 2) {
        ways[f][a] += nCr(f, c) * ways[f - c][a - 1];
      }
    }
  }
}
long long get_val(long long x, int base) {
  string s = to_base(x, base);
  int n = s.size();
  if (n < 2) return 0;
  mask[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int c = s[i - 1] - '0';
    mask[i] = mask[i - 1] ^ (1 << c);
  }
  dp[n] = (mask[n] == 0);
  for (int i = n - 1; i >= 0; --i) {
    int dig = s[i] - '0';
    int gap = n - i;
    dp[i] = 0;
    dp[i] += dp[i + 1];
    for (int c = 0; c < dig; ++c) {
      if (i == 0 and c == 0) {
        long long allzer = 0;
        for (int f = gap - 1; f >= 2; --f) {
          for (int take = 2; take <= f; take += 2) {
            allzer += nCr(base - 1, 1) * nCr(f - 1, take - 1) *
                      ways[f - take][base - 1];
          }
        }
        dp[i] += allzer;
      } else {
        int nmask = mask[i] ^ (1 << c);
        int cnt = __builtin_popcount(nmask);
        if (cnt <= gap - 1) {
          dp[i] += nPr(gap - 1, cnt) * ways[gap - 1 - cnt][base];
        }
      }
    }
  }
  return dp[0];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  preCal();
  int q;
  cin >> q;
  while (q--) {
    int base;
    long long l, r;
    cin >> base >> l >> r;
    long long res = get_val(r, base) - get_val(l - 1, base);
    cout << res << "\n";
  }
  return 0;
}
