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

import java.lang.*;
import java.lang.reflect.Array;
import java.math.*;
import java.util.*;
import java.io.*;

public class Main {
    void solve() {
        int q=ni();
        dp=new long[11][60][1<<10][2];
        for(int i=0;i<11;i++) for(int j=0;j<60;j++) for(int k=0;k<(1<<10);k++) Arrays.fill(dp[i][j][k],-1);
        while(q-->0){
            int b=ni(); long l=nl(),r=nl();
            long ans=solve(r,b)- solve(l-1,b);
           pw.println(ans);
        }
    }
    long dp[][][][];
    long solve(long num,int b){
         a=convert(num,b);
        int n=a.length;
        long ans=go(0,0,1,n,b,1);
      // pw.println(ans);
        return ans;


    }
    int a[];
    long go(int i,int  mask,int t,int n,int b,int l){
        if(i>=n){
            if(mask==0 && l!=1) return 1;
            else return 0;
        }
        if(t==0 && dp[b][i][mask][t]!=-1) {
           // pw.print(i+" "+mask+" "+dp[b][i][mask][t]+" ");
           return dp[b][i][mask][t];
        }
        long cc=0;
        int ed=b-1;
        if(t==1) ed=a[i];
        for(int j=0;j<=ed;j++){
            int nmask=mask,nl=l;
            if(!(l==1 && j==0)) {

                nmask ^= (1 << j);
                nl=0;
            }
            cc+=go(i+1,nmask,(t==1 && j==ed)?1:0,n,b,nl);
        }
      //   if(t==0 && dp[b][i][mask][t]!=-1) pw.println(i+" "+mask+" "+dp[b][i][mask][t]+" "+cc);
           //if(i==58 && mask==0 && t==0) pw.println(dp[b][i][mask][t]+" "+cc);
        if(l!=1)
        dp[b][i][mask][t]=cc;
        return cc;
    }


    int [] convert(long n,int b){

        int a[]=new int[60];
        int id=59;
        while(n>0){
            a[id--]=(int)(n%b);
            n/=b;
        }
        return a;
    }

    long M=(long)1e9+7;
    InputStream is;
    PrintWriter pw;
    String INPUT = "";
    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        pw = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        pw.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");

    }

    public static void main(String[] args) throws Exception { new Main().run(); }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
    private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
}