#include <bits/stdc++.h>
using namespace std;
long long int dp[12][65][2070];
long long int func(long long int n, long long int b) {
  long long int d = 0;
  long long int xx = 1;
  if (n == 0) {
    return 0;
  }
  long long int power[70] = {0};
  power[0] = 1;
  while (xx <= n) {
    if (xx == 1000000000000000000) {
      d++;
      break;
    }
    xx *= b;
    d++;
  }
  for (long long int i = 1; i <= d - 1; i++) {
    power[i] = power[i - 1];
    long long int zz = b;
    power[i] *= zz;
  }
  long long int ans = 0;
  long long int noo = n;
  for (long long int i = d; i >= d; i--) {
    long long int curr = 0;
    long long int s = noo;
    for (long long int j = i; j >= 1; j--) {
      long long int dig = s / power[j - 1];
      for (long long int l = 0; l < dig; l++) {
        if (l == 0) {
          if (j == i) {
            continue;
          }
        }
        curr ^= (1 << l);
        ans += dp[b][j - 1][curr];
        curr ^= (1 << l);
      }
      s -= dig * power[j - 1];
      curr ^= (1 << dig);
    }
    if (curr == 0) {
      ans++;
    }
    noo -= power[i - 1] * (noo / power[i - 1]);
  }
  for (long long int i = d - 1; i >= 1; i--) {
    for (long long int l = 1; l <= b - 1; l++) {
      ans += dp[b][i - 1][1 << l];
    }
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int q;
  cin >> q;
  for (long long int b = 2; b <= 10; b++) {
    dp[b][0][0] = 1;
    for (long long int j = 0; j <= 59; j++) {
      for (long long int k = 0; k < (1 << b); k++) {
        for (long long int l = 0; l < b; l++) {
          dp[b][j + 1][k ^ (1 << l)] += dp[b][j][k];
        }
      }
    }
  }
  while (q--) {
    long long int b, l, r;
    cin >> b >> l >> r;
    cout << func(r, b) - func(l - 1, b) << "\n";
  }
}
