#include <bits/stdc++.h>
using namespace std;
const long long int N = 70, M = 11, MA = 1030;
long long int dp[M][N][M][MA], dps[M][N][MA], dpt[M][N], n, q, b, l, r, x;
vector<long long int> v;
long long int get(long long int a, long long int b) {
  long long int x = a, ans, mask = 0, l;
  v.resize(0);
  while (x > 0) {
    v.push_back(x % b);
    x /= b;
  }
  l = (long long int)v.size();
  ans = dpt[b][l];
  reverse(v.begin(), v.end());
  for (auto y : v) {
    for (int j = b - 1; j > y; j--) ans -= dp[b][l][j][mask];
    mask ^= (1 << y);
    l--;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  for (long long int bi = 2; bi <= 10; bi++) {
    for (int l = 2; l <= 65; l++) {
      for (int c = 0; c < bi; c++) {
        dp[bi][1][c][(1ll << c)] = 1;
        for (int mask = 0; mask < (1ll << bi); mask++)
          dps[bi][1][mask] += dp[bi][1][c][mask];
      }
      dpt[bi][1] = 0;
    }
  }
  for (long long int bi = 2; bi <= 10; bi++) {
    for (int l = 2; l <= 65; l++) {
      dpt[bi][l] = dpt[bi][l - 1];
      for (int c = 0; c < bi; c++) {
        for (int i = 0; i < bi; i++)
          for (int mask = 0; mask < (1ll << bi); mask++) {
            x = mask, x ^= (1ll << c);
            dp[bi][l][c][mask] += dp[bi][l - 1][i][x];
          }
        for (int mask = 0; mask < (1ll << bi); mask++)
          dps[bi][l][mask] += dp[bi][l][c][mask];
        if (c != 0) dpt[bi][l] += dp[bi][l][c][0];
      }
    }
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> b >> l >> r;
    l--;
    cout << get(r, b) - get(l, b) << '\n';
  }
}
