#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
int BASE;
long long f[11][64][1024];
void preprocess() {
  for (BASE = 2; BASE <= 10; ++BASE) {
    f[BASE][0][0] = 1;
    int it = 0;
    long long pow = 1;
    while (pow <= INF / BASE) {
      for (int mask = 0; mask < 1 << BASE; ++mask)
        for (int k = 0; k < BASE; ++k)
          f[BASE][it + 1][mask ^ 1 << k] += f[BASE][it][mask];
      ++it, pow *= BASE;
    }
  }
}
long long solve(long long x) {
  int it = 0, cur = 1, mask = 2;
  long long res = 0, pow = 1;
  while (pow <= x / (cur + 1)) {
    res += f[BASE][it][mask];
    if (cur == BASE - 1) {
      cur = 1, mask = 2;
      ++it, pow *= BASE;
    } else {
      mask ^= 1 << cur++;
      mask ^= 1 << cur;
    }
  }
  while (x -= cur * pow, --it, pow /= BASE) {
    cur = 0, mask ^= 1;
    while ((cur + 1) * pow <= x) {
      res += f[BASE][it][mask];
      mask ^= 1 << cur++;
      mask ^= 1 << cur;
    }
  }
  return (res += !mask);
}
int main() {
  preprocess();
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    long long L, R;
    scanf("%d%lld%lld", &BASE, &L, &R);
    printf("%lld\n", solve(R) - solve(L - 1));
  }
}
