Anti-smoking laws are in effect, and nearly every restaurant has had to change its interior so that smoking and non-smoking customers can be seated in separate parts of the building.
Johann, the owner of a restaurant, has decided to divide his restaurant into two zones connected by air locks. Each air lock also carries a hatch so that food can be passed through, because the staff may not enter the smoking zone. The smoking zone will be connected directly to the outside (saving on heating and providing fresh air), while the non-smoking zone must be connected to the kitchen so that the staff can serve food.
The restaurant is a rectangular grid of equally sized square rooms. Each room may be connected to its (at most four) neighbouring rooms by a passage. To split the restaurant into the two zones, Johann installs an air lock in some of the passages that connect two rooms.
Costs:
So installing an air lock in a passage whose area is $a$ square metres costs $1000 \cdot a + 1000$ euros. A wall that has no passage (area 0) needs neither an air lock nor a hatch, so it costs nothing.
The room connected to the entrance must end up in the smoking zone, and the room connected to the kitchen must end up in the non-smoking zone. Every passage that connects a room in the smoking zone to a room in the non-smoking zone must have an air lock (with a hatch); passages that stay inside a single zone need nothing.
Help Johann split the restaurant into the two zones so that the total cost is as small as possible.
The first line contains a positive integer, the number of test cases. Each test case is given as follows:
An area of 0 means there is no passage (a solid wall) between those two rooms.
For each test case, print a single line with the minimum total cost, in euros, of dividing the restaurant into the two zones so that the entrance room is in the smoking zone and the kitchen room is in the non-smoking zone.