#include<iostream>
#include<tuple>
#include<set>

using namespace std;

int main(){
  for(int M,N;cin>>M>>N,M;){
    int D,ns;
    cin>>D>>ns;
    int k[21][21]={};
    for(int i=0;i<ns;i++){
      char H,d;
      int V;
      cin>>H>>d>>V;
      cin>>k[H-'a'][V];
    }
    int nc;
    cin>>nc;
    bool uc[21][21][21][21]={};
    while(nc--){
      char H1,d1,H2,d2;
      int V1,V2;
      cin>>H1>>d1>>V1>>H2>>d2>>V2;
      uc[H1-'a'][V1][H2-'a'][V2]=uc[H2-'a'][V2][H1-'a'][V1]=true;
    }
    int nj;
    cin>>nj;
    int jam[21][21][21][21]={};
    while(nj--){
      char H1,d1,H2,d2;
      int V1,V2,d;
      cin>>H1>>d1>>V1>>H2>>d2>>V2>>d;
      jam[H1-'a'][V1][H2-'a'][V2]=jam[H2-'a'][V2][H1-'a'][V1]=d;
    }
    char H1,d1,H2,d2;
    int V1,V2;
    cin>>H1>>d1>>V1>>H2>>d2>>V2;
    set<tuple<int,int,int,int> > s;
    s.insert(make_tuple(0,H1-'a',V1,1));
    for(auto e:s){
      if(get<1>(e)==H2-'a'&&get<2>(e)==V2){
	cout<<get<0>(e)<<endl;
	break;
      }
      for(int i=1;i<=3;i++){
	int nd=(get<3>(e)+i+2)%4;
	static int dy[]={-1,0,1,0};
	static int dx[]={0,1,0,-1};
	int sy=get<1>(e);
	int sx=get<2>(e);
	int ny=sy+dy[nd];
	int nx=sx+dx[nd];
	if(ny<0||M<=ny||nx<1||N<nx||uc[sy][sx][ny][nx])continue;
	int nt=get<0>(e)+D+jam[sy][sx][ny][nx];
	if(k[ny][nx]&&nt%(2*k[ny][nx])<k[ny][nx]==nd%2)continue;
	s.insert(make_tuple(nt,ny,nx,nd));
      }
    }
  }
}

    