#include<bits/stdc++.h>
using namespace std;
#define DAY 1
#define NIGHT 1
bool f;
bool undid;
int t;
int n;
int death;
int m;
int i;
int i2;
vector<int> ReturnSave;
vector<int> ReturnKill;
struct node {
int Identity;
int DeathTime;
int SkillUsed;
int Save;
int Kill;
int HunterDied;
int HunterKilled;
};
struct act {
int Skill;
int Player1;
int Player2;
};
node player[25];
act RecentAct[25];
void Undo() {
if (undid)return ;
for (int k = 1; k <= i2; k++) player[RecentAct[k].Player2].DeathTime = 0;
for (auto k : ReturnSave)player[k].Save = 0;
for (auto k : ReturnKill)player[k].Kill = 0;
for (int k = 1; k <= n; k++) {
player[k].HunterDied = 0;
player[k].HunterKilled = 0;
}
undid = 1;
return;
}
int main() {
cin >> t >> n;
for (i = 1; i <= n; i++) {
cin >> player[i].Identity;
player[i].HunterDied = 0;
player[i].HunterKilled = 0;
}
for (i = 1; i <= t; i++) {
for (int k = 1; k <= n; k++) {
player[k].SkillUsed = 0;
player[k].HunterDied = 0;
player[k].HunterKilled = 0;
}
if (NIGHT) {
ReturnSave.clear();
ReturnKill.clear();
undid = 0;
f = 0;
cin >> m;
for (i2 = 1; i2 <= m; i2++) {
cin >> RecentAct[i2].Skill >> RecentAct[i2].Player1 >> RecentAct[i2].Player2;
if (f)continue;
else {
if (RecentAct[i2].Skill == 0) {
if (player[RecentAct[i2].Player1].DeathTime > 0) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].Identity != 1) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player2].DeathTime > 0) {
f = 1;
Undo();
continue;
}
if (RecentAct[i2].Player2 == RecentAct[i2].Player1) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].SkillUsed) {
f = 1;
Undo();
continue;
}
if (RecentAct[i2].Player1 < 1 || RecentAct[i2].Player1 > n || RecentAct[i2].Player2 < 1 || RecentAct[i2].Player2 > n) {
f = 1;
Undo();
continue;
}
player[RecentAct[i2].Player2].DeathTime = i;
player[RecentAct[i2].Player1].SkillUsed = 1;
if (player[RecentAct[i2].Player2].Identity == 3) {
player[RecentAct[i2].Player2].HunterDied++;
}
} else if (RecentAct[i2].Skill == 1) {
if (player[RecentAct[i2].Player1].DeathTime > 0) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].Identity != 4) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player2].DeathTime > 0) {
f = 1;
Undo();
continue;
}
if (RecentAct[i2].Player2 == RecentAct[i2].Player1) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].SkillUsed) {
f = 1;
Undo();
continue;
}
if (RecentAct[i2].Player1 < 1 || RecentAct[i2].Player1 > n || RecentAct[i2].Player2 < 1 || RecentAct[i2].Player2 > n) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].Kill) {
f = 1;
Undo();
continue;
}
player[RecentAct[i2].Player2].DeathTime = i;
player[RecentAct[i2].Player1].SkillUsed = 1;
player[RecentAct[i2].Player1].Kill = 1;
ReturnKill.push_back(RecentAct[i2].Player1);
if (player[RecentAct[i2].Player2].Identity == 3) {
player[RecentAct[i2].Player2].HunterDied++;
}
} else if (RecentAct[i2].Skill == 2) {
if (player[RecentAct[i2].Player1].DeathTime > 0 && RecentAct[i2].Player2 != RecentAct[i2].Player1) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].Identity != 4) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player2].DeathTime != i) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].SkillUsed) {
f = 1;
Undo();
continue;
}
if (RecentAct[i2].Player1 < 1 || RecentAct[i2].Player1 > n || RecentAct[i2].Player2 < 1 || RecentAct[i2].Player2 > n) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].Save) {
f = 1;
Undo();
continue;
}
player[RecentAct[i2].Player2].DeathTime = 0;
player[RecentAct[i2].Player1].SkillUsed = 1;
player[RecentAct[i2].Player1].Save = 1;
ReturnSave.push_back(RecentAct[i2].Player1);
if (player[RecentAct[i2].Player2].Identity == 3) {
player[RecentAct[i2].Player2].HunterDied--;
player[RecentAct[i2].Player2].HunterKilled = 0;
}
} else if (RecentAct[i2].Skill == 3) {
if (player[RecentAct[i2].Player1].DeathTime != i) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].Identity != 3) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player2].DeathTime > 0) {
f = 1;
Undo();
continue;
}
if (RecentAct[i2].Player2 == RecentAct[i2].Player1) {
f = 1;
Undo();
continue;
}
if (player[RecentAct[i2].Player1].SkillUsed) {
f = 1;
Undo();
continue;
}
if (RecentAct[i2].Player1 < 1 || RecentAct[i2].Player1 > n || RecentAct[i2].Player2 < 1 || RecentAct[i2].Player2 > n) {
f = 1;
Undo();
continue;
}
player[RecentAct[i2].Player2].DeathTime = i;
player[RecentAct[i2].Player1].SkillUsed = 1;
player[RecentAct[i2].Player1].HunterKilled++;
}
}
}
if (!f) {
for (int k = 1; k <= n; k++) {
if (player[k].Identity == 3 && player[k].HunterDied > 0) {
if (player[k].DeathTime == i && player[k].HunterKilled != 1) {
f = 1;
Undo();
break;
}
}
}
}
}
if (DAY) {
death = 0;
if (f)cout << "Wrong\n";
else {
for (int k = 1; k <= n; k++) {
if (player[k].DeathTime == i) {
death++;
}
}
if (death == 0)cout << "Safe";
else {
cout << death << " ";
for (int k = 1; k <= n; k++) {
if (player[k].DeathTime == i) {
cout << k << " ";
}
}
}
cout << endl;
}
}
}
}