Under construction

Still being built. Things may change

Breaking the Superconductor

Time limit2sMemory limit1024 MB

Summary
Find the minimum total cost to cut an N gram block into K pieces of at least 1 gram each, where each cut costs the weight of the piece being cut.
Level

Medium6 of 10

Topics
Math, Greedy
Solved
No attempts yet

Problem

You developed a room-temperature, ambient-pressure superconductor and wrote a paper that will change the world. You have a block of superconductor weighing NgN\mathrm{g}. To verify the paper, KK laboratories have asked for superconductor samples. Each laboratory needs a sample of at least 1g1\mathrm{g}. Fortunately, you have a technique for breaking superconductors precisely. For an integer aa with 2≤a2 \le a, a piece of aga\mathrm{g} can be cut as follows.

  • Choose an integer bb with 1≤b<a1 \le b < a.
  • Split the piece into one piece of bgb\mathrm{g} and one piece of (a−b)g(a-b)\mathrm{g}.
  • The cut costs aa won.

To reduce research costs, you need to find the minimum cost of cutting the superconductor into KK pieces. You can assume that under the constraints, the superconductor can be split into KK pieces using the method above.

Input

The first line contains the number of test cases TT.

Each of the next TT lines contains two integers NN and KK, separated by a space. NN is the weight of the superconductor block, and KK is the number of laboratories that asked for samples.

Output

For each test case, print the minimum cost of cutting the superconductor into KK pieces.

Constraints

  • 1≤T≤100 0001 \le T \le 100\,000
  • 2≤K≤N≤1 000 000 0002 \le K \le N \le 1\,000\,000\,000
  • All given numbers are integers.

Examples1

  1. Example 1

    Input
    3
    2 2
    5 3
    10000 1000
    
    Expected output
    2
    7
    19965