Pool construction#4013

Pool construction

1 second
64 megabytes
Hard

You are working for the International Company for Pool Construction, a construction company which specializes in building swimming pools. A new client wants to build several new pool areas.

A pool area is a rectangular grid of w \times h square patches, consisting of zero or more (possibly disconnected) pools. A pool consists of one or multiple connected hole patches, which will later be filled with water. In the beginning, you start with a piece of land where each patch is either a hole in the ground ('.') or flat grass ('#'). In order to transform this land into a pool area, you must adhere to the following:

You are given the task of calculating the cost of the cheapest possible pool area given the layout of the existing piece of land.

Input

On the first line a positive integer: the number of test cases, at most . After that per test case:

  • one line with two integers and (2 \leq w, h \leq 50): the width and height of the building site.

  • one line with three integers , and (1 \leq d, f, b \leq 10000): the costs for digging a new hole, filling an existing hole, and building a boundary element between a pool and grass patch.

  • lines of characters each, denoting the layout of the original building site.

Output

Per test case:

  • one line with an integer: the cost of building the cheapest possible pool area from the original piece of land.

Examples

Example 1

Loading
Loading