Factorials and Powers

Time limit1sMemory limit128 MB

Summary
For each pair n and k, find the largest i such that n! is divisible by k^i.
Level

Medium5 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

A war has broken out in the Land of Mathematics. The Factorial faction and the Power faction are fighting to decide who will rule the land.

The Factorial faction's renowned general nn trains by computing his own factorial, growing as strong as n!n!, while the Power faction's admiral kk prepares an exponent ii so as to raise himself to the ii-th power, growing as strong as kik^i.

At last, today is the day nn and kk clash. Admiral kk has trained for years to divide general nn down into a smaller number.

Since both have grown through training, the fight is really between n!n! and kik^i. Write a program that finds the largest ii such that n!n! is divisible by kik^i.

Input

The first line contains the number of test cases TT. (1≤T≤1001 \le T \le 100)

Each of the next TT lines contains two integers nn and kk separated by a space. (2≤n≤10182 \le n \le 10^{18}, 2≤k≤10122 \le k \le 10^{12})

Output

For each test case, print the largest ii satisfying the condition on its own line.

Examples1

  1. Example 1

    Input
    2
    5 2
    10 10
    
    Expected output
    3
    2