题目收集
题目列表题单
返回列表

password

CodeforcesP0024· 3000ms· 1024 MB· ZJU Summer 2026 Contest 2原题链接
数论

创建于 2026/07/16 · 更新于 2026/07/16

题目描述

Frog Q has a secret integer kkk. Frog Q's friend F only knows that k∈[1,n]k \in [1, n]k∈[1,n].

To reveal kkk to F, Q may perform several hint operations. In each operation:

  • Q chooses an integer ppp satisfying 1≤p≤k1 \le p \le k1≤p≤k;
  • Q tells F: k mod p=ck \bmod p = ckmodp=c;
  • The cost of this hint is ccc (the value of k mod pk \bmod pkmodp).

After some number of hints, if F can uniquely determine the value of kkk, the communication succeeds.

Q wants to minimize the total cost. Compute this minimum total cost.

输入格式

The first line contains an integer T (1≤T≤105)T\ (1 \le T \le 10^5)T (1≤T≤105), the number of test cases.

The next TTT lines each contain two integers n,k (1≤k≤n<231)n, k\ (1 \le k \le n \lt 2^{31})n,k (1≤k≤n<231), separated by a space.

输出格式

Output TTT lines, each containing a single integer — the minimum total cost for the corresponding test case.

If F can never uniquely determine the value of kkk, output −1-1−1.

样例输入 #1

5
5 3
8 3
10 3
2147483647 10
144322 17

样例输出 #1

0
1
-1
-1
10