#include <bits/stdc++.h>
using namespace std;
unsigned long long dp[11][1 << 11][105];
int tmp[81];
unsigned long long solve(unsigned long long base, unsigned long long val) {
  if (val <= 0LL) return 0;
  unsigned long long ans = 0, up = 1;
  for (int i = 1;; i++) {
    up = up * base;
    if (val >= up) {
      for (int x = 1; x < base; x++) {
        ans += dp[base][1 << x][i - 1];
      }
    } else {
      int v[100], n = 0;
      while (val) {
        v[i - n] = val % base;
        n++;
        val /= base;
      }
      int premask = 0;
      for (int j = 0; j <= i; j++) {
        if (j == i) {
          ans += !premask;
          return ans;
        }
        for (int x = ((j == 0) ? 1 : 0); x < v[j + 1]; x++) {
          ans += dp[base][premask ^ (1 << x)][i - j - 1];
        }
        premask ^= (1 << v[j + 1]);
      }
    }
  }
  return ans;
}
int main(void) {
  int q;
  scanf("%d", &q);
  for (int i = 2; i <= 10; i++) {
    dp[i][0][0] = 1;
    for (int j = 0; j < 100; j++) {
      for (int k = 0; k < (1 << i); k++) {
        if (dp[i][k][j] > 0) {
          for (int l = 0; l < i; l++) {
            dp[i][k ^ (1 << l)][j + 1] += dp[i][k][j];
          }
        }
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int b;
    unsigned long long l, r;
    scanf("%d%lld%lld", &b, &l, &r);
    printf("%lld\n", solve(b, r) - solve(b, l - 1LL));
  }
  return 0;
}
