Mirko wants to buy land and build a house for his family on it. So far he has seen K plots. Each plot is a rectangle of N rows and M columns, so it has N×M fields in total.
The plot has to be maintained before construction starts, and the lawn has to be mowed, so Mirko bought a lawn mower. To mow the whole lawn he has to pass over each of the N×M fields at least once. He can start on any field, facing up, down, left, or right. The mower only moves forward to the adjacent field in the direction it faces, or turns 90 degrees in place. For his own safety Mirko never drives off his land, so the mower stays inside the rectangle.
Turning the mower is hard work, so Mirko wants to mow with as few turns as possible. For each plot, find the smallest number of turns that lets him mow the entire lawn.