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>
#pragma GCC optimize("Ofast")
using namespace std;
long long dp[11][20][10][4100];
inline long long changed(long long x, long long i) {
  x ^= (1LL << i);
  return x;
}
inline void count_dp() {
  for (long long b = 2; b <= 10; ++b) {
    for (long long fir = 0; fir < b; ++fir) {
      dp[b][1][fir][changed(0, fir)] = 1;
    }
  }
  for (long long b = 2; b <= 10; ++b) {
    for (long long len = 1; len <= 18; ++len) {
      for (long long fir = 0; fir < b; ++fir) {
        for (long long mask = 0; mask < 2 * (1 << b); ++mask) {
          if (dp[b][len][fir][mask] == 0) continue;
          for (long long new_fir = 0; new_fir < b; ++new_fir) {
            dp[b][len + 1][new_fir][changed(mask, new_fir)] +=
                dp[b][len][fir][mask];
          }
        }
      }
    }
  }
}
inline vector<long long> to(long long x, long long b) {
  if (x == 0) {
    return {0};
  }
  vector<long long> res;
  while (x > 0) {
    res.push_back(x % b);
    x /= b;
  }
  reverse(res.begin(), res.end());
  return res;
}
inline bool check(vector<long long> a, long long b) {
  vector<long long> cnt(10);
  for (long long i = 0; i < (long long)a.size(); ++i) {
    ++cnt[a[i]];
  }
  for (long long i = 0; i < b; ++i) {
    if (cnt[i] & 1) return false;
  }
  return true;
}
inline long long get(long long r, long long b) {
  if (r < 0) {
    return 0;
  }
  vector<long long> a = to(r, b);
  long long len = (long long)a.size();
  long long res = check(a, b);
  long long mask = 0;
  if (r == 9) {
  }
  for (long long Len = 1; Len < len; ++Len) {
    for (long long fir = 1; fir < b; ++fir) {
      res += dp[b][Len][fir][0];
      if (r == 9) {
      }
    }
  }
  for (long long pos = 0; pos < len; ++pos) {
    long long ch = 0;
    long long kek = 0;
    if (pos == 0) {
      kek = 1;
    }
    for (long long cur = kek; cur < a[pos]; ++cur) {
      res += dp[b][len - pos][cur][mask];
      ch += dp[b][len - pos][cur][mask];
    }
    if (r == 9) {
    }
    mask = changed(mask, a[pos]);
  }
  return res;
}
signed main() {
  long long q;
  scanf("%lld", &q);
  count_dp();
  for (long long it = 0; it < q; ++it) {
    long long b, l, r;
    scanf("%lld %lld %lld", &b, &l, &r);
    printf("%lld\n", get(r, b) - get(l - 1, b));
  }
  return 0;
}
