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;
inline void read(int &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void read(long long &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void write(int x) {
  static const int maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
inline void write(long long x) {
  static const int maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
long long b, l, r;
long long P[20];
long long f[300][20][20][20];
long long C[300][300];
map<pair<int, long long>, long long> M;
long long get_C(int n, int m) {
  if (C[n][m] != -1) return C[n][m];
  long long ans = 1;
  for (int i = 1; i <= m; i++) ans = ans * (n - i + 1) / i;
  C[n][m] = ans;
  return ans;
}
long long get_num(int wei, int tot, int tmp, int cnt) {
  if (wei < tot) return 0;
  if (cnt > tmp) return 0;
  if ((wei == 0) && (tot == 0)) return get_C(tmp, cnt);
  if (f[wei][tot][tmp][cnt] != -1) return f[wei][tot][tmp][cnt];
  long long ans = 0;
  if (tot > 0) {
    for (int i = 1; i <= wei - tot + 1; i += 2)
      ans += get_C(wei, i) * get_num(wei - i, tot - 1, tmp, cnt);
  } else {
    for (int i = 2; i <= wei; i += 2)
      ans += get_C(wei, i) * get_num(wei - i, tot, tmp, cnt + 1);
  }
  f[wei][tot][tmp][cnt] = ans;
  return ans;
}
long long get_ans(int b, long long x) {
  if (M.count(make_pair(b, x))) return M[make_pair(b, x)];
  if (x == 0) return 1;
  long long tmp = 1;
  long long wei = 1;
  while ((unsigned long long)tmp * b <= (unsigned long long)x) {
    tmp = tmp * b;
    wei++;
  }
  if (wei % 2 == 1) return get_ans(b, tmp - 1);
  long long ans = get_ans(b, tmp - 1);
  if (x == 1000000000000000000) cout << ans << " " << tmp << endl;
  long long now = 0;
  long long sum = 0;
  bool op = 1;
  long long TMPX = x;
  while (wei) {
    if (sum > wei) break;
    for (int i = op; i < x / tmp; i++) {
      if (((1 << i) & now) == 0)
        sum++;
      else
        sum--;
      ans += get_num(wei - 1, sum, b - sum, 0);
      if (((1 << i) & now) == 0)
        sum--;
      else
        sum++;
    }
    if ((now & ((1 << (x / tmp)))) == 0)
      sum++;
    else
      sum--;
    now = now ^ ((1 << (x / tmp)));
    x = x % tmp;
    tmp = tmp / b;
    op = 0;
    wei--;
  }
  if (sum == 0) ans++;
  M[make_pair(b, TMPX)] = ans;
  return ans;
}
void doit() {
  read(b);
  read(l);
  read(r);
  long long tmp = get_ans(b, r) - get_ans(b, l - 1);
  write(tmp);
  puts("");
}
int main() {
  memset(f, -1, sizeof(f));
  memset(C, -1, sizeof(C));
  int T;
  read(T);
  for (int I = 1; I <= T; I++) doit();
  return 0;
}
