#P16605. [SYSUCPC 2025] Sum

    ID: 18781 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>数学2025进制根号分治高校校赛

[SYSUCPC 2025] Sum

Problem Description

Student A\texttt{Student A} is studying numeral systems. For a natural number nn, Student A\texttt{Student A} wants to know the smallest digit sum among the representations of nn in bases from 22 to RR. For example, the natural number 66 has a representation of (110)2(110)_2 in base 22, with a digit sum of 22, and a representation of (20)3(20)_3 in base 33, also with a digit sum of 22.

Input Format

There are multiple test cases.

The first line of input contains a positive integer T(1T100)T(1\leq T \leq 100), indicating the number of test cases.

The next TT lines each contain two positive integers n,R(2n,R1012)n,R(2\leq n,R\leq 10^{12}).

Output Format

Output TT lines, each containing an integer, representing the smallest digit sum.

4
6 3
7 4
30 5
999967000273 999983
2
3
2
3

Hint

For 66, the digit sum is minimized in base 22 or base 33. For 77, the digit sum is minimized in base 22 or base 33. For 3030, the digit sum is minimized in base 55.