#include <bits/stdc++.h>
using namespace std;
const int N = 1234;
long long dp[11][69][N];
int a[100];
void load() {}
long long solve(int Base, long long val) {
  int cnt = 0;
  a[0] = 0;
  while (val > 0) a[++cnt] = val % Base, val /= Base;
  reverse(a + 1, a + 1 + cnt);
  long long ans = 0;
  for (int i = 1; i < cnt; ++i)
    for (int c = 1; c < Base; ++c) ans += dp[Base][i - 1][(1 << c)];
  int Mask = 0;
  for (int i = 0; i < cnt; ++i) {
    if (i) Mask ^= (1 << a[i]);
    for (int c = (i == 0); c < a[i + 1]; ++c)
      ans += dp[Base][cnt - i - 1][Mask ^ (1 << c)];
  }
  if ((Mask ^ (1 << a[cnt])) == 0) ++ans;
  return ans;
}
void trungtt() {
  for (int Base = 2; Base <= 10; ++Base) {
    dp[Base][0][0] = 1;
    for (int i = 1; i <= 64; ++i)
      for (int Mask = 0; Mask < (1 << Base); ++Mask)
        if (dp[Base][i - 1][Mask]) {
          for (int c = 0; c < Base; ++c)
            dp[Base][i][Mask ^ (1 << c)] += dp[Base][i - 1][Mask];
        }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int b;
    long long l, r;
    scanf("%d%lld%lld", &b, &l, &r);
    printf("%lld\n", solve(b, r) - solve(b, l - 1));
  }
  return;
}
int main() {
  load();
  trungtt();
  return 0;
}
