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>

#define ll long long

using namespace std;
ll vals[11][11][61];

ll combos[61][61];

ll calc(int odd, int even, int leng) {
	if (leng < 0 || even < 0 || odd < 0) return 0;
	// cout << "calc:  " << odd << " " << even << " " << leng << endl;
	if (vals[odd][even][leng] != -1) {
		return vals[odd][even][leng];
	}
	ll cur = 0LL;
	if (odd > leng) {
		cur = 0;
	}
	else if (odd % 2 != leng %2) {
		cur = 0;
	}
	else if (leng == 0) {
		cur = 1;
	}
	else if (odd == 0 && even == 0) {
		cur = 0;
	}
	
	else if (odd > 0) {
		for (int i = 1; i <= leng; i+=2) {
			// cout << " " << leng << " " << i << " " << combos[leng][i] << endl;
			// cout << "-->  " << odd-1 << " " << even << " " << leng-i << 
			// 	" " << calc(odd-1, even, leng-i) << endl;
			cur += calc(odd-1,even,leng-i) * combos[leng][i];
			// cout << "cur:  " << cur << endl;
		}
	}
	else {
		for (int i = 0; i <= leng; i+=2) {
			cur += calc(odd, even-1, leng-i) * combos[leng][i];
		}
	}
	// cout << "cur:  " << cur << " " << odd << " " << even <<
	// 	" " << leng << endl;
	vals[odd][even][leng] = cur;
	return cur;
	
}

ll run(ll num, int base) {
	char buffer[62];
	itoa(num, buffer, base);
	string cur = buffer;
	ll ans = 0;
	// cout << cur << endl;
	for (int i = 1; i < cur.length(); i++) {
		// cout << "i:   " << i << " " << base << endl;
		// cout << "val  " << i-1 << "  " << calc(1, base-1, i-1) << endl;
		ans += (base-1)*calc(1, base-1, i-1);
		// cout << "a:  " << ans << endl;
	}
	// cout << "anshere:   " << ans << endl;
	int vals[base];
	for (int i = 0; i < base; i++) {
		vals[i] = 0;
	}
	for (int i = 0; i < cur.length()-1; i++) {
		vals[cur[i]-'0']++;
		for (int j = 0; j < cur[i+1]-'0'; j++) {
			int od = 0, ev = 0;
			for (int k = 0; k < base; k++) {
				if (k == j) continue;
				if (vals[k]%2==0) ev++;
				else od++;
			}
			if (vals[j]%2==1) ev++;
			else od++;
			// cout << od << " " << ev << " " << cur.length()-(i+2) << 
			// 	" " << calc(od, ev, cur.length()-(i+2)) << 
			// 	"   i:  " << i << endl;
			ans += calc(od, ev, cur.length()-(i+2));
		}
		
	}
	vals[cur[cur.length()-1]-'0']++;
	bool ok = true;
	for (int i = 0; i < base; i++) {
		if (vals[i]%2==1) ok = false;
	}
	if (ok) ans++;
	return ans;
}

int main() {
	//FAST IO FAST IO FAST IO FAST IO FAST IO
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;
	int b;
	ll l, r;
	for (int i = 0; i < 61; i++) {
		combos[i][0] = 1;
		combos[i][1] = i;
	}
	for (int i = 0; i <= 10; i++) {
		for (int j = 0; j <= 10; j++) {
			for (int k = 0; k <= 60; k++) {
				vals[i][j][k] = -1;
			}
		}
	}
	for (int i = 1; i < 61; i++) {
		for (int j = 1; j <= i; j++) {
			combos[i][j] = combos[i-1][j-1] + combos[i-1][j];
		}
		for (int j = i+1; j < 61; j++) {
			combos[i][j] = 0;
		}
	}
	for (int numrun = 0; numrun < q; numrun++) {
		cin >> b >> l >> r;
		ll ans = run(r, b);
		// cout << "ans:  " << ans << endl;
		ans -= run(l, b);
		cout << ans << '\n';
	}
	// cin >> q;

}