#include<bits/stdc++.h>
using namespace std;
struct Si{
int sf,xl,ps;
char pa[200010];
bool zb;
}pi[15];int ts[15];
bool zt[15];
void sp(int nu,int si){
int i;
for(i=si;i<pi[nu].ps;i++)pi[nu].pa[i]=pi[nu].pa[i+1];
pi[nu].pa[pi[nu].ps]=' ';
pi[nu].ps--;
}
int n,m,men=0;
int fnu=0,sfn=0;
char me[2010];
void mp(int nu){
if(men+1<=m)pi[nu].pa[++pi[nu].ps]=me[++men];
else pi[nu].pa[++pi[nu].ps]=me[men];
if(men+1<=m)pi[nu].pa[++pi[nu].ps]=me[++men];
else pi[nu].pa[++pi[nu].ps]=me[men];
}void mp3(int nu){
if(men+1<=m)pi[nu].pa[++pi[nu].ps]=me[++men];
else pi[nu].pa[++pi[nu].ps]=me[men];
if(men+1<=m)pi[nu].pa[++pi[nu].ps]=me[++men];
else pi[nu].pa[++pi[nu].ps]=me[men];
if(men+1<=m)pi[nu].pa[++pi[nu].ps]=me[++men];
else pi[nu].pa[++pi[nu].ps]=me[men];
}
void out(){
int i,j;
for(i=1;i<=n;i++){
if(zt[i])for(j=1;j<=pi[i].ps;j++)printf("%c ",pi[i].pa[j]);
else printf("DEAD");
printf("\n");
}
}
void dead(int kier,int bekier){
zt[bekier]=false;
if(pi[bekier].sf==2){
sfn++;
if(sfn==fnu){
cout<<"MP"<<endl;
out();exit(0);
}
mp3(kier);
}else if(pi[bekier].sf==0){
cout<<"FP"<<endl;
out();exit(0);
}else if(pi[bekier].sf==1&&kier==1){
int i;
for(i=1;i<=pi[1].ps;i++){
pi[1].pa[i]=' ';
}pi[1].ps=0;
pi[1].zb=false;
}
}void widead(int kier,int bekier){
int i;
for(i=1;i<=pi[bekier].ps;i++){
if(pi[bekier].pa[i]=='P'){
sp(bekier,i);pi[bekier].xl++;
return;
}
}dead(kier,bekier);
}
void facek(int user,int nu){
int i;
for(i=1;i<=pi[nu].ps;i++){
if(pi[nu].pa[i]=='D'){
sp(nu,i);return;
}
}pi[nu].xl--;
if(pi[nu].xl==0){
widead(user,nu);
}
}
void jue(int user,int beu){
bool ch=false;
int i,j;
if(user==1&&pi[beu].sf==1){
pi[beu].xl--;
if(pi[beu].xl==0)widead(user,beu);
return;
}
while(1){
if(ch){
for(i=1;i<=pi[user].ps;i++)if(pi[user].pa[i]=='K'){sp(user,i);ch=false;break;}
if(ch){
pi[user].xl--;
if(pi[user].xl==0)widead(beu,user);
return;
}
}else{
for(i=1;i<=pi[beu].ps;i++)if(pi[beu].pa[i]=='K'){sp(beu,i);ch=true;break;}
if(!ch){
pi[beu].xl--;
if(pi[beu].xl==0)widead(user,beu);
return;
}
}
}
}
void nan(int user,int beu){
int i;
for(i=1;i<=pi[beu].ps;i++)if(pi[beu].pa[i]=='K'){sp(beu,i);return;}
pi[beu].xl--;
if(beu==1&&ts[user]!=3&&ts[user]!=2)ts[user]=1;
if(pi[beu].xl==0) widead(user,beu);
}
void wan(int user,int beu){
int i;
for(i=1;i<=pi[beu].ps;i++)if(pi[beu].pa[i]=='D'){sp(beu,i);return;}
pi[beu].xl--;
if(beu==1&&ts[user]!=3&&ts[user]!=2)ts[user]=1;
if(pi[beu].xl==0) widead(user,beu);
}bool wu_w(int user){
int i,j;
for(i=user+1;i!=user;i++){i=(i-1)%n+1;
if((pi[i].sf==0||pi[i].sf==1)&&(ts[user]==2)){
for(j=1;j<=pi[i].ps;j++){
if(pi[i].pa[j]=='J'){
ts[i]=2;sp(i,j);
if(wu_w(i))return false;
else return true;
}
}
}else if(pi[i].sf==2&&(ts[user]==3||ts[user]==4)){
for(j=1;j<=pi[i].ps;j++){
if(pi[i].pa[j]=='J'){
ts[i]=2;sp(i,j);
if(wu_w(i))return false;
else return true;
}
}
}
}
return false;
}
bool wu(int beu){
int i,j;
if(ts[beu]==0||ts[beu]==1)return false;
if(ts[beu]!=0&&ts[beu]!=1){
for(j=1;j<=pi[beu].ps;j++){
if(pi[beu].pa[j]=='J'){
sp(beu,j);
if(wu_w(beu))return false;
else return true;
}
}
}
for(i=beu+1;i!=beu;i++,i=(i-1)%n+1){
if((pi[i].sf==0||pi[i].sf==1)&&(ts[beu]==3||ts[beu]==4)){
for(j=1;j<=pi[i].ps;j++){
if(pi[i].pa[j]=='J'){
if(ts[i]!=4)ts[i]=3;
sp(i,j);
if(wu_w(i))return false;
else return true;
}
}
}
else if(pi[i].sf==2&&ts[beu]==2){
for(j=1;j<=pi[i].pa[j];j++){
if(pi[i].pa[j]=='J'){
ts[i]=2;sp(i,j);
if(wu_w(i))return false;
else return true;
}
}
}
}
return false;
}
void chu(int nu){
int i,j;mp(nu);bool cs=false;
for(i=1;i<=pi[nu].ps;i++){
if(pi[nu].pa[i]=='P'){
if(pi[nu].xl<4){
sp(nu,i);
pi[nu].xl++;
i--;
}
}
else if(pi[nu].pa[i]=='Z'){
sp(nu,i);
if(!pi[nu].zb){
pi[nu].zb=true;
i=0;
}else i--;
}else if(pi[nu].pa[i]=='K'){
if(pi[nu].sf==0){
int t;
for(j=2;j<=n;j++)if(zt[j]){t=j;break;}
if((ts[t]==1||ts[t]==2)&&(!cs||pi[i].zb)){
sp(1,i);facek(1,t);cs=true;i--;
}
}else if(pi[nu].sf==1){
int t=-1;
for(j=nu+1;j!=nu;j++){j=(j-1)%n+1;if(zt[j]){t=j;break;}}
if((t!=-1)&&ts[t]==2&&(!cs||pi[i].zb)){
sp(nu,i);facek(nu,t);cs=true;ts[nu]=3;i--;
}
}else if(pi[nu].sf==2){
int t1;
for(j=nu+1;;j++){j=(j-1)%n+1;if(zt[j]){t1=j;break;}}
if(ts[t1]==4&&(!cs||pi[nu].zb)){sp(nu,i);facek(nu,t1);cs=true;ts[nu]=2;i--;}
else if(ts[t1]==3&&(!cs||pi[nu].zb)){sp(nu,i);facek(nu,t1);cs=true;ts[nu]=2;i--;}
}
}
if(pi[nu].pa[i]=='F'){
if(pi[nu].sf==2){
sp(nu,i);ts[nu]=2;i=0;
if(!(wu(1)))jue(nu,1);
}else if(pi[nu].sf==1){
int t=-1;
for(j=nu+1;j!=nu;j++){j=(j-1)%n+1;if(ts[j]==2){t=j;break;}}
if(t!=-1){
ts[nu]=3;sp(nu,i);i=0;
if(!(wu(t)))jue(nu,t);
}
}else if(pi[nu].sf==0){
int t=-1;
for(j=nu+1;j!=nu;j=(j)%n+1){if(ts[j]==2||ts[j]==1){t=j;break;}}
if(t!=-1){
sp(nu,i);i=0;
if(!(wu(t)))jue(nu,t);
}
}if(!zt[nu])break;
}
else if(pi[nu].pa[i]=='N'){
for(j=nu+1;j!=nu;j++){
j=(j-1)%n+1;
if(!(wu(j)))nan(nu,j);
}sp(nu,i);i--;
}else if(pi[nu].pa[i]=='W'){
for(j=nu+1;j!=nu;j++){
j=(j-1)%n+1;
if(!(wu(j)))wan(nu,j);
}sp(nu,i);i--;
}
}
}
void xd(){
int i=0;
while(1){
i++;
i=(i-1)%n+1;
if(zt[i])chu(i);
}
}
int main(){
scanf("%d%d",&n,&m);
getchar();
int i,j;string tts;
for(i=1;i<=n;i++){
getline(cin,tts,'\n');
if(tts[0]=='M')pi[i].sf=0;
else if(tts[0]=='Z')pi[i].sf=1;
else pi[i].sf=2,fnu++;
pi[i].ps=4;zt[i]=true;pi[i].xl=4;pi[i].zb=false;
pi[i].pa[1]=tts[3];pi[i].pa[2]=tts[5];
pi[i].pa[3]=tts[7];pi[i].pa[4]=tts[9];
}ts[1]=4;
for(i=1;i<=m;i++)cin>>me[i];
if(fnu==0){
out();return 0;
}
xd();
return 0;
}
5 2000
MP K K P P
FP D K D K
ZP P P D K
FP D K P D
FP P K P P
D D D P D K P P D D P D D K P P D P D P P K K P D P K D P P D Z P P P K P P D P K K Z K K P D K K P D Z K D P D P K P P K K D P P K P P P P K P K P D K K D P D K D P K Z K K K K D D K K Z P K D D P K P D K Z K P P P K D P K P K K P K Z D K Z K D P P K D P K K D D D D P K D K D K Z D K D P P Z D P K P P P K D D D D K P K K D D K D K P D P D D K D P K P P K P P D P K P K K K D P K D D P K K K K P K D Z K K P K P Z K D P P P P D P P Z P K K P D K K D D Z D K P P D P D Z P D P Z K K P K Z D D D D P P P K P D P D K K Z P K Z D K D D P K K D K K P D K P K K D P K P Z D P P P P P D P K K D Z K D P D K K P Z P P D D K K Z P D P P D P D P D P P D K P D P P Z K K D D D K K K Z P K P D P K K D P K D P D D K D D P K K P P K K P K D P K P K P D D D P D K D P Z D P K D D D K D D Z D P K D K K P D D K K D P P K D D K K K P P P D K P Z P P P P K P D D K D D K D K P K P P K Z K P D K P K D K K P Z K D P P K P K P P K K K D K P K P P K D K K Z K P P D D K K P D P D D P K K P D P P K P D K P P K K P D D P K P K P P K P P K K D D D D P K P P Z D K K D K P P P K K P P K D K D K P D Z D K P K P P P K D Z D Z P K K D K D K P P D P P P P D D K P D D D K P D D D K K K K D D D P P K P P K P D P P P D P P D D P P Z Z P K D K D K K K K P K K D D P K K K K K P D K K K P K Z P D K P P K P K D K P D K P K D K D P K K Z P K P Z D D P K D D D K D K D P D D K P D K Z P K P D K Z D K D D P D D D P D D P P P D K D K D P Z P D D D D P D D D D K K P D D P D K D D P P D D K K P K D P P D Z K P D K K D P K K Z P D D P K P D K Z D P P D P K P D P P Z P P P K D P D Z P K P P K D K D K Z K K D P P D D D K P D K D P K D D K K P P D P P P K D P K D P D P D Z D K D P D K D P P K D P K D K K K P Z K D P Z P K P P K D K K D K P D P P P D K P K P D P P D D P P Z P Z K P P D D P P P K P K Z P D D D K P P P D K P K D D K D P D K Z P D P D P D P K P P P K D D D P K P P P D K K D K K K Z K P K K K K P K K P Z D D P D P D D P P D D D D K K D K K P K K P K K P D D D P P K D D K K P K K Z D P D K K K D P P P K P K P P D P D D K P P K D K K K P P K K D P D P D D P D P P K P P K D K D P P D K K P P P P P P P D P K D D K D P K D P P P D D K K Z P Z K D P D P K P K D D D Z D P K K K K K P P D K D D P P P P K K P K K D D P D D D D K P P P K K D P D P K K D P P Z D P P D D K P Z K P P P P D K D P D Z K Z K Z D D K P D D P D P D K K P K K P D P D K P P D P P D K K D K P K D K P D D D D D P K P D D K D P P P K D Z K K P D K D P P D D P P D K K P P P P P D K P K D K K D P D D P P D Z D P K K K K P Z K P P Z D P P P K K D K K P D K P D P P K D K P D K K K K D P K K Z D D D P P D P Z K D P P D P K P P Z D K K P D K P P Z D D K K P D D P K K D P K K D Z D P Z D P K D P Z K P K D K P D D D D P P Z K D D D P P K D D K D P P K K Z K D D P K K D P K P D D P P K P P D D P P K P K P P K Z K Z P D P D P D P P D P K K P P P K K P K P K K K D P D P P D P K D P Z D P K K P P K K D P K P D K D K D D K D P K D P P K K D K K D P K P K Z D P D P P K D P K K K P P P K P D P P K D D D K D K K P P K Z D K K K P K K P K K D D K P K D K K P P K D P K D K P K K P K K D Z P P P D D P D D P P P P K D K P K K P K P K P K K K P K P D K K K K P Z D K P D P D K P K P P P P K D P D P Z K Z D K D D K P P P D P D D K K Z D Z Z P D K P D D P K P D D K P P D K Z P P Z D P D K K K D D D D P D K K P K K K P K P D P K P K D D K K P D P D P Z P P K P K D K D D P K P D K P D K D K D P K D P D P K K Z P P K D P D K D D K D K D K K K P Z P P P D Z D D K P Z P D K K P K D P D D D D D P P K D K K K P D P D P D P K K P K K K P D D K D D D P P K K K P K K P D P D K D K P Z K P K D K P K P K K D K D P D Z P Z P K D K D K D P K D D K D P D D K K D P D D P D D P P K P K D P K D D D D D Z K K K P D P Z D K K K K P D K K K D D P D K D P K P P P P P D D K P P K P D K P K P D P D D K K P K P P Z D P D P D P K P K D P D K P P P P P D D K D P D D D D P P D K D P D K P K P P P K P P K P P P P D K P D K P K K P P D P D K P Z K D D P P P K P P P P K Z D D P D K P K K K Z D D Z D D K D P D K D K K D K D P P D K P K D K P P K D D D D K D P D D Z K
FP
DEAD
D K D K D P D K K P P P K K D D P K P P K K K P K D P D D K D P K D D K D P K D P K K P P D K D P P K K P D K P P K K P D P D K D D P K K D P K D P D D D K P K D K D D P P D K P K K P P D P K K P P K D D K D K D P K P K P P D K D D D P P K K K K P K P K K K D D K K P D D P D D K D K D K D K P K P D P K D D D D K D P K D P K P D K K P P K P P P P D P K K P K K P P P K P D P K P P D K P K D P P P P P K D P D D K P P K D D P D P P P P K P D D K D K D D P P D P P P K K D P P K P K P K K D D K D K P K K D D D D P K K P K D D K P D P K P D K P K D P P P K P K P P P P K P K K P K P D K D P K K K K P D P P P D K P P P D D K P D K P K P D K D K D D K D P K P K P D D D K P K P P K P D D P D K D K K K K D K D D K P P P P K P D P P P P P D P D P P D D D D D K P K D K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K
P P D K D K P P D P P K K P P D P K D K K P K K P K P P K P K P P D D K D P K P K K K P K D P K D D D D P K D D K D P D P P P D P D K K K D D P D P D K D K K P D D P K D K K P P P P K D K K P P K P P D P K P K D P P K D P P P D D P P P P P K K P D K D K P D D P D D K D P P D K D P P P P K D K D P D K K P K P K D P D D K P K P D D P K P K D K K P P P P K P D D D K K P K D P K P P P K D P K D K D P P P K D D P D K P D K P K D D D K D D K K P P P K P K K D P D P P D P K K D P D K D K D D P P K P K P K D P D D P K K P D P P P P K P K P K P K K D K K D K K K D K K K P P K P D P K P K K P K P K P D P D D D D P K P K P P K K P P D P D K K D K D P D K P K K D K K K K D K D K D D P P K K P P D D P K P D P D D P D K D K P K K P P D P K P D D K D D K D P K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K
D K P D P P D P K D P P D K P K P P K D K K D D P P K D P D K D D D K P P P K K K K P P D P D D D D K D P P K P P K D D D P D P P K P K K K K P D P D D D D K K P P D K K P K K K D K K P D P K P K P K P P P K P P K K D D D D D P K D P K D K D D K P P P K K P K D K D D P P D D K P P D D K D P P K K K D K P D P D K D K K P P P P P D P D P D P K P D P K D K K K D P D D P K D D D K K P K D P D P P K P K D P D P D D P K D K K P P K D K P D P K P P K D P D K P D K D P P D D D K K P P P D K K D P D P D K D D D K P P P D D D D D P K P D P K P K K D P K D D K D K P D P P D D D K D D K D K K D D K K P K D K P P D P D D K K P P D D K D P D P D K D K P K K D P P D D K K K P D D P D P K K D K D P D D D D D D P K K K P P K D D P D D K D P D P P P D K K P P P K P D K K D P P D D K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K
P P P D D D P P P D P P P P P P P D D P D D D P P D P P D P P D P D P D P D P P P D P D P D P P P P D D P P P D D D P D D P P D D P P D D P P P P P D D D P P D D D P P D D D D D D D P P D P P P P P P D P D P P P P D P P P D P D D P D P D D D D P D D D D D P P P D P D D P P D D P D P D D P P P D D D P P D P D D P P D P P P P P P D P D P P P P P D D P P P D P P P D P D P D P D P P P D P P D P P P P D P D P D D P D P D P P D P D D D D P P P D D D P D P P P D D P D D D P P D D D D P P P D P D P P D D D P P P P D P P P D