#include <bits/stdc++.h>
using namespace std;
long long f[20][75][2205], l[100], r[100];
long long b, q, s1, s2, ll = 64, ans;
long long dfs(long long tp, long long k, long long s, long long l,
              long long c) {
  if (!k) return !s;
  if (f[tp][k][s] != -1 && l != 1 && c != 1) return f[tp][k][s];
  long long st, sum = 0;
  if (l)
    st = r[k];
  else
    st = b - 1;
  for (long long i = st; i >= 0; i--) {
    if (c && (!i)) {
      sum += dfs(tp, k - 1, s, (i == r[k]) & l, 1);
      continue;
    }
    sum += dfs(tp, k - 1, s ^ (1 << i), (i == r[k]) & l, 0);
  }
  if (l | c) return sum;
  return f[tp][k][s] = sum;
}
signed main() {
  cin >> q;
  memset(f, -1, sizeof(f));
  while (q--) {
    scanf("%lld%lld%lld", &b, &s1, &s2);
    s1--;
    for (long long i = 1; i <= ll; i++) {
      l[i] = s1 % b, s1 /= b;
      r[i] = s2 % b, s2 /= b;
    }
    ans = dfs(b, 64, 0, 1, 1);
    for (long long i = 1; i <= ll; i++) r[i] = l[i];
    printf("%lld\n", ans - dfs(b, 64, 0, 1, 1));
  }
  return 0;
}
