#include <bits/stdc++.h>
using namespace std;
long long dp[12][120][(1 << 11)];
int a[120];
int b;
int check(int sta) {
  int f = 0;
  for (int i = 1; i <= b; ++i) {
    if (sta & 1) f++;
    sta >>= 1;
  }
  return f;
}
long long dfs(int pos, int sta, bool zero, bool limit) {
  if (pos == 0) {
    return !sta;
  }
  if (!limit && !zero && dp[b][pos][sta] != -1) return dp[b][pos][sta];
  int up = limit ? a[pos] : b - 1;
  long long tmp = 0;
  for (int i = 0; i <= up; i++) {
    if (zero && i == 0) {
      if (check(sta) > pos - 1) continue;
      tmp += dfs(pos - 1, sta, 1, limit && i == a[pos]);
    } else {
      if (check(sta ^ (1 << i)) > pos - 1) continue;
      tmp += dfs(pos - 1, sta ^ (1 << i), 0, limit && i == a[pos]);
    }
  }
  if (!limit && !zero) dp[b][pos][sta] = tmp;
  return tmp;
}
long long solve(long long k) {
  int pos = 0;
  while (k > 0) {
    a[++pos] = k % b;
    k /= b;
  }
  return dfs(pos, 0, true, true);
}
int main(void) {
  long long le, ri;
  memset(dp, -1, sizeof dp);
  int n;
  scanf("%d", &n);
  while (n--) {
    scanf("%d%I64d%I64d", &b, &le, &ri);
    printf("%I64d\n", solve(ri) - solve(le - 1));
  }
  return 0;
}
