#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
template <class T>
inline bool read(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
int b, a[70];
long long l, r, dp[70][15][2500], ans[MAXN];
vector<pair<pair<long long, long long>, int> > v[15];
long long dfs(int pos, int kk, int sta, bool lead, bool limit) {
  if (pos == -1) {
    if (sta) return 0;
    return 1;
  }
  if (!limit && !lead && dp[pos][kk][sta] != -1) return dp[pos][kk][sta];
  int up = limit ? a[pos] : (b - 1);
  long long tmp = 0;
  for (int i = 0; i <= up; ++i) {
    if (lead && i == 0)
      tmp += dfs(pos - 1, i, sta, true, limit && a[pos] == i);
    else
      tmp += dfs(pos - 1, i, sta ^ (1 << i), false, limit && a[pos] == i);
  }
  if (!limit && !lead) dp[pos][kk][sta] = tmp;
  return tmp;
}
long long solve(long long x) {
  int pos = 0;
  while (x) {
    a[pos++] = x % (long long)b;
    x /= (long long)b;
  }
  return dfs(pos - 1, 0, 0, true, true);
}
int main() {
  int q;
  read(q);
  for (int i = 1; i <= q; ++i) {
    read(b), read(l), read(r);
    v[b].push_back(make_pair(make_pair(l, r), i));
  }
  for (int i = 2; i <= 10; ++i) {
    if (v[i].empty()) continue;
    b = i;
    memset(dp, -1, sizeof(dp));
    for (int j = 0; j < v[i].size(); ++j) {
      ans[v[i][j].second] =
          solve(v[i][j].first.second) - solve(v[i][j].first.first - 1);
    }
  }
  for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
  return 0;
}
