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>
constexpr int MAX_N = 100100;
constexpr int MAX_BASE = 11;
constexpr int MAX_LENGTH = 100;
using namespace std;
int n;
int base[MAX_N];
long long left[MAX_N];
long long right[MAX_N];
long long dp[MAX_BASE][MAX_LENGTH][1 << MAX_BASE][MAX_BASE];
vector<int> digits(int b, long long k) {
  vector<int> ans;
  while (k > 0) {
    ans.push_back(k % b);
    k /= b;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
long long solve(int b, long long k) {
  int mask = 0;
  long long ans = 0;
  vector<int> num = digits(b, k);
  for (int i = 1; i < num.size(); i++) {
    for (int digit = 1; digit < b; digit++) {
      ans += dp[b][i][0][digit];
    }
  }
  for (int i = 0; i < num.size(); i++) {
    int digit = num[i];
    for (int smaller_digit = (i == 0 ? 1 : 0); smaller_digit < digit;
         smaller_digit++) {
      ans += dp[b][num.size() - i][mask][smaller_digit];
    }
    mask ^= 1 << digit;
  }
  return ans;
}
int main() {
  scanf(" %d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %d %lld %lld", &base[i], &left[i], &right[i]);
  }
  for (int b = 2; b < MAX_BASE; b++) {
    for (int digit = 0; digit < b; digit++) {
      dp[b][1][1 << digit][digit] = 1;
    }
    for (int length = 2; length < MAX_LENGTH; length++) {
      for (int mask = 0; mask < (1 << b); mask++) {
        for (int digit = 0; digit < b; digit++) {
          int new_mask = mask ^ (1 << digit);
          for (int last_digit = 0; last_digit < b; last_digit++) {
            dp[b][length][mask][digit] +=
                dp[b][length - 1][new_mask][last_digit];
          }
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    printf("%lld\n", solve(base[i], right[i]) - solve(base[i], left[i] - 1));
  }
  return 0;
}
