#include <bits/stdc++.h>
using namespace std;
long long t, b, l, r;
long long a[60];
long long num[10];
long long dp[11][70][1024];
inline long long get(long long st, long long sh) {
  memset(num, 0, sizeof(num));
  long long cnt = 9, tot = 0;
  while (cnt) {
    num[cnt--] = st % 2;
    st /= 2;
  }
  num[9 - sh]++;
  for (int i = 0; i <= 9; i++) {
    if (num[i] == 2) num[i] = 0;
    tot = tot * 2 + num[i];
  }
  return tot;
}
long long dfs(long long now, long long st, bool limit, bool lead) {
  if (!now) return (!st);
  if (!lead && !limit && dp[b][now][st] != -1) return dp[b][now][st];
  long long up = limit ? a[now] : b - 1;
  long long sum = 0;
  for (int i = 0; i <= up; i++) {
    int new_st = ((i == 0 && lead) ? st : st ^ (1 << i));
    sum += dfs(now - 1, new_st, limit && i == a[now], lead && i == 0);
  }
  if (!lead && !limit) dp[b][now][st] = sum;
  return sum;
}
inline long long solve(long long num) {
  long long pos = 0;
  while (num) {
    a[++pos] = num % b;
    num /= b;
  }
  return dfs(pos, 0, 1, 1);
}
int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld%lld%lld", &b, &l, &r);
    printf("%lld\n", solve(r) - solve(l - 1));
  }
}
