Milly and balls
Practice
3.5 (446 votes)
Mathematics
Easy
Problem
43% Success 8193 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Milly is standing at the centre of a circle made by her friends. There are N people standing in a circle facing towards Milly as shown below. 2nd person is at the left of the 1st person and right of the 3rd one. Similarly Nth person ia at the right of the 1st person and at the left of the (N-1)th person. Milly has K balls initially with her. She starts from the 1st person's position and move in a clock-wise order. She will give i balls to every ith person of this circle. She wants to complete the circle fully which means that if at some instant she is not able to give the balls due to lack of required number of balls then she will require some P number of additional balls in order to complete the circle and then she will stop.enter image description here

Note : Milly can complete the circle any number of time till she has the required number of balls but once it is not possible to give the balls as per the requirement then she will take those P additional balls to complete the circle and then she will stop. If she is able to complete the circle some number of times using those K balls then she will not require any additional balls.

Input

  • First line of the input will contain a integer T (number of test cases).
  • Then for every test case there will be one line containing two space separated integers N and K.

Output

  • For every test case, print the value of P in a new line.

Constraints

  • 1 ≤ T ≤ 105
  • 1 ≤ N ≤ 109
  • 0 ≤ K ≤ 109

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
1 votes
Tags:
StringBasics of bit manipulationBasic ProgrammingBit manipulationBasics of Bit ManipulationLogic-BasedEasyBit Manipulation
Points:20
178 votes
Tags:
GeometryHiringApprovedEasy
Points:30
5 votes
Tags:
Ad-HocAlgorithmsMediumDynamic ProgrammingDynamic programming