#include<bits/stdc++.h>
using namespace std;
const int N = 20;
vector<char> P;
int n, m, f;
int nxt[N], lst[N];
struct Pig {
string name;
vector<char> p;
int hp;
int z;
int t;
void throwcard(int& x) {
p.erase(p.begin() + x);
x--;
}
int search(char c) {
for (int i = 0; i < p.size(); i++) {
if (p[i] == c) {
throwcard(i);
return 1;
}
}
return 0;
}
void getcard(int x) {
while (x--) {
char c = P.front();
p.push_back(c);
P.erase(P.begin());
if (P.empty()) P.push_back(c);
}
}
}a[N];
void end() {
if (a[1].hp == 0) cout << "FP" << endl;
else cout << "MP" << endl;
for (int i = 1; i <= n; i++) {
if (a[i].hp == 0) cout << "DEAD" << endl;
else {
int len = a[i].p.size();
for (int j = 0; j < len; j++)
cout << a[i].p[j] << " ";
cout << endl;
}
}
exit(0);
}
int hostility(int x, int y) {
if (a[x].name == "MP" && (a[y].t == 2 || a[y].t == 3))
return 1;
if (a[x].name == "ZP" && a[y].t == 2)
return 1;
if (a[x].name == "FP" && a[y].t == 1)
return 1;
return 0;
}
int squire(int x, int y) {
if (a[x].name == "MP" && a[y].t == 1)
return 1;
if (a[x].name == "ZP" && a[y].t == 1)
return 1;
if (a[x].name == "FP" && a[y].t == 2)
return 1;
return 0;
}
void killed(int x, int y) {
if (a[y].name == "MP") end();
if (a[y].name == "FP") {
f--;
if (f == 0) end();
a[x].getcard(3);
}
else if (a[y].name == "ZP" && a[x].name == "MP") {
a[x].p.clear();
a[x].z = 0;
}
nxt[lst[y]] = nxt[y];
lst[nxt[y]] = lst[y];
}
void attack(int x, int y) {
a[y].hp--;
if (a[y].hp == 0) {
int t = a[y].search('P');
if (t == 0) killed(x, y);
else a[y].hp++;
}
}
int Impeccable(int s, int x) {
for (int i = s, j = 0; !j || i != s; i = nxt[i], j = 1) {
int t;
if (x == 0) t = hostility(i, s);
else t = squire(i, x);
if (t && a[i].search('J')) {
if (a[i].name == "FP") a[i].t = 2;
else a[i].t = 1;
return !Impeccable(i, 0);
}
}
return 0;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
a[i].name = s;
if (s[0] == 'F') f++;
for (int j = 0; j < 4; j++) {
char c;
cin >> c;
a[i].p.push_back(c);
}
nxt[i] = i + 1, lst[i] = i - 1;
a[i].hp = 4;
a[i].z = a[i].t = 0;
}
for (int i = 0; i < m; i++) {
char c;
cin >> c;
P.push_back(c);
}
nxt[n] = 1, lst[1] = n;
int now = 1;
a[1].t = 1;
if (f == 0) end();
while (true) {
int kill = 0;
a[now].getcard(2);
for (int i = 0; i < a[now].p.size() && a[now].hp; i++) {
if (a[now].p[i] == 'Z') {
a[now].z = 1;
a[now].throwcard(i);
i = -1;
}
else if (a[now].p[i] == 'P') {
if (a[now].hp == 4) continue;
a[now].hp++;
a[now].throwcard(i);
}
else if (a[now].p[i] == 'K') {
if (a[now].z || kill == 0) {
int t = nxt[now];
if (hostility(now, t)) {
if (a[now].name == "FP") a[now].t = 2;
else a[now].t = 1;
a[now].throwcard(i);
kill = 1;
if (!a[t].search('D')) attack(now, t);
}
}
}
else if (a[now].p[i] == 'F') {
int j;
if (a[now].name == "FP") j = 1;
else j = nxt[now];
for (; j != now; j = nxt[j]) {
if (hostility(now, j)) {
a[now].throwcard(i);
if (a[now].name == "FP") a[now].t = 2;
else a[now].t = 1;
if (Impeccable(now, j)) break;
if (a[j].name == "ZP" && a[now].name == "MP") {
attack(now, j);
break;
}
int t = 1;
while (true) {
if (t & 1) {
if (!a[j].search('K')) {
attack(now, j);
break;
}
}
else {
if (!a[now].search('K')) {
attack(j, now);
i = max(-1, i - 1);
break;
}
}
t ^= 1;
}
i = -1;
break;
}
}
}
else if (a[now].p[i] == 'N' || a[now].p[i] == 'W') {
char c = a[now].p[i], d;
if (c == 'N') d = 'K';
else d = 'D';
a[now].throwcard(i);
for (int j = nxt[now]; j != now; j = nxt[j]) {
if (!Impeccable(now, j) && !a[j].search(d)) {
if (a[j].name == "MP" && a[now].t == 0) a[now].t = 3;
attack(now, j);
}
}
i = -1;
}
}
now = nxt[now];
}
return 0;
}