package kaoshiyi;
import java.util.Scanner;
public class Kaoshiyi {
public static int s[][];
public static int bhang[];
public static int[] hang_min;
public static int zongmin;
public static int dig[];
public static int C1;
public static int R1;
public static int M1;
public static int C2;
public static int R2;
public static int M2;
public static int min;
public static int N;
public static int free(int m,int n){
int min=0x7fffffff;
if(n%2==0){
int cutposi=n/2;
int repeat=n-1;
for(int i=0;i<=cutposi;i++){
int sum=0;
for(int j=0;j<n;j++){
if(j<i) {
sum=sum+s[m][j]*C1;
}else{
sum=sum+s[m][j]*C2;
}
}
sum=sum+R2*repeat;
if(min>sum) min=sum;
repeat=repeat-2;
}
repeat=1;
for(int i=(cutposi+1);i<n;i++){
int sum=0;
for(int j=0;j<n;j++){
if(j<i) {
sum=sum+s[m][j]*C1;
}else{
sum=sum+s[m][j]*C2;
}
}
sum=sum+R1*repeat;
if(min>sum) min=sum;
repeat=repeat+2;
}
}else{
int cutposi=n/2;
int repeat=n-1;
for(int i=0;i<=cutposi;i++){
int sum=0;
for(int j=0;j<n;j++){
if(j<i) {
sum=sum+s[m][j]*C1;
}else{
sum=sum+s[m][j]*C2;
}
}
sum=sum+R2*repeat;
if(min>sum) min=sum;
repeat=repeat-2;
}
repeat=0;
for(int i=(cutposi+1);i<n;i++){
int sum=0;
for(int j=0;j<n;j++){
if(j<i) {
sum=sum+s[m][j]*C1;
}else{
sum=sum+s[m][j]*C2;
}
}
sum=sum+R1*repeat;
if(min>sum) min=sum;
repeat=repeat+2;
}
}
return min;
}
public static boolean isGoon(int step,int k){
if(k==0||step==0) {
return true;
}else{
if(bhang[step-1]==1) return false;
}
return true;
}
public static void dFS_one(int step,int endstep){
if(step==endstep){
int temp=0;
for(int i=0;i<endstep;i++){
if(bhang[i]==1) temp++;
}
if(temp==N){
int sum=0;
int digpc=0;
for(int i=0;i<endstep;i++){
if(bhang[i]==1) sum+=hang_min[i];
}
for(int i=0;i<endstep;i++){
if(bhang[i]==1) dig[digpc++]=i;
}
for(int i=1;i<digpc;i++){
sum+=(dig[i]-dig[i-1])*(M1*M1+M2*M2);
}
if(zongmin>sum) zongmin=sum;
}
return;
}
for(int k=0;k<2;k++){
if(isGoon(step,k)){
bhang[step]=k;
dFS_one(step+1,endstep);
}
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int tnum=sc.nextInt();
int nnum=tnum;
while(tnum-->0){
N=sc.nextInt();
int H=sc.nextInt();
int V=sc.nextInt();
s=new int[V][H];
for(int i=0;i<V;i++){
for(int j=0;j<H;j++){
s[i][j]=sc.nextInt();
}
}
C1=sc.nextInt();
R1=sc.nextInt();
M1=sc.nextInt();
C2=sc.nextInt();
R2=sc.nextInt();
M2=sc.nextInt();
hang_min=new int[V];
dig=new int[V];
for(int i=0;i<V;i++){
free(i,H);
hang_min[i]=free(i,H);
}
bhang=new int[V];
zongmin=0x7fffffff;
dFS_one(0,V);
System.out.println(zongmin);
}
}
}