1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <set>
#include <list>
#include <unordered_set>

using namespace std;

class Node
{
public:
	unordered_set<Node*> parents;
	unordered_set<Node*> children;

	bool marked = false;
};


Node X[100000], Y[100000];

typedef pair<Node&, int> para;

void RemoveMeFromMyParentChildren(unordered_set<Node*>& themWithChildren, unordered_set<Node*>& themWithOnlyParents, int& otherThem, Node* xToRemove)
{
	for (const auto& parent : xToRemove->parents) {
		parent->children.erase(xToRemove);
		if (parent->children.empty()) {
			themWithChildren.erase(parent);
			if (parent->parents.empty())
				otherThem++;
			else
				themWithOnlyParents.insert(parent);
		}
	}
}

void removeOne(unordered_set<Node*>& meWithChildren, unordered_set<Node*>& meWithOnlyParents, int& otherMe, unordered_set<Node*>& themWithChildren, unordered_set<Node*>& themWithOnlyParents, int& otherThem)
{
	if (!meWithChildren.empty())
	{
		auto beginIterator = meWithChildren.begin();
		auto xToRemove = *beginIterator;
		
		for (const auto& child : xToRemove->children) {
			child->parents.erase(xToRemove);
			if (child->children.empty() && child->parents.empty()) {
				themWithOnlyParents.erase(child);
				otherThem++;
			}
		}
		RemoveMeFromMyParentChildren(themWithChildren, themWithOnlyParents, otherThem, xToRemove);
		meWithChildren.erase(beginIterator);
	}
	else if (otherMe > 0)
	{
		otherMe--;
	}
	else
	{
		auto beginIterator = meWithOnlyParents.begin();
		auto xToRemove = *beginIterator;
		RemoveMeFromMyParentChildren(themWithChildren, themWithOnlyParents, otherThem, xToRemove);
		meWithOnlyParents.erase(beginIterator);		
	}
}

int main()
{
	int t, n, m;
	int a, b;
	char c;
	scanf("%d", &t);

	while (t-- > 0)
	{
		scanf("%d %d", &n, &m);

		for (int i = 0; i < n; ++i)
		{
			X[i].children.clear();
			X[i].parents.clear();

			Y[i].children.clear();
			Y[i].parents.clear();
		}

		int otherXs = 0;
		unordered_set<Node*> xsWithChildren;
		unordered_set<Node*> xsWithOnlyParents;
		int otherYs = 0;
		unordered_set<Node*> ysWithChildren;
		unordered_set<Node*> ysWithOnlyParents;

		for (int i = 0; i < m; ++i)
		{
			scanf("%d %c %d", &a, &c, &b);

			--a; --b;

			if (c == '<')
			{
				X[a].parents.insert(&Y[b]);
				Y[b].children.insert(&X[a]);
				ysWithChildren.insert(&Y[b]);
			}

			if (c == '>')
			{
				X[a].children.insert(&Y[b]);
				Y[b].parents.insert(&X[a]);
				xsWithChildren.insert(&X[a]);
			}
		}

		for (int i = 0; i < n; ++i) {
			const bool is_in = !X[i].children.empty();
			if (!is_in) {
				if (X[i].parents.empty())
					otherXs++;
				else
					xsWithOnlyParents.insert(&X[i]);
			}
		}

		for (int i = 0; i < n; ++i) {
			const bool is_in = !Y[i].children.empty();
			if (!is_in) {
				if (Y[i].parents.empty())
					otherYs++;
				else
					ysWithOnlyParents.insert(&Y[i]);
			}
		}

		for (int step = 0; step < n - 1; ++step)
		{
			removeOne(ysWithChildren, ysWithOnlyParents, otherYs, xsWithChildren, xsWithOnlyParents, otherXs);
			removeOne(xsWithChildren, xsWithOnlyParents, otherXs, ysWithChildren, ysWithOnlyParents, otherYs);
		}

		if (!ysWithChildren.empty())
			printf("PRZEGRANA\n");
		else if (!xsWithChildren.empty())
			printf("WYGRANA\n");
		else
			printf("REMIS\n");
	}
}