Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts teacher. 

Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge's office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers l and r (both inclusive). 

Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base b, all the digits from 0 to b - 1 appear even number of times in its representation without any leading zeros.

You have to answer q queries to unlock the office. Each query has three integers bi, li and ri, the base and the range for which you have to find the count of magic numbers.

Input

First line of input contains q (1 ≤ q ≤ 105) — number of queries.

Each of the next q lines contain three space separated integers bi, li, ri (2 ≤ bi ≤ 10, 1 ≤ li ≤ ri ≤ 1018).

Output

You have to output q lines, each containing a single integer, the answer to the corresponding query.

Examples

Input

2
2 4 9
3 1 10


Output

1
2


Input

2
2 1 100
5 1 100


Output

21
4

Note

In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get: 

  * 4 = 1002, 
  * 5 = 1012, 
  * 6 = 1102, 
  * 7 = 1112, 
  * 8 = 10002, 
  * 9 = 10012. 



Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ProblemE2 {
    BufferedReader rd;
    long[][][][] dp = new long[11][61][10][1024];

    ProblemE2() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        startDp();
        int q = pint();
        StringBuilder buf = new StringBuilder();
        for(int i=0;i<q;i++) {
            long[] a = longarr();
            long b = a[0];
            long l = a[1];
            long r = a[2];

            if(i > 0) {
                buf.append('\n');
            }
            buf.append(solve((int)b,l,r));
        }
        out(buf);
    }

    private long solve(int base, long l, long r) {
        return countLe(base, r) - countLe(base, l-1);
    }

    private long countLe(int base, long x) {
        String s = Long.toString(x, base);
        int len = s.length();
        long res = 0;

        for(int i=1;i<len;i++) {
            for(int d=1;d<=base-1;d++) {
                res += dp[base][i][d][0];
            }
        }

        int state = 0;
        for(int i=0;i<len;i++) {
            int firstDigit = s.charAt(i)-'0';
            int limit = firstDigit;
            if(i == len-1) {
                limit++;
            }
            int start;
            if(i == 0) {
                start = 1;
            } else {
                start = 0;
            }
            for(int digit=start;digit<limit;digit++) {
                res += dp[base][len - i][digit][state];
            }
            state = flip(state, firstDigit);
        }
        return res;
    }

    private void startDp() {
        for(int base=2;base <= 10;base++) {
            for(int len=1;len <= 60;len++) {
                for(int firstDigit=0;firstDigit<base;firstDigit++) {
                    int s = 1<<base;
                    for(int state=0;state<s;state++) {
                        long res = 0;
                        int newState = flip(state, firstDigit);
                        if(len == 1) {
                            res = newState == 0 ? 1 : 0;
                        } else {
                            for(int nextDigit = 0;nextDigit<base;nextDigit++) {
                                res += dp[base][len-1][nextDigit][newState];
                            }
                        }
                        dp[base][len][firstDigit][state] = res;
                    }
                }
            }
        }
    }

    private int flip(int state, int digit) {
        int f = 1<<digit;
        int newState;
        if((state & f) > 0) {
            newState = state - f;
        } else {
            newState = state + f;
        }
        return newState;
    }

    private int pint() throws IOException {
        return pint(rd.readLine());
    }

    private int pint(String s) {
        return Integer.parseInt(s);
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemE2();
    }
}
