#include <bits/stdc++.h>
using namespace std;
const int N = 64, R = 10;
long long dp[R + 1][N + 1][1 << R], pre[R + 1][N + 1];
void build() {
  for (int r = 2; r <= R; r++) {
    pre[r][0] = 1;
    dp[r][0][0] = 1;
    for (int n = 0; n < N; n++) {
      pre[r][n + 1] += pre[r][n];
      for (int mask = 0; mask < 1 << r; mask++) {
        for (int d = 0; d < r; d++) {
          int b = 1 << d;
          dp[r][n + 1][mask ^ b] += dp[r][n][mask];
          if (d > 0 && (mask ^ b) == 0) {
            pre[r][n + 1] += dp[r][n][mask];
          }
        }
      }
    }
  }
}
long long solve(long long x, int r, bool inclusive) {
  vector<int> a;
  while (x > 0) {
    a.push_back(x % r);
    x /= r;
  }
  int n = a.size();
  long long ans = pre[r][n - 1];
  int mask = 0;
  for (int i = n; i--;) {
    for (int d = i == n - 1; d < a[i]; d++) {
      int b = 1 << d;
      ans += dp[r][i][mask ^ b];
    }
    mask ^= 1 << a[i];
  }
  if (inclusive && mask == 0) ans++;
  return ans;
}
int main() {
  build();
  int q;
  cin >> q;
  while (q--) {
    int b;
    long long l, r;
    cin >> b >> l >> r;
    cout << solve(r, b, true) - solve(l, b, false) << '\n';
  }
}
