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;
long long bin[20][20];
long long dp[11][20][11];
long long solve(const int b, long long val) {
  vector<int> s;
  while (val) {
    s.push_back(val % b);
    val /= b;
  }
  reverse((s).begin(), (s).end());
  long long ans = 0;
  for (int f = 1; f < (int)(s).size(); ++f) {
    for (int d = 1; d < b; ++d) {
      ans += dp[b][(int)(s).size() - f - 1][1];
    }
  }
  for (int d = 1; d < s[0]; ++d) {
    ans += dp[b][(int)(s).size() - 1][1];
  }
  int cnt[b];
  int sum = 1;
  memset(cnt, 0, sizeof(cnt));
  cnt[s[0]] = 1;
  for (int f = 1; f < (int)(s).size(); ++f) {
    for (int d = 0; d < s[f]; ++d) {
      cnt[d] ^= 1;
      if (cnt[d] == 0) {
        --sum;
      } else {
        ++sum;
      }
      ans += dp[b][(int)(s).size() - f - 1][sum];
      cnt[d] ^= 1;
      if (cnt[d] == 0) {
        --sum;
      } else {
        ++sum;
      }
    }
    cnt[s[f]] ^= 1;
    if (cnt[s[f]] == 0) {
      --sum;
    } else {
      ++sum;
    }
  }
  ans += dp[b][0][sum];
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  bin[0][0] = 1;
  for (int i = 1; i < 20; ++i) {
    bin[i][0] = 1;
    for (int j = 1; j < 20; ++j) {
      bin[i][j] = bin[i - 1][j] + bin[i - 1][j - 1];
    }
  }
  dp[1][0][0] = 1;
  for (int len = 1; len < 20; ++len) {
    dp[1][len][len % 2] = 1;
  }
  for (int b = 2; b <= 10; ++b) {
    dp[b][0][0] = 1;
    for (int len = 1; len < 20; ++len) {
      for (int cnt = 0; cnt <= b; ++cnt) {
        if (cnt == 0) {
          for (int z = 0; z <= len; z += 2) {
            dp[b][len][cnt] += dp[b - 1][len - z][cnt] * bin[len][z];
          }
        } else {
          for (int z = 1; z <= len; z += 2) {
            dp[b][len][cnt] += dp[b - 1][len - z][cnt - 1] * bin[len][z];
          }
        }
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int b;
    long long l, r;
    cin >> b >> l >> r;
    long long ans = solve(b, r);
    if (l > 1) {
      ans -= solve(b, l - 1);
    }
    cout << ans << "\n";
  }
}
