#P16605. [SYSUCPC 2025] Sum

[SYSUCPC 2025] Sum

题目描述

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.

输入格式

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 TT lines, each containing an integer, representing the smallest digit sum.

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

提示

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.