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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#define int                 long long
#define ll              	long long
#define ld                  long double
#define rep(i,n)            for(int i=0;i<n;i++)
#define hell                1000000007
#define inf                 (ll)1e15
#define vi                  vector<int>
#define vii                 vector< vi >
#define pb                  push_back
#define mp                  make_pair
#define fi                  first
#define se                  second
#define pii                 pair<int,int>
#define all(c)              c.begin(),c.end()
#define sz(c)               (int)c.size()
#define hash1               105727LL
#define hash2               hell
//#define endl                '\n'
#define eps                 1e-10
#define pi                  3.141592653589793238462643383279502884197
typedef tree<int,null_type,less<int>,
				rb_tree_tag,tree_order_statistics_node_update> statisticsTree;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
ll power(ll x, ll y,ll p)
{
	ll res=1;
	x=x%p;
	while(y>0)
	{
		if(y&1)
			res=((res%p)*(x%p))%p;
		y=y>>1;
		x=((x%p)*(x%p))%p;
	}
	return res;
}
ll raichu(ll x,ll y)
{
	ll res=1;
	while(y>0)
	{
		if(y&1)
			res=((res)*(x));
		y=y>>1;
		x=((x)*(x));
	}
	return res;
}
bool isprime(ll n)
{
	if(n<2) return false;
	else if(n==2) return true;
	else if(n%2==0) return false;
	else
	{
		ll z=sqrt(n);
		rep(i,z-1) if(n%(i+2)==0) return false;
		return true;
	}
}
ll dp[11][1050][11];
ll res[11][1050][11][2];
ll magic(int l,int mask,int b)
{
	if(l==0)
	{
		if(mask==0) return 1;
		else return 0;
	}
	else if(dp[l][mask][b]!=-1) return dp[l][mask][b];
	ll ans=0;
	for(int i=0;i<=b-1;i++) ans+=magic(l-1,mask^(1<<i),b);
	dp[l][mask][b]=ans;
	return dp[l][mask][b];
}
ll solve(vector<int> n,int b,int mask,int l,int prev)
{
	if(l==0)
	{
		if(mask==0) return 1;
		else return 0;
	}
	else if(res[l][mask][b][prev]!=-1) return res[l][mask][b][prev];
	else if(prev==1)
	{
		ll ans=0;
		for(int i=0;i<n[sz(n)-1-l+1];i++)
		{
			ans+=magic(l-1,mask^(1<<i),b);
		}
		ans+=solve(n,b,mask^(1<<n[sz(n)-1-l+1]),l-1,1);
		res[l][mask][b][prev]=ans;
		return ans;
	}
	else
	{
		ll ans=0;
		for(int i=l-1;i>=1;i--)
		{
			for(int j=1;j<=b-1;j++)
			{
				ans+=magic(i-1,mask^(1<<j),b);
			}
		}
		for(int i=1;i<n[sz(n)-1-l+1];i++)
		{
			ans+=magic(l-1,mask^(1<<i),b);
		}
		ans+=solve(n,b,mask^(1<<n[sz(n)-1-l+1]),l-1,1);
		res[l][mask][b][prev]=ans;
		return ans;
	}
}
int32_t main()
{
	//freopen("B-large.in","r",stdin);
	//freopen("out2.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1,Tests;
	cin>>tests;
	Tests=tests;
	memset(dp,-1,sizeof dp);
	memset(res,-1,sizeof res);
	while(tests--)
	{
		int b;
		ll l,r;
		cin>>b>>l>>r;
		vector<int> R;
		int idx=0;
		while(r>0)
		{
			R.pb(r%b);
			idx++;
			r/=b;
		}
		vector<int> L;
		idx=0;
		l--;
		while(l>0)
		{
			L.pb(l%b);
			idx++;
			l/=b;
		}
		reverse(all(L));
		reverse(all(R));
		//cout<<sz(L)<<" "<<sz(R)<<endl;
		ll ans=0;
		if(sz(R)>0) ans+=solve(R,b,0,sz(R),0);
		if(sz(L)>0) ans-=solve(L,b,0,sz(L),0);
		cout<<ans<<endl;
	}
	return 0;
}