#Lutece0831. Food and Productivity

Food and Productivity

Migrated from Lutece 831 Food and Productivity

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

In the Game Civilization X, the world is made up of square-shaped tiles, forming a rectangle on the world map. For a tile located at row XX and column YY, we give it a coordinate, say (XX, YY).

As you can see, different tiles have different terrain-types, so they may have different amount of Resources. In this problem, we only consider two of them: Food and Productivity.

An important step in the Game is building cities. After you choose a tile to build a city, the Food amount in that tile will increase by AA, and the Productivity amount will increase by BB, no other tiles will be changed.

You decide to build your first city on the map, as an experienced player, you will choose the best tile on the map, here is how you make up your mind:

For each tile (XX, YY), you set up an Influence Range of that tile. Each tile (xix_i, yiy_i) is said to be within the range if the following equation holds:

$\max(\left | X - x_i \right |, \left | Y - y_i \right |) \leq R$

After checking all the tiles within that range, you will get two values: the sum of all those tiles' Food amount, and the sum of all those tiles' Productivity. Among those two values, the final score for tile (XX, YY) will be the smaller one.

When choosing tiles, you just choose the tile with the highest score to build your city.

You are given a map with size N×MN\times M, and you wonder, for different values of AA and BB, what is the highest score for your first city?

Input

The first line has a number TT (T10T\leq 10) , indicating the number of test cases.

For each test case, first line has four numbers NN, MM, RR and QQ (1N1 \leq N, M500M \leq 500, 02×R+1min(N,M)0\leq 2\times R + 1 \leq min(N,M), Q200000Q \leq 200000), which is the size of the map.

Then come NN lines each with MM numbers, they are the original FoodFood (0<Food30 < Food \leq 3) amount for each tile.

Then come NN lines each with MM numbers, they are the original ProductivityProductivity (0<Productivity30 < Productivity \leq 3) amount for each tile.

Then come QQ lines each with two numbers AA and BB (0A0 \leq A, B106B\leq 10^6). Each is a query for you to answer. Those queries will not affect each other.

Output

For test case XX, output Case #X: first, in a single line.

Then output QQ lines, each with an answer.

There should be a blank line BETWEEN each test case.

Samples

1
3 3 0 2
1 2 1
1 1 1
1 1 1
1 1 1
1 2 1
1 1 1
1 2
2 1
Case #1:
3
3

Resources

2013 ACM-ICPC China Chengdu Provincial Programming Contest