POUR1 - Pouring water


Given two vessels, one of which can accommodate a litres of water and the other - b litres of water, determine the number of steps required to obtain exactly c litres of water 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 water from one vessel to the other, without spilling, until one of the vessels is either full or empty.

 

Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by t sets of input data, each consisting of three positive integers a, b, c, not larger than 40000, given in separate lines.

Output

For each set of input data, output the minimum number of steps required to obtain c litres, or -1 if this is impossible.

Example

Sample input:
2
5
2
3
2
3
4

 

Sample output:
2
-1

hide comments
Jorge Bernadas: 2009-08-21 21:58:01

I solved it using a graph and a bit of math to prune impossible cases.

Ankul Garg: 2009-06-29 21:41:11

doesnt seems a graph problem :P go for some mathematics

zkk: 2009-06-27 00:29:07

Who could tell me the ALGO?
I used HASH and BFS,but my program would take 2 seconds.
I wonder there may be a math way.


Added by:adrian
Date:2004-05-31
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:An ancient problem, formulated in these words by Mr Tadeusz Ratajczak