Excited Scooby
Practice
2.3 (6 votes)
Mathematics
Easy
Mathematics
Mathematics
Mathamatics
Problem
31% Success 8410 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Scooby and all of his friends have gathered for a party. There are N friends present. Scooby is really happy to see all of his friends in one place and is excited to greet them.

All N friends are seated in a circle, and are numbered from 0 to N-1. Scooby is initially sitting beside the Ath friend. After greeting one friend, he goes clockwise to the Bth next friend, sits next to him and greets him. He repeats this till he returns to the Ath friend.

In his excitement, it is possible that Scooby misses out on greeting some friends. Your job is to find the number of friends (including A) that Scooby will have greeted before reaching back to A.

Input:

The first line contains T, the number of test cases.

Each of the next T lines contain three space-separated integers, the values of A, B and N for that test case.

Output:

For each test case, output the number of friends that Scooby will have greeted before reaching back to A.

Constraints:

1 ≤ T ≤ 100000

1 ≤ N ≤ 1015

0 ≤ B ≤ 1015

0 ≤ A < N

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:20
46 votes
Tags:
Ad-HocAlgorithmsOpenApprovedEasyMathamatics
Points:20
7 votes
Tags:
MathNumber theoryBasic MathAlgorithms
Points:20
4 votes
Tags:
Basic ProgrammingOpenApprovedEasy