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 q;
void getVector(vector<int> &value, int b, long long r) {
  while (r > 0) {
    value.push_back(r % b);
    r /= b;
  }
  reverse(value.begin(), value.end());
}
void printVector(vector<int> &value) {
  for (int i = 0; i < value.size(); ++i) {
    cout << value[i] << " ";
  }
  cout << endl;
}
long long calc(int b, int sz, int msk, long long value, bool kek = false) {
  if (value == 0) {
    return 0;
  }
  if (sz == 0) {
    return value;
  }
  long long dp[61][2049];
  int number = 1 << (b + 1);
  long long ans = 0;
  for (int i = 0; i < number; ++i) {
    for (int j = 0; j <= sz; ++j) {
      dp[j][i] = 0;
    }
  }
  int from = 1;
  dp[0][msk] = value;
  if (kek) {
    for (int i = 1; i < b; ++i) {
      dp[1][1 >> i] += dp[0][msk];
    }
    from = 2;
  }
  for (int i = from; i <= sz; ++i) {
    for (int j = 0; j < b; ++j) {
      for (int k = 0; k < number; ++k) {
        bool had = (k >> j) % 2;
        if (had) {
          dp[i][k - (1 << j)] += dp[i - 1][k];
        } else {
          dp[i][k + (1 << j)] += dp[i - 1][k];
        }
      }
    }
  }
  return dp[sz][0];
}
long long ans(int b, long long r) {
  if (r == 0) {
    return 1;
  }
  vector<int> value;
  getVector(value, b, r);
  long long ans = 0;
  long long dp[61][2049];
  int number = 1 << (b + 1);
  for (int i = 0; i <= number; ++i) {
    for (int j = 0; j <= 60; ++j) {
      dp[j][i] = 0;
    }
  }
  dp[0][0] = 1;
  for (int i = 1; i <= value.size(); ++i) {
    int digit = value[i - 1];
    int sz = value.size() - i;
    if (i == 1) {
      for (int j = 0; j <= sz; ++j) {
        ans += calc(b, sz - j, 0, 1, true);
      }
    }
    int from = 1;
    if (i != 1) {
      from = 0;
    }
    for (int j = from; j < digit; ++j) {
      for (int k = 0; k < number; ++k) {
        int msk;
        bool had = (k >> j) % 2;
        if (had) {
          msk = k - (1 << j);
        } else {
          msk = k + (1 << j);
        }
        ans += calc(b, sz, msk, dp[i - 1][k]);
      }
    }
    int j = digit;
    for (int k = 0; k < number; ++k) {
      bool had = (k >> j) % 2;
      if (had) {
        dp[i][k - (1 << j)] += dp[i - 1][k];
      } else {
        dp[i][k + (1 << j)] += dp[i - 1][k];
      }
    }
    if (i == value.size()) {
      ans += dp[i][0];
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int b;
    long long l, r;
    cin >> b >> l >> r;
    cout << ans(b, r) - ans(b, l - 1) << endl;
  }
  return 0;
}
