#include<bits/stdc++.h>
using namespace std;
vector<string> lntai[15];

int volun[15][105][105],poin[15][105][105];
int startr,startc;
bool vis[15][105][105];
queue<pair< int, pair<int,int> > >info;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int l,h,w,n,s;
int cost[105],memo[10005][105];
struct nn{
	int x,y,z;
};

nn org[105];

void bfs(int floo,int ro,int co){
	vis[floo][ro][co]=1;
	info.push(make_pair(floo,make_pair(ro,co)));
	int step=-1;
	while(!info.empty()){
		step++;
		int sz=info.size();
		while(sz--){
			pair<int, pair<int,int > > now=info.front();
			int I,J;
			if(volun[now.first][now.second.first][now.second.second]){
				cost[volun[now.first][now.second.first][now.second.second]]=3*step;
			}
			for(int i=0;i<4;i++){
				I=dx[i]+now.second.first;
				J=dy[i]+now.second.second;
				if(I>=0 && J>=0 && I<h && J<w){
					if(vis[now.first][I][J]==0){
						vis[now.first][I][J]=1;
						if(lntai[now.first][I][J]!='X'){
							info.push(make_pair(now.first,make_pair(I,J)));
						}	
					}
				}
			}
			if(lntai[now.first][now.second.first][now.second.second]=='U'){
				if(vis[now.first+1][now.second.first][now.second.second]==0){
					vis[now.first+1][now.second.first][now.second.second]=1;
					info.push(make_pair(now.first+1,make_pair(now.second.first,now.second.second)));
				}
			}
			if(lntai[now.first][now.second.first][now.second.second]=='D'){
				if(vis[now.first-1][now.second.first][now.second.second]==0){
					vis[now.first-1][now.second.first][now.second.second]=1;
					info.push(make_pair(now.first-1,make_pair(now.second.first,now.second.second)));
				}
			}
			info.pop();
		}
	}
	return;
}


int dp(int time,int pos){
	if(pos>n) return 0;
	if(memo[time][pos]!=-1)return memo[time][pos];
	if(cost[pos]<=time){
		return memo[time][pos]=max(dp(time,pos+1),poin[org[pos].x][org[pos].y][org[pos].z]+dp(time-cost[pos],pos+1));
	}
	return memo[time][pos]=dp(time,pos+1);
}

int main(){
	int tc;
	scanf("%d",&tc);
	while(tc--){
		memset(memo,-1,sizeof(memo));
		memset(vis,0,sizeof(vis));
		memset(volun,0,sizeof(volun));
		for(int i=0;i<15;i++)lntai[i].clear();
		string ss;
		scanf("%d%d%d%d%d",&l,&h,&w,&n,&s);
		for(int lant=0;lant<l;lant++){
			for(int i=0;i<h;i++){
				cin>>ss;
				if(lant==0){
					int leng=ss.size();
					for(int j=0;j<leng;j++){
						if(ss[j]=='S'){
							startr=i;
							startc=j;
						}
					}	
				}
				lntai[lant].push_back(ss);
			}
		}
		for(int i=1;i<=n;i++){
			int f,r,c,p;
			scanf("%d%d%d%d",&f,&r,&c,&p);
			f--;
			r--;
			c--;
			org[i].x=f;
			org[i].y=r;
			org[i].z=c;
			volun[f][r][c]=i;
			poin[f][r][c]=p;
			cost[i]=10000000;
		}
		bfs(0,startr,startc);
		printf("%d\n",dp(s,1));
	}
	return 0;
}