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;
using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using pdd = pair<double, double>;
using vpdd = vector<pdd>;
template <typename T>
using uset = unordered_set<T>;
template <typename T>
using umultiset = unordered_multiset<T>;
template <typename K, typename V>
using umap = unordered_map<K, V>;
template <typename K, typename V>
using umultimap = unordered_multimap<K, V>;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1};
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const ll INFLL = 0x7FFFFFFFFFFFFFFFLL;
const double pi = acos(-1);
template <typename T>
T take(queue<T>& O) {
  T tmp = O.front();
  O.pop();
  return tmp;
}
template <typename T>
T take(stack<T>& O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <typename T>
T take(priority_queue<T>& O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <typename T>
inline bool inRange(const T& z, const T& a, const T& b) {
  return a <= z && z <= b;
}
void OPEN(const string& in = "input.txt", const string& out = "output.txt") {
  freopen(in.c_str(), "r", stdin);
  freopen(out.c_str(), "w", stdout);
  return;
}
struct DP {
  int val;
  int flag = 0;
};
const int MOD = 1000000007;
int b;
int dp[65][11][1 << 10];
int len;
int nums[65];
inline void add(int& res, int val) {
  res += val;
  if (res >= MOD) res -= MOD;
  if (res < 0) res += MOD;
}
int fdp(int sisa, int b, int mask) {
  if (sisa < 0) return 0;
  int& res = dp[sisa][b][mask];
  if (res != -1) return res;
  if (sisa == 0) {
    res = (mask == 0 ? 1 : 0);
    return res;
  }
  res = 0;
  for (int(i) = (0), _t = (b - 1); i <= (_t); ++(i)) {
    add(res, fdp(sisa - 1, b, mask ^ (1 << i)));
  }
  return res;
}
int dpfull[11][65];
int full(int b, int len) {
  int& res = dpfull[b][len];
  if (res != -1) return res;
  res = 0;
  for (int(i) = (len - 1), _t = (1); i >= (_t); --(i)) {
    if (i & 1) {
      for (int(dig) = (1), _t = (b - 1); dig <= (_t); ++(dig)) {
        add(res, fdp(i, b, 1 << dig));
      }
    }
  }
  return res;
}
int process(ll x) {
  len = 0;
  while (x || len == 0) {
    nums[len++] = x % b;
    x /= b;
  }
  reverse(nums, nums + len);
  int res = 0;
  add(res, full(b, len - 1));
  int mask = 0;
  int digits = len;
  for (int(i) = (0), _t = (len); i < (_t); ++(i)) {
    --digits;
    for (int(j) = ((i == 0 ? 1 : 0)), _t = (nums[i]); j < (_t); ++(j)) {
      mask ^= 1 << j;
      add(res, fdp(digits, b, mask));
      mask ^= 1 << j;
    }
    mask ^= 1 << nums[i];
  }
  return res;
}
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  memset(dp, -1, sizeof(dp));
  memset(dpfull, -1, sizeof(dpfull));
  int n;
  cin >> n;
  for (int(tc) = (1), _t = (n); tc <= (_t); ++(tc)) {
    cin >> b;
    ll x, y;
    cin >> x >> y;
    --x;
    int ans = process(y);
    ans -= process(x);
    if (ans < 0) ans += MOD;
    cout << ans << "\n";
  }
}
