#P4339. [ZJOI2018] 迷宫
[ZJOI2018] 迷宫
Description
Summer vacation is coming, and Kelian plans to build a castle next to her private beach so she can invite her friends over during the holidays. She also plans to build a maze under the castle, because exploration is always fun.
After a simple design, Kelian plans to build a maze as follows:
- The maze can be abstracted as a directed graph with vertices and edges. Vertex is the only entrance and also the only exit.
- Each vertex has exactly outgoing edges, and these outgoing edges are labeled in order by integers in .
- Self-loops and parallel edges are allowed.
A good maze should also have some puzzle elements. Kelian hopes that every cycle starting from vertex and returning to vertex follows a certain pattern. She finds that if you record the labels of all edges along a path starting from vertex , you obtain a base- number (possibly with leading s). Conversely, for every base- number (possibly with leading s), there exists a corresponding path starting from vertex .
Kelian then chooses an integer . She wants the maze to satisfy: a path starting from vertex returns to vertex if and only if the number corresponding to this path is a multiple of .
Now that and have been chosen, she realizes that not every admits a maze design that meets all the conditions above. Since building a maze is time-consuming and laborious, Kelian wants to find the minimum that satisfies the conditions.
However, Kelian is not interested in complicated calculations, so she asks you to compute this value for her.
Input Format
The first line contains an integer — the number of test cases.
Each of the next lines contains two positive decimal integers — the chosen integers.
Output Format
For each test case, output a single integer: the minimum that satisfies all the conditions. If no such exists, output -1.
3
2 3
2 4
6 8
3
3
5
Hint
One possible design for the first test case (left) and the second test case (right) is shown below. Purple edges denote the edge labeled , and blue edges denote the edge labeled .


Translated by ChatGPT 5
京公网安备 11011102002149号