XHXMMMlOP0xMWg#,,^?

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Statement

You are given two integers n n and k k .

In one operation, you can subtract any power of k k from n n . Formally, in one operation, you can replace n n by (nkx) (n-k^x) for any non-negative integer x x .

Find the minimum number of operations required to make n n equal to 0 0 .

Format

Input

Each test contains multiple test cases. The first line contains the number of test cases t t ( 1t104 1 \le t \le 10^4 ). The description of the test cases follows.

The only line of each test case contains two integers n n and k k ( 1n,k109 1 \le n, k \le 10^9 ).

Output

For each test case, output the minimum number of operations on a new line.

Sample

6
5 2
3 5
16 4
100 3
6492 10
10 1
2
3
1
4
21
10

ShanghaiTech ACM 炸鱼杯

未参加
状态
已结束
规则
OI
题目
46
开始于
2024-12-10 23:00
结束于
2024-12-15 13:00
持续时间
110 小时
主持人
参赛人数
112