eolymp
bolt
Try our new interface for solving problems
Məsələlər

Exact Number of Drops

Exact Number of Drops

Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB

- Hmm... I asked for 400 drops, but there are 402 here.

- 400 drops, we never make mistakes.

Russian cartoon _The Mystery of the Third Planet

Do you think that managing a restaurant is easy? That is not the case when all clients are pretty demanding.

You are managing a restaurant, and every day a lot of people come and ask you to give them some drinks. As your restaurant has just recently opened, you don't have any vessels except two, which can accommodate a drops of liquid and b drops of liquid, respectively. In order not to make your clients wait (or at least to minimize their waiting time), you always have to determine the minimal number of steps required to obtain exactly c drops of liquid in one of the vessels.

At the beginning both vessels are empty. The following operations are counted as steps:

  • emptying a vessel;

  • filling a vessel;

  • pouring liquid from one vessel to the other (without spilling) until one of the vessels is either full or empty.

Eventually you became tired to do it all by hand and decided to write a program which will help you calculate the minimum number of steps required in all these cases.

Giriş verilənləri

The first line contains the number of test cases T (1T10^5). Each of the next T lines contains one testcase and consists of three integers a, b and c (1a, b, c10^9).

Çıxış verilənləri

For each test case print the minimum number of steps required to obtain exactly c drops of liquid in one of the vessels or -1 if this is impossible.

Nümunə

Giriş verilənləri #1
2
2 5 4
5 3 7
Çıxış verilənləri #1
4
-1
Müəllif Gennady Korotkevich
Mənbə Gennady Korotkevich Contest 1, Petrozavodsk Training Camp, Day 1, Friday, August 23, 2013