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 convBase(long long a[], long long num, long long b) {
  int ind = 0;
  while (num) a[ind++] = num % b, num /= b;
  return ind;
}
long long dp[12][(1 << 10) + 2][2][70], num[70];
long long solve(int base, int mask, int start, int pos) {
  if (pos < 0) return (start && mask == 0);
  if (dp[base][mask][start][pos] != -1) return dp[base][mask][start][pos];
  long long ans = 0;
  if (!start) ans += solve(base, mask, 0, pos - 1);
  if (start) ans += solve(base, mask ^ 1, 1, pos - 1);
  for (int i = 1; i < base; i++) {
    ans += solve(base, (mask ^ (1LL << i)), 1, pos - 1);
    ans %= 1000000007;
  }
  return dp[base][mask][start][pos] = ans;
}
long long calc(int len, int base) {
  long long tot = solve(base, 0, 0, len - 1);
  int tmask = 0;
  for (int i = len; i >= 0; i--) {
    for (int j = 0; j < num[i]; j++) {
      if (!j && i == len) continue;
      tot += solve(base, (tmask ^ (1LL << j)), 1, i - 1);
      tot %= 1000000007;
    }
    tmask = (tmask ^ (1LL << num[i]));
  }
  return tot;
}
int main() {
  memset(dp, -1, sizeof(dp));
  long long big = 1e18;
  for (int i = 2; i <= 10; i++) solve(i, 0, 0, convBase(num, big, i) - 1);
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    long long b, l, r, ans = 0;
    scanf("%I64d %I64d %I64d", &b, &l, &r);
    int len = convBase(num, (long long)l, b);
    ans = -calc(len - 1, b);
    ans %= 1000000007;
    len = convBase(num, r + 1, b);
    ans += calc(len - 1, b);
    ans %= 1000000007;
    if (ans < 0) ans += 1000000007;
    printf("%I64d\n", ans);
  }
}
