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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <vector>
#include <set>
#include <tuple>

class BiGraph
{
public:
	struct Node
	{
		std::set<int> E,E2;
		int in() const
		{
			return E2.size();
		}
		int out() const
		{
			return E.size();
		}
		int n;
	};
	
	struct Cmp
	{
		bool operator()(const Node* a, const Node* b) const
		{
			if (a->out() == b->out())
				return a->in() < b->in();
			return a->out() > b->out();
		}
	};
	
	BiGraph(int n)
	{
		this->n = n;
		
		L.assign(n,{{},{},0});
		R.assign(n,{{},{},0});
		
		for (int i=0;i<n;++i)
		{
			L[i].n = i;
			R[i].n = i;
		}
	}
	
	void AddL(int a, int b)
	{
		L[a].E.insert(b);
		R[b].E2.insert(a);
	}
	
	void AddR(int a, int b)
	{
		R[b].E.insert(a);
		L[a].E2.insert(b);
	}
	
	void Init()
	{
		Lit.assign(n,Lset.end());
		Rit.assign(n,Rset.end());
		for (int i=0;i<n;++i)
		{
			Lit[i] = Lset.insert(&L[i]);
			Rit[i] = Rset.insert(&R[i]);
		}
	}
	
	void RemoveL()
	{
		auto it = Lset.begin();
		Node *node = *it;
		Lset.erase(it);
		
		for (int v : node->E)
		{
			Rset.erase(Rit[v]);
			R[v].E2.erase(node->n);
			Rit[v] = Rset.insert(&R[v]);
		}
		for (int v : node->E2)
		{
			Rset.erase(Rit[v]);
			R[v].E.erase(node->n);
			Rit[v] = Rset.insert(&R[v]);
		}
	}
	
	void RemoveR()
	{
		auto it = Rset.begin();
		Node *node = *it;
		Rset.erase(it);
		
		for (int v : node->E)
		{
			Lset.erase(Lit[v]);
			L[v].E2.erase(node->n);
			Lit[v] = Lset.insert(&L[v]);
		}
		for (int v : node->E2)
		{
			Lset.erase(Lit[v]);
			L[v].E.erase(node->n);
			Lit[v] = Lset.insert(&L[v]);
		}
	}
	
	int Result()
	{
		auto it = Lset.begin();
		Node *node = *it;
		if (node->E.size() > 0)
			return 1;
		else if (node->E2.size() > 0)
			return -1;
		else
			return 0;
	}
	
	std::multiset<Node*,Cmp> Lset;
	std::multiset<Node*,Cmp> Rset;
	int n;
	std::vector<Node> L;
	std::vector<Node> R;
	std::vector<std::multiset<Node*,Cmp>::iterator> Lit;
	std::vector<std::multiset<Node*,Cmp>::iterator> Rit;
};

void Solve()
{
	int n,m;
	std::cin >> n >> m;
	
	BiGraph G(n);
	for (int i=0;i<m;++i)
	{
		int a,b;
		char w;
		std::cin >> a >> w >> b;
		--a;
		--b;
		if (w == '>')
			G.AddL(a,b);
		else
			G.AddR(a,b);
	}
	G.Init();
	
	for (int i=1;i<n;++i)
	{
		G.RemoveR();
		G.RemoveL();
	}
	switch (G.Result())
	{
		case 1:
			std::cout << "WYGRANA\n";
			break;
		case 0:
			std::cout << "REMIS\n";
			break;
		case -1:
			std::cout << "PRZEGRANA\n";
			break;
	}
}

int main()
{
	int T;
	std::cin >> T;
	while (T--)
		Solve();
}