#include <bits/stdc++.h>
using namespace std;
long long dp[11][70][1 << 11], l, r;
int tp[70], b, q, len;
long long dfs(int x, int sta, bool zero, bool lim) {
  if (!x) return !sta;
  if (!zero && !lim && ~dp[b][x][sta]) return dp[b][x][sta];
  int up = lim ? tp[x] : b - 1;
  long long res = 0;
  for (int i = 0; i <= up; i++)
    res += dfs(x - 1, (!i && zero) ? sta : sta ^ (1 << i), zero && !i,
               lim && (i == up));
  return !zero && !lim ? dp[b][x][sta] = res : res;
}
long long solve(long long x) {
  len = 0;
  while (x) tp[++len] = x % b, x /= b;
  return dfs(len, 0, 1, 1);
}
int main() {
  ios ::sync_with_stdio(0);
  memset(dp, -1, sizeof dp);
  cin >> q;
  while (q--) {
    cin >> b >> l >> r;
    cout << solve(r) - solve(l - 1) << endl;
  }
  return 0;
}
