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 MAXN = (int)5e5 + 228;
const char nxtl = '\n';
const int mod = (int)1e9 + 7;
const double eps = (double)1e-7;
template <typename T>
inline bool updmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool updmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
long long dp[100][2][2][64];
int sz, arr[64], b, timer, clr[100][2][2][64], rnd[10];
long long calc(int msk, bool beg, bool sp, int pos) {
  if (pos == sz) {
    if (msk == 0)
      return 1;
    else
      return 0;
  }
  long long &res = dp[msk][beg][sp][pos];
  if (clr[msk][beg][sp][pos] == timer) return res;
  clr[msk][beg][sp][pos] = timer;
  res = 0;
  if (sp) {
    for (int dig = 0; dig <= arr[pos]; ++dig) {
      if (dig == 0 && !beg) {
        res += calc(msk, 0, dig == arr[pos], pos + 1);
      } else
        res += calc(msk ^ rnd[dig], 1, dig == arr[pos], pos + 1);
    }
  } else {
    for (int dig = 0; dig <= b - 1; ++dig) {
      if (dig == 0 && !beg) {
        res += calc(msk, 0, 0, pos + 1);
      } else
        res += calc(msk ^ rnd[dig], 1, 0, pos + 1);
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  unsigned int FOR;
  asm("rdtsc" : "=A"(FOR));
  srand(FOR);
  vector<int> vv;
  for (int i = 0; i <= 30; ++i) vv.push_back(i);
  random_shuffle(vv.begin(), vv.end());
  for (int i = 0; i <= 9; ++i) {
    rnd[i] = vv.back();
    vv.pop_back();
  }
  int q;
  cin >> q;
  while (q--) {
    timer++;
    long long l, r;
    cin >> b >> l >> r;
    long long res = 0;
    sz = 0;
    while (r) {
      arr[sz++] = r % b;
      r /= b;
    }
    reverse(arr, arr + sz);
    res += calc(0, 0, 1, 0);
    timer++;
    sz = 0;
    l--;
    while (l) {
      arr[sz++] = l % b;
      l /= b;
    }
    reverse(arr, arr + sz);
    res -= calc(0, 0, 1, 0);
    cout << res << nxtl;
  }
  return 0;
}
