Under construction

Still being built. Things may change

The Idiot of the Year Contest!

Time limit1sMemory limit128 MB

Summary
For each test case, count how many times a chosen digit occurs in the decimal representation of n! where n is at most 366.
Level

Medium4 of 10

Topics
Math, Implementation, Array, Simulation
Solved
No attempts yet

Problem

The Idiot of the Year Contest (IYC) has just one basic rule! Each contestant picks a single digit from 00 to 99, computes the factorial of the number of the day of the year on which they were born, and counts how many times the chosen digit appears in that factorial. The contestant with the highest count becomes the Idiot of the Year!

For example, if someone was born on the 129129th day of the year and picked the digit 66, their score is the number of times the digit 66 appears in 129!129! (that is, 1×2×3×⋯×1291 \times 2 \times 3 \times \dots \times 129).

The chief judge wants you to write a program that reads the day of the year on which a contestant was born and the digit they picked, and reports how many times that digit appears in the factorial of the day number.

Input

The first line of the input contains a single integer TT, the number of test cases. Each of the following TT lines contains two numbers: the first is the day of the year on which a contestant was born, and the second is the digit they picked (from 00 to 99).

Output

Print TT lines. Each line contains a single integer: the number of times the chosen digit appears in the factorial of the first number.

Examples1

  1. Example 1

    Input
    2
    5 2
    7 0
    
    Expected output
    1
    2