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;
long long dp[11][63][2048], ans[11][63], p[11][63];
void preprocess() {
  for (long long base = 2; base < 11; ++base) {
    p[base][0] = 1;
    for (int i = 1; i < 63; ++i) {
      p[base][i] = (p[base][i - 1] * base);
      if (p[base][i] >= 1e18) break;
    }
  }
  for (int base = 2; base < 11; ++base) {
    for (int i = 1; i < 63; ++i) {
      for (int mask = 0; mask < 2048; ++mask) {
        dp[base][i][mask] = 0;
      }
    }
    ans[base][0] = 0;
  }
  for (int base = 2; base < 11; ++base) {
    dp[base][0][0] = 1;
    long long sum = 0;
    for (int i = 1; i < 63; ++i) {
      for (int mask = 0; mask < 2048; ++mask) {
        for (int dig = 0; dig < base; ++dig) {
          dp[base][i][mask] += dp[base][i - 1][mask ^ p[2][dig]];
        }
      }
      ans[base][i] = ans[base][i - 1];
      for (int dig = 1; dig < base; ++dig) {
        ans[base][i] += dp[base][i][p[2][dig]];
      }
    }
  }
}
long long find_num(long long n, long long base) {
  int ln = (0.5 + (double)(log((double)n) / log((double)base)));
  int mask = 0;
  long long ret = 0;
  for (int i = ln; i >= 0; --i) {
    int dig = (n / p[base][i]) % base;
    int low_lim = (i != ln) ? 0 : 1;
    for (int d = dig - 1; d >= low_lim; --d) {
      ret += dp[base][i][mask ^ p[2][d]];
    }
    mask ^= p[2][dig];
  }
  if (mask == 0) ret++;
  ret += (ln > 0) ? ans[base][ln - 1] : 0;
  return ret;
}
int main() {
  preprocess();
  int q;
  scanf("%d", &q);
  while (q--) {
    long long b, l, r;
    scanf("%lld %lld %lld", &b, &l, &r);
    long long num0 = find_num(l, b), num1 = find_num(r, b);
    int mask = 0;
    while (l > 0) {
      mask ^= p[2][l % b];
      l /= b;
    }
    printf("%lld\n", num1 - num0 + (mask == 0));
  }
}
