#include <bits/stdc++.h>
using namespace std;
const long long N = 100;
long long q, b;
long long l, r;
long long a[N];
long long f[15][N][10005];
long long dfs(long long pos, long long num, long long limit, long long lead) {
  if (!pos) return num == 0;
  if (!lead && !limit && f[b][pos][num] > -1) return f[b][pos][num];
  long long r = limit ? a[pos] : b - 1, ret = 0;
  for (long long i = 0; i <= r; i++) {
    ret += dfs(pos - 1, (lead && i == 0) ? num : num ^ (1 << i),
               limit && i == a[pos], lead && i == 0);
  }
  if (!lead && !limit) f[b][pos][num] = ret;
  return ret;
}
long long solve(long long x) {
  long long cnt = 0;
  while (x) {
    a[++cnt] = x % b;
    x /= b;
  }
  return dfs(cnt, 0, true, true);
}
signed main() {
  scanf("%lld", &q);
  memset(f, -1, sizeof f);
  while (q--) {
    scanf("%lld%lld%lld", &b, &l, &r);
    printf("%lld\n", solve(r) - solve(l - 1));
  }
  return 0;
}
