Under construction

Still being built. Things may change

Rainy Day

Time limit1sMemory limit1024 MB

Summary
Given N students, M umbrellas, and a per-umbrella capacity K, find the fewest moves for all students to cross between two halls, or -1 if impossible.
Level

Medium5 of 10

Topics
BFS, Simulation
Solved
No attempts yet

Problem

On a rainy day, NN students want to cross from the Creative Talent Hall to the Convergence Talent Hall. The Creative Talent Hall has MM umbrellas, and the Convergence Talent Hall has none. One umbrella can be shared by up to KK students at a time. The students repeat the following operations to cross.

  • Some of the students in one building cross to the other building using umbrellas. They do not all need to use the same umbrella.
  • Any umbrella that nobody is using can be taken along without limit. For example, one student can use one umbrella and carry three more.

Determine whether all students can cross without getting wet. If they can, find the minimum number of operations needed for all students to cross. Not every umbrella has to be brought to the Convergence Talent Hall.

Strictly speaking, the state is an ordered quadruple (a,b,c,d)(a,b,c,d), where aa is the number of students in the Creative Talent Hall, bb is the number of umbrellas in the Creative Talent Hall, cc is the number of students in the Convergence Talent Hall, and dd is the number of umbrellas in the Convergence Talent Hall. Initially a=Na=N, b=Mb=M, c=d=0c=d=0, and the goal is c=Nc=N in the fewest operations. An operation is one of the following.

  • Choose two positive integers xx and yy and change (a,b,c,d)(a,b,c,d) to (a−x,b−y,c+x,d+y)(a-x,b-y,c+x,d+y). This means xx students move from the Creative Talent Hall to the Convergence Talent Hall using yy umbrellas. Here 0<x≤a0 < x \le a, 0<y≤b0 < y \le b, and x≤Kyx \le Ky must hold.
  • Choose two positive integers zz and ww and change (a,b,c,d)(a,b,c,d) to (a+z,b+w,c−z,d−w)(a+z,b+w,c-z,d-w). This means zz students move from the Convergence Talent Hall to the Creative Talent Hall using ww umbrellas. Here 0<z≤c0 < z \le c, 0<w≤d0 < w \le d, and z≤Kwz \le Kw must hold.

Input

The first line contains the number of test cases TT.

Each of the next TT lines contains three integers NN, MM, and KK, separated by spaces.

Output

For each test case, print one integer on its own line. If all students can cross, print the minimum number of operations needed for all students to cross. Otherwise, print -1.

Constraints

  • 1≤T≤1 0001\le T\le 1\, 000
  • 1≤i≤T1\le i\le T
  • 1≤N,M,K≤101\le N,M,K\le 10

Examples1

  1. Example 1

    Input
    3
    7 2 2
    2 1 1
    1 3 5
    
    Expected output
    3
    -1
    1