import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TunnelConstruction {
static int n;
static int h;
static int v;
static int smin;
static int amin;
static int[][] s;
static int[] e1;
static int[] e2;
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
sc = new Scanner(new File("files/tunnel"));
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
n = sc.nextInt();
h = sc.nextInt();
v = sc.nextInt();
s = new int[v + 1][h + 1];
e1 = new int[3];
e2 = new int[3];
for (int i = 1; i <= v; i++) {
for (int j = 1; j <= h; j++) {
s[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 3; i++)
e1[i] = sc.nextInt();
for (int i = 0; i < 3; i++)
e2[i] = sc.nextInt();
amin=(e1[2]*e1[2]+e2[2]*e2[2])*v*v;
for (int i = 1; i <= v; i++) {
smin = 10000000;
for (int j = 1; j <= h + 1; j++) {
CacuMin(i, j);
}
s[i][0] = smin;
amin+=s[i][0];
}
dfs(0,-1,0);
System.out.println("Case #"+(t+1));
System.out.println(amin);
}
}
private static void dfs(int step, int pre, int cost) {
// TODO Auto-generated method stub
if(cost>=amin)
return;
if(step==n){
amin=cost;
return;
}
for(int i=pre+2;i<=v-2*(n-step-1);i++){
int dis=i-pre;
if(pre==-1)
dis=0;
dfs(step+1,i,cost+s[i][0]+dis*(e1[2]*e1[2]+e2[2]*e2[2]));
}
}
private static void CacuMin(int no, int cp) {
// TODO Auto-generated method stub
int cost1 = 0, cost2 = 0;
int n1 = cp - 1;
int n2 = h + 1 - cp;
if (n1 > n2)
cost1 = e1[1] * (n1 - n2 - 1);
else if (n2 > n1)
cost2 = e2[1] * (n2 - n1 - 1);
for (int i = 1; i < cp; i++) {
cost1 += s[no][i] * e1[0];
if (cost1 >= smin)
return;
}
for (int i = h; i >= cp; i--) {
cost2 += s[no][i] * e2[0];
if (cost2 + cost1 >= smin)
return;
}
smin=cost1+cost2;
}
}
sample input:
4
3 5 6
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
10 10 10
20 10 5
1 3 1
42 3 99
4 19 3
4 1 5
1 5 1
1 4 1 3 11
7 2 3
2 6 4
2 5 4
1 4 1 3 11
13 5 1 4 11
1 2 3 4 5
99 2 47 3 31
7 2 3
2 6 5
sample output:
Case #1
8120
Case #2
576
Case #3
57
Case #4
170