#include <bits/stdc++.h>
using namespace std;
long long mem[11][61][1 << 10][2];
long long sum_UR(int b, int n_digits, int parities, bool allow_leading_zero) {
  if (mem[b][n_digits][parities][allow_leading_zero] == -1) {
    long long ans;
    if (n_digits == 0) {
      ans = allow_leading_zero && parities == 0;
    } else {
      ans = 0;
      if (!allow_leading_zero) {
        ans += sum_UR(b, n_digits - 1, parities, false);
      }
      for (int i = allow_leading_zero ? 0 : 1; i < b; i++) {
        ans += sum_UR(b, n_digits - 1, parities ^ (1 << i), true);
      }
    }
    mem[b][n_digits][parities][allow_leading_zero] = ans;
  }
  return mem[b][n_digits][parities][allow_leading_zero];
}
int count_digits(int b, long long R) {
  int ans = 0;
  while (R > 0) {
    R /= b;
    ans++;
  }
  return ans;
}
int get_digit(int b, long long R, int C) {
  for (int i = 1; i < C; i++) {
    R /= b;
  }
  return int(R % b);
}
long long sum_R(int b, long long R, int C, int parities,
                bool allow_leading_zero) {
  if (C == 0) {
    return 0;
  } else {
    int dig = get_digit(b, R, C);
    long long ans = 0;
    if (!allow_leading_zero) {
      ans += sum_UR(b, C - 1, parities, false);
    }
    for (int i = allow_leading_zero ? 0 : 1; i < dig; i++) {
      ans += sum_UR(b, C - 1, parities ^ (1 << i), true);
    }
    ans += sum_R(b, R, C - 1, parities ^ (1 << dig), true);
    return ans;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(mem, -1, sizeof mem);
  int q;
  cin >> q;
  while (q--) {
    int b;
    long long L, R;
    cin >> b >> L >> R;
    cout << sum_R(b, R + 1, count_digits(b, R + 1), 0, false) -
                sum_R(b, L, count_digits(b, L), 0, false)
         << '\n';
  }
  return 0;
}
