Noisy Neighbors

Place N tenants on an R by C grid to minimize the number of shared walls between occupied neighbors.

Medium4Brute forceBit manipulationInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

You are a landlord. Your building is an R×CR \times C grid of apartments, and each apartment is a unit square with four walls. You want to rent out NN of the apartments to tenants, exactly one tenant per apartment, and leave the rest empty.

Every tenant is noisy. Whenever two occupied apartments share a wall, the building gains one point of unhappiness. Two apartments that touch only at a corner do not share a wall. For example, a 2×22 \times 2 building with all four apartments occupied has four walls between neighboring tenants, so its unhappiness is 4.

Place the NN tenants so that the unhappiness is as small as possible, and report that minimum.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains three integers RR, CC, and NN, separated by single spaces.

Limits

  • 1T10001 \le T \le 1000
  • 1R×C161 \le R \times C \le 16
  • 0NR×C0 \le N \le R \times C

Output

For each test case, print one line of the form Case #x: y, where xx is the test case number starting at 1 and yy is the minimum possible unhappiness of the building.

Notes

In sample case 1, every apartment of the 2×32 \times 3 building is occupied, so all seven internal walls sit between two tenants.

In sample case 2, the two tenants fit into the 4×14 \times 1 building in several ways without sharing a wall.

In sample case 3, the best placement puts the eight tenants in a ring along the border of the 3×33 \times 3 building and leaves the middle apartment empty.

The picture below shows sample cases 1, 2, and 3. Each red wall costs one point of unhappiness.