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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jialin Ouyang (Jialin.Ouyang@gmail.com)
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    QuickScanner in = new QuickScanner(inputStream);
    QuickWriter out = new QuickWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    static int MAXB = 10;
    static int MAXL = 64;
    static int MAXMASK = 1 << MAXB;
    long[][][] anyCnt;
    long[][] positiveCnt;
    int totalLength;
    int[] digits;

    public void solve(int testNumber, QuickScanner in, QuickWriter out) {
      init();
      for (int remQ = in.nextInt(); remQ > 0; --remQ) {
        int b = in.nextInt();
        long l = in.nextLong();
        long r = in.nextLong();
        out.println(calc(b, r) - calc(b, l - 1));
      }
    }

    void init() {
      anyCnt = new long[MAXB + 1][MAXL][MAXMASK];
      for (int b = 2; b <= MAXB; ++b) {
        anyCnt[b][0][0] = 1;
        for (int length = 1; length < MAXL; ++length)
          for (int mask = (1 << b) - 1; mask >= 0; --mask) {
            long sum = 0;
            for (int digit = 0; digit < b; ++digit) {
              sum += anyCnt[b][length - 1][mask ^ (1 << digit)];
            }
            anyCnt[b][length][mask] = sum;
          }
      }
      positiveCnt = new long[MAXB + 1][MAXL];
      for (int b = 2; b <= MAXB; ++b)
        for (int length = 1; length < MAXL; ++length) {
          long sum = 0;
          for (int digit = 1; digit < b; ++digit) {
            sum += anyCnt[b][length - 1][1 << digit];
          }
          positiveCnt[b][length] = sum;
        }
    }

    long calc(int base, long x) {
      if (x == 0) return 0;
      if (digits == null) {
        digits = new int[MAXL];
      }
      for (totalLength = 0; x > 0; x /= base) {
        digits[totalLength++] = (int) (x % base);
      }
      long res = 0;
      for (int length = 1; length < totalLength; ++length) {
        res += positiveCnt[base][length];
      }
      int mask = 0;
      for (int i = totalLength - 1; i >= 0; --i) {
        for (int digit = i + 1 == totalLength ? 1 : 0; digit < digits[i]; ++digit) {
          res += anyCnt[base][i][mask ^ (1 << digit)];
        }
        mask ^= 1 << digits[i];
      }
      return res + (mask == 0 ? 1 : 0);
    }

  }

  static class QuickScanner {
    private static final int BUFFER_SIZE = 1024;
    private InputStream stream;
    private byte[] buffer;
    private int currentPosition;
    private int numberOfChars;

    public QuickScanner(InputStream stream) {
      this.stream = stream;
      this.buffer = new byte[BUFFER_SIZE];
      this.currentPosition = 0;
      this.numberOfChars = 0;
    }

    public int nextInt() {
      int c = nextNonSpaceChar();
      boolean positive = true;
      if (c == '-') {
        positive = false;
        c = nextChar();
      }
      int res = 0;
      do {
        if (c < '0' || '9' < c) throw new RuntimeException();
        res = res * 10 + c - '0';
        c = nextChar();
      } while (!isSpaceChar(c));
      return positive ? res : -res;
    }

    public long nextLong() {
      int c = nextNonSpaceChar();
      boolean positive = true;
      if (c == '-') {
        positive = false;
        c = nextChar();
      }
      long res = 0;
      do {
        if (c < '0' || '9' < c) throw new RuntimeException();
        res = res * 10 + c - '0';
        c = nextChar();
      } while (!isSpaceChar(c));
      return positive ? res : -res;
    }

    public int nextNonSpaceChar() {
      int res = nextChar();
      for (; isSpaceChar(res) || res < 0; res = nextChar()) ;
      return res;
    }

    public int nextChar() {
      if (numberOfChars == -1) {
        throw new RuntimeException();
      }
      if (currentPosition >= numberOfChars) {
        currentPosition = 0;
        try {
          numberOfChars = stream.read(buffer);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        if (numberOfChars <= 0) {
          return -1;
        }
      }
      return buffer[currentPosition++];
    }

    public boolean isSpaceChar(int c) {
      return c == ' ' || c == '\t' || isEndOfLineChar(c);
    }

    public boolean isEndOfLineChar(int c) {
      return c == '\n' || c == '\r' || c < 0;
    }

  }

  static class QuickWriter {
    private final PrintWriter writer;

    public QuickWriter(OutputStream outputStream) {
      this.writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public QuickWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects) {
      for (int i = 0; i < objects.length; ++i) {
        if (i > 0) {
          writer.print(' ');
        }
        writer.print(objects[i]);
      }
    }

    public void println(Object... objects) {
      print(objects);
      writer.print('\n');
    }

    public void close() {
      writer.close();
    }

  }
}

