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;
template <class T>
inline void gmin(T &x, const T &y) {
  if (x > y) x = y;
}
template <class T>
inline void gmax(T &x, const T &y) {
  if (x < y) x = y;
}
const int BufferSize = 1 << 16;
char buffer[BufferSize], *Bufferhead, *Buffertail;
bool Terminal;
inline char Getchar() {
  if (Bufferhead == Buffertail) {
    int l = fread(buffer, 1, BufferSize, stdin);
    if (!l) {
      Terminal = 1;
      return 0;
    }
    Buffertail = (Bufferhead = buffer) + l;
  }
  return *Bufferhead++;
}
template <class T>
inline bool read(T &x) {
  x = 0;
  char c = Getchar(), rev = 0;
  while (c < '0' || c > '9') {
    c = Getchar();
    rev |= c == '-';
    if (Terminal) return 0;
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = Getchar();
  if (c == '.') {
    c = Getchar();
    double t = 0.1;
    while (c >= '0' && c <= '9')
      x = x + (c - '0') * t, c = Getchar(), t = t / 10;
  }
  x = rev ? -x : x;
  return 1;
}
template <class T1, class T2>
inline bool read(T1 &x, T2 &y) {
  return read(x) & read(y);
}
template <class T1, class T2, class T3>
inline bool read(T1 &x, T2 &y, T3 &z) {
  return read(x) & read(y) & read(z);
}
template <class T1, class T2, class T3, class T4>
inline bool read(T1 &x, T2 &y, T3 &z, T4 &w) {
  return read(x) & read(y) & read(z) & read(w);
}
inline bool reads(char *x) {
  char c = Getchar();
  while (c < 33 || c > 126) {
    c = Getchar();
    if (Terminal) return 0;
  }
  while (c >= 33 && c <= 126) (*x++) = c, c = Getchar();
  *x = 0;
  return 1;
}
template <class T>
inline void print(T x, const char c = '\n') {
  if (!x) {
    putchar('0');
    putchar(c);
    return;
  }
  if (x < 0) putchar('-'), x = -x;
  int m = 0, a[20];
  while (x) a[m++] = x % 10, x /= 10;
  while (m--) putchar(a[m] + '0');
  putchar(c);
}
const int inf = 0x3f3f3f3f;
const int N = 100005, M = 100005, mod = 1e9 + 7;
template <class T, class S>
inline void ch(T &x, const S y) {
  x = (x + y) % mod;
}
inline int exp(int x, int y, const int mod = ::mod) {
  int ans = 1;
  while (y) {
    if (y & 1) ans = (long long)ans * x % mod;
    x = (long long)x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long dp[105][12][12], f[12][105];
inline long long solve(int x, int a, int b) {
  if (!x) {
    if (!b) return 1;
    return 0;
  }
  if (~dp[x][a][b]) return dp[x][a][b];
  dp[x][a][b] = 0;
  if (a) dp[x][a][b] += solve(x - 1, a - 1, b + 1) * a;
  if (b) dp[x][a][b] += solve(x - 1, a + 1, b - 1) * b;
  return dp[x][a][b];
}
inline long long calc(int b, int x) { return solve(x - 1, b - 1, 1) * (b - 1); }
inline long long get(int b, long long n) {
  if (!n) return 0;
  int m = 0, a[105];
  while (n) a[++m] = n % b, n /= b;
  reverse(a + 1, a + m + 1);
  long long ans = 0;
  for (int i = 2; i < m; i += 2) ans += calc(b, i);
  if (m % 2 == 0) {
    int x = b, y = 0;
    bool vis[12];
    memset(vis, 0, sizeof(vis));
    ans += (a[1] - 1) * solve(m - 1, b - 1, 1);
    for (int i = 2; i <= m; i++) {
      if (vis[a[i - 1]])
        y--, x++;
      else
        x--, y++;
      vis[a[i - 1]] ^= 1;
      for (int j = 0; j < a[i]; j++)
        if (vis[j])
          ans += solve(m - i, x + 1, y - 1);
        else
          ans += solve(m - i, x - 1, y + 1);
    }
    if (!y) ans++;
  }
  return ans;
}
int main() {
  int Q;
  read(Q);
  memset(dp, -1, sizeof(dp));
  memset(f, -1, sizeof(f));
  while (Q--) {
    int b;
    long long l, r;
    read(b, l, r);
    print(get(b, r) - get(b, l - 1));
  }
  return 0;
}
