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 FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000
#define bpc(x) __builtin_popcount(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 

using namespace std;

ll dp[11][1024][64];
ll powa[64];

vector<ll> get(ll n, ll b)
{
    vector<ll> hold;
    while(n>0)
    {
        hold.pb(n%b);
        n/=b;
    }
    return hold;
}

ll gans(vector<ll> &v, ll b, ll cmask,bool st)
{
    if(v.size() == 0)
        return dp[b][cmask][0];
    ll len = v.size();    
    if(st == 0)
    {
        ll ans = 0;
        for(ll l = 1;l<len;l++)
            ans+=dp[b][0][l];    
        //cout<<"for lesser = "<<ans<<"\n";        
        for(ll dig=1;dig<v.back();dig++)
        {
            ll temp = (cmask^powa[dig]);
            ans+=dp[b][temp][len-1];   
        }
        cmask^=powa[v.back()];
        v.pop_back();
        return ans + gans(v,b,cmask,1);
    }
    ll ans = 0;
    for(ll dig=0;dig<v.back();dig++)
    {
        ll temp = (cmask^powa[dig]);
        ans+=dp[b][temp][len-1];
    }
    cmask^=powa[v.back()];
    v.pop_back();
    return ans + gans(v,b,cmask,1);
}

int main()
{
   FAST/**/
   memset(dp,0,sizeof(dp));
   
   powa[0] = 1;
   for(ll i=1;i<64;i++)
        powa[i] = powa[i-1]*2;
   
   for(ll base=2;base<11;base++)
   {
       dp[base][0][0] = 1;
       ll pref[1024];
       memset(pref,0,sizeof(pref));
       ll lim = powa[base];
       for(ll dig=1;dig<base;dig++)
            dp[base][(1ll<<dig)][1] = 1, pref[powa[dig]] = 1;
       pref[1]++;    
    
       for(ll len = 2;len<64;len++)
       {
            for(ll mask=0;mask<lim;mask++)
            {
                for(ll dig=1;dig<base;dig++)
                {
                    ll temp = (mask^(powa[dig]));
                    dp[base][mask][len] += pref[temp];
                    /*if(base == 2 && len == 4 && mask == 0)
                    {
                        cout<<"for "<<dig<<" = "<<dp[base][temp][len-1] + dp[base][(temp^powa[0])][len-2]<<"\n";
                    }*/
                }
            }
            
            ll pref1[1024];
            memset(pref1,0,sizeof(pref1));
            for(ll mask=0;mask<lim;mask++)
                pref1[mask] = pref[mask^powa[0]];
            for(ll mask=0;mask<lim;mask++)
                pref[mask] = pref1[mask];
            for(ll mask=0;mask<lim;mask++)
                pref[mask]+=dp[base][mask][len];
            
       }
   }
   //cout<<"test = "<<dp[2][0][3]<<"\n";
   ll q;
   cin>>q;
   
   while(q--)
   {
       ll b,l,r;
       cin>>b>>l>>r;
       l--;
       vector<ll> v1 = get(l,b), v2 = get(r,b);
       ll add = 0, sub = 0;
       if(l!=0)
            sub = gans(v1,b,0,0);
       add = gans(v2,b,0,0);
       cout<<add-sub<<"\n";
       
   }
   
   return 0;        
}  
   