#include<iostream>
using namespace std;
int M,N,sr,sc;
int C,H,T;
int Chay[200][2];
int Ho[200][2];
int Thoat[200][2];
int Arr[20][20];
int Arr_thoat[20][20];
int diamond[20][20];
int time_chay[20][20];
bool visit[20][20];
int f=-1,r=-1;
int Qx[100000];
int Qy[100000];
int max_kt=0;
int dx[4] ={0,0,1,-1};
int dy[4]={-1,1,0,0};
void push(int x,int y)
{
f++;
Qx[f] =x;
Qy[f]=y;
}
void pop(int &x,int &y)
{
r++;
x= Qx[r];
y= Qy[r];
}
void bfs(int x,int y)
{
f=r=-1;
for(int i=1; i<=C; i++)
{
push(Chay[i][0], Chay[i][1]);
time_chay[Chay[i][0]][Chay[i][1]] =0;
}
while(f != r)
{
pop(x,y);
for(int i=0; i<4; i++)
{
int xx= x+dx[i];
int yy =y+dy[i];
if(xx>=1 && xx <=M && yy >=1 && yy <=N)
{
if(time_chay[xx][yy] == 1000 && Arr[xx][yy] !=2){
push(xx,yy);
time_chay[xx][yy] = time_chay[x][y] +1;
}
}
}
}
}
void BT(int sr,int sc,int time ,int summ)
{
if(Arr_thoat[sr][sc] == 3){
if(summ > max_kt) { max_kt = summ; }
return ;
}
for(int i=0; i<4; i++)
{
int xx= sr+dx[i];
int yy =sc+dy[i];
if((xx<1 && xx >M && yy <1 && yy >N) || visit[xx][yy]){
continue;
}
else {
int next_time;
if(Arr[xx][yy] == 2) { next_time =time +2;}
else { next_time = time +1;}
if(next_time < time_chay[xx][yy] ){
visit[xx][yy] =true;
BT(xx,yy,next_time,summ + diamond[xx][yy]);
visit[xx][yy] =false;
}
}
}
}
int main()
{
int t;
cin >> t;
for(int stt=1; stt <=t; stt++)
{
cin >> M >> N >> sr >> sc;
for(int i=1; i<=M; i++)
{
for(int j=1; j<=N; j++)
{
visit[i][j] = Arr_thoat[i][j] = Arr[i][j]=0;
}
}
cin >> C;
for(int i=1; i<=C; i++)
{
cin >> Chay[i][0] >> Chay[i][1];
}
cin >> H;
for(int i=1; i<=H; i++)
{
cin >> Ho[i][0] >> Ho[i][1];
Arr[Ho[i][0]][Ho[i][1]] =2;
}
cin >> T;
for(int i=1; i<=T; i++)
{
cin >> Thoat[i][0] >> Thoat[i][1];
Arr_thoat[Thoat[i][0]][Thoat[i][1]] =3;
}
for(int i=1; i<=M; i++)
{
for(int j=1; j<=N; j++)
{
cin >> diamond[i][j];
time_chay[i][j] = 1000;
}
}
////////////////////////////////////////////////
bfs(0,0);
BT(sr,sc,0,diamond[sr][sc]);
cout << max_kt << endl;
max_kt =0;
///////
}
}
////////////////////////////////////////////////////////
#include<iostream>
using namespace std;
int N;
int M[20];
int visit[20];
int save[20];
int count_;
int summ;
bool so_nt(int n)
{
for(int i=2; i*i <=n; i++)
{
if(n %i == 0)
return false;
}
return true;
}
void reset_visit()
{
count_=0; summ=0;
for(int i=1; i<=N; i++)
{
visit[i] =0;
}
visit[1]=1;
save[0]=M[1];
}
void BT(int k,int m)
{
if(k>1 && k <= N && !so_nt(save[k-2]+save[k-1])){
return;
}
if(k == N && so_nt(save[0]+save[N-1])){
for(int i=0; i<N; i++)
{
cout << save[i] << " " ;
}
cout << endl;
return ;
}
for(int i=1; i<=N; i++)
{
if(visit[i] == 0 ){
save[k]= M[i];
visit[i] =1;
BT(k+1,i);
visit[i] =0;
}
}
}
int main()
{
//freopen("Text.txt","r",stdin);
int t;
cin >> t;
for(int stt=1; stt <=t; stt++)
{
cin >> N;
for(int i=1; i<=N; i++)
{
cin >> M[i];
}
///////////////////
reset_visit();
BT(1,0);
cout << count_ << endl;
}
return 0;
}