#include <bits/stdc++.h>
using namespace std;
long long q, dp[11][65][1 << 10][2];
long long base, dig[65];
long long dpp(long long pos, long long mask, bool sta, bool ok) {
  if (pos == 0) {
    if (ok == 0)
      return (mask == 0);
    else
      return 0;
  }
  if (!sta && dp[base][pos][mask][ok] != -1) return dp[base][pos][mask][ok];
  long long high = sta ? dig[pos] + 1 : base, ans = 0;
  for (long long i = 0; i < high; ++i) {
    ans += dpp(pos - 1, ((i == 0 && ok) ? mask : mask ^ (1 << i)),
               ((sta && i == high - 1) ? 1 : 0), ((ok && i == 0)) ? 1 : 0);
  }
  if (!sta) dp[base][pos][mask][ok] = ans;
  return ans;
}
long long calc(long long num) {
  long long cnt = 0;
  while (num) {
    dig[++cnt] = num % base;
    num /= base;
  }
  return dpp(cnt, 0, 1, 1);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  memset(dp, -1, sizeof dp);
  while (q--) {
    long long l, r;
    cin >> base >> l >> r;
    cout << calc(r) - calc(l - 1) << "\n";
  }
  return 0;
}
