Divizibilitate

Time limit: 2s Memory limit: 64MB Input: Output:

Simon learned the divisibility criteria yesterday at school, and being a very smart student, Georgescu gave him an interesting problem he had been thinking about since his youth.

Task

Given the set of digits M={1,6,8,9}M = \{ 1, 6, 8, 9 \}, answer QQ questions of the form:
How many numbers with NN digits formed only from the digits in set MM have a remainder of XX when divided by 33.

Input data

The first line contains the number QQ, and the next QQ lines contain two natural numbers NN and XX.

Output data

Print QQ natural numbers, each on a new line, representing the answer to each question in order.

Constraints and clarifications

  • Since the answer can be very large, Simon will only tell Georgescu the remainder when it is divided by 109+710^9 + 7.
  • 1N10181 \leq N \leq 10^{18}.
  • 1Q100 0001 \leq Q \leq 100 \ 000.
  • Clearly, 0X20 \leq X \leq 2.
# Points Constraints
1 11 1N61 \leq N \leq 6
2 20 X=0X = 0
3 41 7N100 0007 \leq N \leq 100 \ 000
4 28 No other restrictions

Example

stdin

3
2 0
3 1
5 2

stdout

6
21
341

Explanation

For N=2N = 2 and X=0X = 0, the convenient numbers are: 1818, 8181, 6666, 9999, 6969, 9696.
There are 66 numbers with 22 digits formed from the digits in set MM that are divisible by 33.
The remainder of 66 divided by 109+710^9 + 7 is 66.

Log in or sign up to be able to send submissions!