#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx,tune=native")
using namespace std;
vector<int> NUM;
long long int m1[11][64][1 << 10];
long long int cnt(int base, int len, int mask) {
  if (len == 0) return mask == 0 ? 1 : 0;
  long long int &ans = m1[base][len][mask];
  if (ans != -1) return ans;
  ans = 0;
  for (int d = 0; d < (base); d++) ans += cnt(base, len - 1, mask ^ (1 << d));
  return ans;
}
long long int f(long long int x, int base) {
  if (x == 0) return 0;
  NUM.clear();
  while (x) NUM.push_back(x % base), x /= base;
  reverse((NUM).begin(), (NUM).end());
  long long int ans = 0;
  int n = ((int)(NUM).size());
  for (int len = 1; len < n; len++)
    for (int d = 1; d < base; d++) ans += cnt(base, len - 1, 1 << d);
  int mask = 0;
  for (int i = 0; i < n; i++) {
    for (int d = i ? 0 : 1; d < NUM[i]; d++)
      ans += cnt(base, n - i - 1, mask ^ (1 << d));
    mask ^= (1 << NUM[i]);
  }
  ans += cnt(base, 0, mask);
  return ans;
}
int main() {
  memset(m1, -1, sizeof(m1));
  int q;
  scanf("%d", &q);
  while (q--) {
    int base;
    long long int a, b;
    scanf("%d %lld %lld", &base, &a, &b);
    printf("%lld\n", f(b, base) - f(a - 1, base));
  }
  return 0;
}
