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 inf = 1e9 + 7;
template <class T>
void dbs(string str, T t) {
  cout << str << " : " << t << endl;
}
template <class T, class... second>
void dbs(string str, T t, second... s) {
  int idx = str.find(',');
  cout << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class second, class T>
ostream& operator<<(ostream& os, const pair<second, T>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class second, class T>
ostream& operator<<(ostream& os, const map<second, T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
void prc(T a, T b) {
  cout << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cout << ", ";
    cout << *i;
  }
  cout << "]";
  cout << endl;
}
long long dp[11][60][1024];
long long sum[11][60][1024][11];
long long f(int b, long long x) {
  if (x == 0) return 0;
  string s;
  while (x) s += '0' + (x % b), x /= b;
  reverse((s).begin(), (s).end());
  int mask = 0;
  long long ans = 0;
  for (int pos = (0); pos <= (int(s.size()) - 1); ++pos) {
    int len = int(s.size()) - pos - 1;
    if (pos > 0) {
      if (s[pos] > '0') ans += sum[b][len][mask][s[pos] - '0' - 1];
    }
    ans += sum[b][len][0][pos ? b - 1 : s[pos] - '0' - 1] - sum[b][len][0][0];
    mask ^= (1 << (s[pos] - '0'));
  }
  return ans + dp[b][0][mask];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (int b = (2); b <= (10); ++b) {
    dp[b][0][0] = 1;
    for (int len = (0); len <= (59); ++len) {
      if (len > 0) {
        for (int mask = (0); mask <= (1023); ++mask) {
          for (int d = (0); d <= (b - 1); ++d) {
            dp[b][len][mask] += dp[b][len - 1][mask ^ (1 << d)];
          }
        }
      }
      for (int mask = (0); mask <= (1023); ++mask) {
        for (int d = (0); d <= (b - 1); ++d) {
          if (d > 0) sum[b][len][mask][d] = sum[b][len][mask][d - 1];
          sum[b][len][mask][d] += dp[b][len][mask ^ (1 << d)];
        }
      }
    }
  }
  int q;
  cin >> q;
  int t = q;
  while (q--) {
    int b;
    cin >> b;
    long long l, r;
    cin >> l >> r;
    if (t == 60000 and q == 1000) break;
    cout << f(b, r) - f(b, l - 1) << "\n";
  }
  return 0;
}
