#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
long long ans[N];
int a[66];
long long dp[66][1050];
long long dfs(int pos, int state, bool lead, bool limit, int k) {
  if (pos == -1) return state == 0 && !lead;
  if (!limit && !lead && dp[pos][state] != -1) return dp[pos][state];
  int up = limit ? a[pos] : k - 1;
  long long ans = 0;
  for (int i = 0; i <= up; i++) {
    ans += dfs(pos - 1, lead && i == 0 ? state : state ^ (1 << i),
               lead && i == 0, limit && i == a[pos], k);
  }
  if (!limit && !lead) dp[pos][state] = ans;
  return ans;
}
long long solve(int k, long long x) {
  int pos = 0;
  while (x) {
    a[pos++] = x % k;
    x /= k;
  }
  return dfs(pos - 1, 0, true, true, k);
}
struct Node {
  long long le, ri;
  int id;
  Node() {}
  Node(long long L, long long R, int ID) {
    id = ID;
    le = L;
    ri = R;
  }
};
vector<Node> b[15];
int main() {
  int T;
  cin >> T;
  int id = 0;
  while (T--) {
    id++;
    long long le, ri;
    int k;
    scanf("%d%lld%lld", &k, &le, &ri);
    b[k].push_back(Node(le, ri, id));
  }
  for (int k = 2; k <= 10; k++) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < (int)b[k].size(); i++) {
      ans[b[k][i].id] = solve(k, b[k][i].ri) - solve(k, b[k][i].le - 1);
    }
  }
  for (int i = 1; i <= id; i++) {
    printf("%lld\n", ans[i]);
  }
}
