10
4 108 114
5 30 155
8 193 197
9 71 169
2 163 166
8 120 144
8 22 151
4 21 166
2 46 127
8 38 51
 20
9 142 172
7 132 256
8 245 315
9 496 496
3 345 362
8 13 162
5 342 470
9 16 488
9 467 482
4 471 478
10 92 224
6 228 261
9 54 167
2 402 409
5 9 10
7 225 360
3 438 483
6 252 342
3 491 493
6 296 367
 1
8 1234567890123 123456789012345

0
3
0
1
3
0
5
16
9
1
 0
0
0
0
4
6
13
7
0
0
1
2
3
0
0
2
12
9
1
4
 774752389568

#include <bits/stdc++.h>
using namespace std;
int a[500000];
long long minC = LONG_LONG_MAX;
long long ipow(long long base, long long exp) {
  if (exp == 0) return 1;
  if (exp == 1) return base;
  long long res = ipow(base, exp / 2);
  res *= res;
  if (exp % 2) res *= base;
  return res;
}
class Tuple {
 public:
  long long base, maxim, ndigits;
  int mask;
  bool leading;
};
long long DP[11][100][11] = {0};
long long nMagic2(long long base, long long n) {
  if (n == 0) return 0;
  int digits[100];
  int ndig = 0;
  long long n2 = n;
  int z = 0;
  while (n2 != 0) {
    digits[z++] = n2 % base;
    n2 /= base, ndig++;
  }
  long long res = 0;
  int mask = 0;
  int n1 = 0;
  for (int nd = ndig - 1; nd >= 0; nd--) {
    mask ^= 1 << digits[nd];
    if ((mask & (1 << digits[nd])) != 0)
      n1++;
    else
      n1--;
    if (nd == 0 && n1)
      res += 0;
    else if (nd > 0) {
      res += (digits[nd]) * DP[base][nd][n1];
    }
  }
  n1 = ndig % 2;
  for (int nd = ndig - 1; nd >= 0; nd--) {
    n1 = !n1;
    if (n1)
      res -= DP[base][nd][n1];
    else
      res += DP[base][nd][n1];
  }
  res--;
  return res;
}
int main() {
  for (int b = 2; b <= 10; b++) {
    DP[b][0][0] = 1;
    for (int n1 = 1; n1 <= b; n1++) DP[b][0][n1] = 0;
    for (int nd = 1; nd <= 65; nd++)
      for (int n1 = 0; n1 <= b; n1++) {
        DP[b][nd][n1] = 0;
        if (n1 > 0) DP[b][nd][n1] += n1 * DP[b][nd - 1][n1 - 1];
        DP[b][nd][n1] += (b - n1) * DP[b][nd - 1][n1 + 1];
      }
  }
  cout << nMagic2(2, 0) << endl;
  int q;
  std::ios_base::sync_with_stdio(false);
  cin >> q;
  for (int i = 0; i < q; i++) {
    long long b, l, r;
    cin >> b >> l >> r;
    cout << nMagic2(b, r) - nMagic2(b, l - 1) << "\n";
  }
}
