#include <iostream>
using namespace std;
struct node {
int life,ccnt,last,next;
bool z;
char cards[2010];
string id;
};
int m,fcnt;
bool gend;
char cheap[2010];
string zid[20];
node pigs[20];
void G(int from,int cnt) {
for (int i = 1;i <= cnt;i++) {
if (!m) {
m++;
}
pigs[from].cards[++pigs[from].ccnt] = cheap[m];
m--;
}
}
void E(int from,int to) {
for (int i = 1;i <= pigs[to].ccnt;i++) {
if (pigs[to].cards[i] == 'P') {
pigs[to].cards[i] = 'V';
pigs[to].life++;
return ;
}
}
if (to == 1) {
gend = true;
return ;
}
if (pigs[to].id == "FP") {
fcnt--;
}
if (!fcnt) {
gend = true;
return ;
}
if (pigs[to].id == "FP") {
G(from,3);
}
if (pigs[from].id == "MP" && pigs[to].id == "ZP") {
pigs[from].ccnt = 0;
pigs[from].z = false;
}
pigs[pigs[to].next].last = pigs[to].last;
pigs[pigs[to].last].next = pigs[to].next;
}
void B(int from,int to) {
pigs[to].life--;
if (pigs[to].life <= 0) {
E(from,to);
}
}
void K(int from,int to) {
for (int i = 1;i <= pigs[to].ccnt;i++) {
if (pigs[to].cards[i] == 'D') {
pigs[to].cards[i] = 'V';
return ;
}
}
B(from,to);
}
bool J(int from,int to,bool type) {
int i = from;
while (1) {
if (type) {
if (pigs[i].id == zid[to] || (pigs[i].id == "MP" && zid[to] == "ZP") || (pigs[i].id == "ZP" && zid[to] == "MP")) {
for (int j = 1;j <= pigs[i].ccnt;j++) {
if (pigs[i].cards[j] == 'J') {
pigs[i].cards[j] = 'V';
zid[i] = pigs[i].id;
return !J(i,from,false);
}
}
}
}
else {
if (((pigs[i].id == "MP" || pigs[i].id == "ZP") && zid[from] == "FP") || (pigs[i].id == "FP" && (zid[from] == "MP" || zid[from] == "ZP"))) {
for (int j = 1;j <= pigs[i].ccnt;j++) {
if (pigs[i].cards[j] == 'J') {
pigs[i].cards[j] = 'V';
zid[i] = pigs[i].id;
return !J(i,from,false);
}
}
}
}
i = pigs[i].next;
if (i == from) {
break;
}
}
return false;
}
void F(int from,int to) {
if (J(from,to,true)) {
return ;
}
if (zid[from] == "MP" && pigs[to].id == "ZP") {
B(from,to);
return ;
}
int idx1 = 1,idx2 = 1;
while (true) {
while (pigs[to].cards[idx2] != 'K' && idx2 <= pigs[to].ccnt) {
idx2++;
}
if (idx2 > pigs[to].ccnt) {
B(from,to);
return ;
}
else {
pigs[to].cards[idx2] = 'V';
}
while (pigs[from].cards[idx1] != 'K' && idx1 <= pigs[from].ccnt) {
idx1++;
}
if (idx1 > pigs[from].ccnt) {
B(to,from);
return ;
}
else {
pigs[from].cards[idx1] = 'V';
}
}
}
void N(int from) {
for (int i = pigs[from].next;i != from;i = pigs[i].next) {
if (J(from,i,true)) {
continue;
}
bool c = false;
for (int j = 1;j <= pigs[i].ccnt;j++) {
if (pigs[i].cards[j] == 'K') {
pigs[i].cards[j] = 'V';
c = true;
break;
}
}
if (!c) {
B(from,i);
if (pigs[i].id == "MP" && zid[from] == "UP") {
zid[from] = "LP";
}
}
if (gend) {
return ;
}
}
}
void W(int from) {
for (int i = pigs[from].next;i != from;i = pigs[i].next) {
if (J(from,i,true)) {
continue;
}
bool c = false;
for (int j = 1;j <= pigs[i].ccnt;j++) {
if (pigs[i].cards[j] == 'D') {
pigs[i].cards[j] = 'V';
c = true;
break;
}
}
if (!c) {
B(from,i);
if (pigs[i].id == "MP" && zid[from] == "UP") {
zid[from] = "LP";
}
}
if (gend) {
return ;
}
}
}
int main() {
int n;
cin >> n >> m;
for (int i = 1;i <= n;i++) {
cin >> pigs[i].id;
if (pigs[i].id == "FP") {
fcnt++;
}
for (int j = 1;j <= 2000;j++) {
pigs[i].cards[j] = 'V';
}
for (int j = 1;j <= 4;j++) {
cin >> pigs[i].cards[j];
}
pigs[i].life = pigs[i].ccnt = 4;
pigs[i].last = i - 1;
pigs[i].next = i + 1;
zid[i] = "UP";
}
pigs[1].last = n;
pigs[n].next = 1;
zid[1] = "MP";
for (int i = 1;i <= m;i++) {
cin >> cheap[m - i + 1];
}
if (fcnt) {
for (int i = 1;;i = pigs[i].next) {
G(i,2);
bool killed = false;
for (int j = 1;j <= pigs[i].ccnt;j++) {
if (pigs[i].cards[j] == 'V') {
continue;
}
if (pigs[i].life <= 0) {
break;
}
if (pigs[i].cards[j] == 'P') {
if (pigs[i].life == 4) {
continue;
}
pigs[i].cards[j] = 'V';
pigs[i].life++;
}
else if (pigs[i].cards[j] == 'K') {
if (killed && !pigs[i].z) {
continue;
}
if (pigs[i].id == "MP" && zid[pigs[i].next] != "FP" && zid[pigs[i].next] != "LP") {
continue;
}
else if (pigs[i].id == "ZP" && zid[pigs[i].next] != "FP") {
continue;
}
else if (pigs[i].id == "FP" && zid[pigs[i].next] != "MP" && zid[pigs[i].next] != "ZP") {
continue;
}
pigs[i].cards[j] = 'V';
K(i,pigs[i].next);
killed = true;
zid[i] = pigs[i].id;
}
else if (pigs[i].cards[j] == 'F') {
if (pigs[i].id == "FP") {
pigs[i].cards[j] = 'V';
F(i,1);
zid[i] = pigs[i].id;
j = 0;
}
else {
for (int k = pigs[i].next;k != i;k = pigs[k].next) {
if ((pigs[i].id == "MP" && (zid[k] == "FP" || zid[k] == "LP")) || (pigs[i].id == "ZP" && zid[k] == "FP")) {
pigs[i].cards[j] = 'V';
F(i,k);
zid[i] = pigs[i].id;
j = 0;
break;
}
}
}
}
else if (pigs[i].cards[j] == 'N') {
pigs[i].cards[j] = 'V';
N(i);
j = 0;
}
else if (pigs[i].cards[j] == 'W') {
pigs[i].cards[j] = 'V';
W(i);
j = 0;
}
else if (pigs[i].cards[j] == 'Z') {
pigs[i].cards[j] = 'V';
pigs[i].z = true;
j = 0;
}
if (gend) {
break;
}
}
if (gend) {
break;
}
}
}
if (pigs[1].life > 0) {
cout << "MP\n";
}
else {
cout << "FP\n";
}
for (int i = 1;i <= n;i++) {
if (pigs[i].life <= 0) {
cout << "DEAD\n";
}
else {
for (int j = 1;j <= pigs[i].ccnt;j++) {
if (pigs[i].cards[j] != 'V') {
cout << pigs[i].cards[j] << ' ';
}
}
cout << '\n';
}
}
return 0;
}