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 unsigned int long long N = 60, L = 10;
unsigned int long long dp[L + 1][L + 1][N + 1][L + 1], q;
unsigned int long long fk(unsigned int long long x) {
  unsigned int long long rt = 1;
  for (unsigned int long long i = 1; i <= x; i++) rt *= i;
  return (rt);
}
unsigned int long long C(unsigned int long long n, unsigned int long long r) {
  return (fk(n) / fk(r) / fk(n - r));
}
string conv(unsigned int long long x, unsigned int long long a) {
  string s = "";
  while (x) {
    s.push_back(char(x % a + '0'));
    x /= a;
  }
  reverse(s.begin(), s.end());
  return (s);
}
void Do(set<char>& st, char c) {
  if (st.find(c) == st.end())
    st.insert(c);
  else
    st.erase(c);
}
bool C(string s) {
  set<char> st;
  for (char c : s) Do(st, c);
  return (st.empty());
}
unsigned int long long get(string s, char b) {
  unsigned int long long ln = (unsigned int long long)s.size();
  unsigned int long long rt = C(s);
  if (!(ln & 1)) {
    for (unsigned int long long i = 0; i < ln; i++)
      for (char c = '0'; c < s[i] && c < b; c++) {
        unsigned int long long rtt = 1;
        set<char> st;
        unsigned int long long lnb = ln - i - 1;
        if (!i && c == '0') continue;
        Do(st, c);
        for (unsigned int long long j = 0; j < i; j++) Do(st, s[j]);
        if ((unsigned int long long)st.size() > lnb) continue;
        rtt *= dp[b - '0'][b - '0'][lnb][(unsigned int long long)st.size()] /
               C(b - '0', (unsigned int long long)st.size()) /
               fk((unsigned int long long)st.size());
        rt += rtt;
      }
  }
  for (unsigned int long long i = 2; i < ln; i += 2) {
    unsigned int long long gt = dp[b - '0'][b - '0'][i][0];
    gt -= gt / (b - '0');
    rt += gt;
  }
  return (rt);
}
int32_t main() {
  for (unsigned int long long i = 0; i <= L; i++)
    for (unsigned int long long j = 0; j <= L; j++) dp[i][j][0][0] = 1;
  for (unsigned int long long b = 1; b <= L; b++) {
    for (unsigned int long long bp = 1; bp <= b; bp++) {
      for (unsigned int long long tl = 1; tl <= N; tl++) {
        for (unsigned int long long k = 0; k <= bp; k++) {
          if (!k) {
            for (unsigned int long long j = 0; j <= tl; j += 2) {
              dp[b][bp][tl][0] += C(tl, j) * dp[b][bp - 1][tl - j][0];
            }
            continue;
          }
          for (unsigned int long long j = 1; j <= tl; j += 2) {
            dp[b][bp][tl][k] += bp * C(tl, j) * dp[b][bp - 1][tl - j][k - 1];
          }
        }
      }
    }
  }
  scanf("%lld", &q);
  for (unsigned int long long Q = 0; Q < q; Q++) {
    unsigned int long long b, l, r;
    scanf("%lld %lld %lld", &b, &l, &r);
    unsigned int long long rt = get(conv(r, b), char(b + '0'));
    if (l > 1) rt -= get(conv(l - 1, b), char(b + '0'));
    printf("%lld\n", rt);
  }
}
