Real World

5

3 votes
Prime Factorization, Mathematics
Problem

In the real world, Bob is solving a programming problem but he is stuck on it. He is given Q (1  Q 105) queries. Each query consists of two integers a and b (1 a,b 5*105). For each query, he is required to find the minimum non-negative integer such that ac % b is 0 or ac is divisible by b. If no such c exists, the answer for that query is -1. Can you help him ?

Input: 
First line consists of a single integer Q, the number of queries.
Q lines follow. Each line represents a query and consists of two space separated integers a and b, as described above.

Output: 
Print Q lines. In the ith line, print the answer for ith query.

Constraints:
 Q 105
1 a,b  5*105

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?