#P4339. [ZJOI2018] 迷宫

    ID: 3269 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学2018各省省选浙江强连通分量,缩点

[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 nn vertices and nmnm edges. Vertex 11 is the only entrance and also the only exit.
  • Each vertex has exactly mm outgoing edges, and these outgoing edges are labeled in order by integers in [0,m)[0, m).
  • Self-loops and parallel edges are allowed.

A good maze should also have some puzzle elements. Kelian hopes that every cycle starting from vertex 11 and returning to vertex 11 follows a certain pattern. She finds that if you record the labels of all edges along a path starting from vertex 11, you obtain a base-mm number (possibly with leading 00s). Conversely, for every base-mm number (possibly with leading 00s), there exists a corresponding path starting from vertex 11.

Kelian then chooses an integer KK. She wants the maze to satisfy: a path starting from vertex 11 returns to vertex 11 if and only if the number corresponding to this path is a multiple of KK.

Now that mm and KK have been chosen, she realizes that not every nn 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 nn 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 TT — the number of test cases.

Each of the next TT lines contains two positive decimal integers m,Km, K — the chosen integers.

Output Format

For each test case, output a single integer: the minimum nn that satisfies all the conditions. If no such nn 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 00, and blue edges denote the edge labeled 11.

Translated by ChatGPT 5