You will be given two positive integers **A** and **N **and you have to find the smallest positive integer **x **such that **N** divides **Ax - 1**.

**Input: **

The first line of input contains a single integer **T** denoting the number of test cases.

Then **T** test cases follow. The first and only line of each test case consists of two space separated positive integers denoting **A** and **N**.

**Output:**

Corresponding to each test case, in a new line, print the smallest positive number **x** such that **N** divides **Ax - 1**. If it is not possible to find such a number **x**, then print -1.

**Constraints:**

1 ≤ **T** ≤ 100

1 ≤ **N**,**A** ≤ 10000

**Example:**

**Input**

3

10 8

7 16

5 18

**Output**

-1

7

11

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.

ramabhatta | 35 |

Shubham Awasthi | 34 |

pguptacse17 | 30 |

abhithakur588 | 28 |

adityasuman2025 | 27 |

the_coder95 | 1236 |

RishabhTanwar1 | 1104 |

thanuvinu94 | 676 |

tathagat289 | 664 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4889 |

Quandray | 4547 |

Login to report an issue on this page.