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 = 100;
const int Pt = 61;
long long A[MAXN + 5][MAXN + 5];
long long Odd[MAXN + 5][MAXN + 5], Even[MAXN + 5][MAXN + 5];
int s[MAXN + 5];
int vis[MAXN + 5];
void D_solve() {
  A[1][1] = 1;
  A[1][2] = 1;
  for (int i = 2; i <= 61; i++) {
    for (int j = 1; j <= i + 1; j++) A[i][j] = A[i - 1][j] + A[i - 1][j - 1];
  }
  for (int i = 1; i <= Pt; i += 2) Odd[i][1] = 1;
  Odd[0][0] = 1;
  for (int i = 2; i <= 10; i++) {
    for (int L = 1; L <= Pt; L++) {
      for (int j = 1; j <= L; j += 2)
        Odd[L][i] += Odd[L - j][i - 1] * A[L][j + 1];
    }
  }
  for (int i = 2; i <= Pt; i += 2) Even[i][1] = 1;
  Even[0][0] = 1;
  for (int i = 2; i <= 10; i++) {
    for (int L = 1; L <= Pt; L++) {
      for (int j = 2; j <= L; j += 2)
        Even[L][i] += Even[L - j][i - 1] * A[L][j + 1];
    }
  }
}
int N;
long long b, L, R;
long long dfs(int d, int cnt, bool check, bool flag) {
  long long sum = 0;
  if (d == 0) {
    if (cnt == 0 && check) return 1;
    return 0;
  }
  if (check) {
    for (int i = 0; i <= s[d] - 1; i++) {
      int ans = cnt;
      vis[i]++;
      if (vis[i] & 1)
        ans++;
      else
        ans--;
      int a = b - ans;
      for (int L = ans; L <= d - 1; L++) {
        for (int t = 0; t <= a; t++) {
          sum +=
              Odd[L][ans] * Even[d - 1 - L][t] * A[a][t + 1] * A[d - 1][L + 1];
        }
      }
      vis[i]--;
    }
    vis[s[d]]++;
    if (vis[s[d]] & 1)
      cnt++;
    else
      cnt--;
    sum += dfs(d - 1, cnt, true, false);
  } else {
    if (flag) {
      for (int i = 1; i <= s[d] - 1; i++) {
        int a = 1;
        for (int L = a; L <= d - 1; L++) {
          for (int t = 0; t <= b - a; t++) {
            sum += Odd[L][a] * Even[d - 1 - L][t] * A[b - a][t + 1] *
                   A[d - 1][L + 1];
          }
        }
      }
      vis[s[d]]++;
      if (vis[s[d]] & 1)
        cnt++;
      else
        cnt--;
      sum += dfs(d - 1, cnt, true, false);
      sum += dfs(d - 1, 0, false, false);
    } else {
      for (int i = 1; i <= b - 1; i++) {
        int a = 1;
        for (int L = a; L <= d - 1; L++) {
          for (int t = 0; t <= b - a; t++) {
            sum += Odd[L][a] * Even[d - 1 - L][t] * A[b - a][t + 1] *
                   A[d - 1][L + 1];
          }
        }
      }
      sum += dfs(d - 1, 0, false, false);
    }
  }
  return sum;
}
long long solve(long long a, long long x) {
  memset(vis, 0, sizeof(vis));
  int L = 0;
  while (x > 0) s[++L] = x % a, x /= a;
  return dfs(L, 0, false, true);
}
int main() {
  D_solve();
  int cnt = 0;
  scanf("%d", &N);
  while (N--) {
    scanf("%I64d %I64d %I64d", &b, &L, &R);
    long long suma = solve(b, L - 1), sumb = solve(b, R);
    printf("%I64d\n", sumb - suma);
  }
}
