#include <bits/stdc++.h>
using namespace std;
int q, b;
long long l, r;
int a[65];
long long f[11][65][(1 << 11) - 1][2];
long long dfs(int pos, int sta, int limit, int pre0) {
  if (pos == 0) return !sta;
  if (!limit && f[b][pos][sta][pre0] != -1) return f[b][pos][sta][pre0];
  int v = limit ? a[pos] : b - 1;
  long long res = 0;
  for (int i = 0; i <= v; i++) {
    if (pre0 && i == 0)
      res += dfs(pos - 1, sta, limit && (i == v), 1);
    else
      res += dfs(pos - 1, sta ^ (1 << i), limit && (i == v), 0);
  }
  return limit ? res : f[b][pos][sta][pre0] = res;
}
long long solve(long long x) {
  int k = 0;
  while (x) {
    a[++k] = x % b;
    x /= b;
  }
  return dfs(k, 0, 1, 1);
}
int main() {
  scanf("%d", &q);
  memset(f, -1, sizeof f);
  while (q--) {
    scanf("%d%lld%lld", &b, &l, &r);
    printf("%lld\n", solve(r) - solve(l - 1));
  }
}
