#include <bits/stdc++.h>
using namespace std;
int base;
long long dp[11][70][2000][2];
int digit[70];
long long dfs(int pos, int sta, bool limit, int lead) {
  if (pos == -1) {
    if (!sta)
      return 1;
    else
      return 0;
  }
  if (!limit && dp[base][pos][sta][lead] != -1) {
    return dp[base][pos][sta][lead];
  }
  long long res = 0;
  int up = limit ? digit[pos] : base - 1;
  for (int i = 0; i <= up; i++) {
    res += dfs(pos - 1, (lead && !i) ? 0 : sta ^ (1 << i), limit && i == up,
               lead && !i);
  }
  if (!limit) dp[base][pos][sta][lead] = res;
  return res;
}
long long solve(long long n) {
  int p = 0;
  while (n) {
    digit[p++] = n % base;
    n /= base;
  }
  return dfs(p - 1, 0, 1, 1);
}
int main() {
  memset(dp, -1, sizeof(dp));
  int q;
  scanf("%d", &q);
  while (q--) {
    long long l, r;
    scanf("%d%I64d%I64d", &base, &l, &r);
    printf("%I64d\n", solve(r) - solve(l - 1));
  }
}
