#include <bits/stdc++.h>
using namespace std;
const int N = 70;
int T, b, cnt;
int a[N];
long long L, R;
long long dp[11][N][(1 << 11) - 1];
long long dfs(int cur, int sta, bool f, bool g) {
  if (!cur) return !sta;
  if (!f && !g && dp[b][cur][sta] != -1) return dp[b][cur][sta];
  int v = b - 1;
  if (f) v = a[cur];
  long long ans = 0;
  for (int i = 0; i <= v; i++) {
    if (g && i == 0)
      ans += dfs(cur - 1, sta, f && (i == v), 1);
    else
      ans += dfs(cur - 1, sta ^ (1 << i), f && (i == v), 0);
  }
  if (!f && !g) dp[b][cur][sta] = ans;
  return ans;
}
long long solve(long long x) {
  cnt = 0;
  while (x) {
    a[++cnt] = x % b;
    x /= b;
  }
  return dfs(cnt, 0, 1, 1);
}
int main() {
  cin >> T;
  memset(dp, -1, sizeof dp);
  while (T--) {
    cin >> b >> L >> R;
    cout << solve(R) - solve(L - 1) << endl;
  }
}
