Minimum Notes Required
Submissions: 981   Accuracy:

33.13%

  Difficulty: Easy   Marks: 2

John works in RS Software and today he is supposed to receive his salary. RS Software pays its employees in cash. However, John's wallet has one drawback. His wallet can contain no more than M notes or coins. Taking his case into consideration, his boss pays him his salary by the minimum notes possible. However John may have to leave out some money. Tell John how much money will he have to lose.

Note: The values of notes or coins available are 1000, 500, 100, 50, 20, 10, 5, 2 and 1.
 

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of a single line containing two space separated integers N and M denoting John's salary and the maximum number of notes his wallet can hold, respectively.

Output:

Corresponding to each test case, print the desired output in a single line.

Constraints:
1<=T<=20
1<=N<=100000
1<=M<=1000


Example:
Input:
3
1712 4
1023 2
2341 3

Output:
12
3
241

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Arpan Kundu
Author: akundusaltlake


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.