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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#include <bitset>
#include <sstream>
//#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std ;
using namespace __gnu_cxx ;
typedef long long LL ;
typedef pair<int,int> PII ;
typedef vector<int> VI ;
const int INF = 1e9+100 ;
const LL INFLL = (LL)INF*INF ;
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(c) c.begin(),c.end()
#define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i)
#define CLEAR(t) memset(t,0,sizeof(t))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second

template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;}
template<class T>           ostream& operator<<(ostream &s, const vector<T>   &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T>           ostream& operator<<(ostream &s, const set<T>      &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; }

template<class T1, class T2> T1 conv(const T2 &x) {
	stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ;
}

template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; }
void _debug(bool, const char* s, const char*) {
	for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ;
}
template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) {
	bool o=0, str=(*s=='"') ;
	for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ;
	for(s++;*s && *s==' '; s++) ;
	if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ;
	_debug(printName, s, y...) ;
}

template<class T>             void _coord(const T &x)         { cerr << "[" << x << "]" ;             }
template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; }

template<class T, class I>             void _val(T &t, const I &i)         { cerr << t[i] ; }
template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; }

#ifndef LOCAL
	#define CERR(...)
	#define DEBUG(...)
	#define DARRAY(...)
	#define DFLAG 0
#else
	#define CERR(...)     if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__)
	#define DEBUG(...)    if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__)	
	#define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; }
	#define DFLAG 0
#endif

////////////////////////////////////////////////////////////////////////////////

struct node {
	VI neight ;
	int degree ;
	bool removed ;
	
	void clear() {
		degree=0 ;
		removed=false ;
		neight.clear() ;
	}
	node() {
		clear() ;
	}
	void insert(int u) {
		neight.PB(u) ;
		degree++ ;
	}
} ;

const int MAXN = 100100 ;	
node Ga[MAXN], Gb[MAXN] ;

void clearAllGraphs(int n) {
	for(int i=1 ; i<=n ; i++) {
		Ga[i].clear() ;
		Gb[i].clear() ;
	}
}

///////////////////////////////////////////////////////////

struct queueElement {
	int u, degree ;
	bool isForbidden ;
	
	queueElement(int uu, int ddeg, bool isF=false) :
		u(uu), degree(ddeg), isForbidden(isF) {}
} ;
struct sortA {
	bool operator()(const queueElement &x, const queueElement &y) {
		if(x.isForbidden != y.isForbidden)
			return x.isForbidden > y.isForbidden ;
		else if(x.degree != y.degree)
			return x.degree > y.degree ;
		else
			return x.u > y.u ;
	}
} ;
struct sortB {
	bool operator()(const queueElement &x, const queueElement &y) {
		if(x.degree != y.degree)
			return x.degree < y.degree ;
		else if(x.isForbidden != y.isForbidden)
			return x.isForbidden > y.isForbidden ;
		else
			return x.u < y.u ;
	}
} ;

///////////////////////////////////////////////////////////

template<class T1, class T2>
void process(T1 &Qa, T2 &Qb, node Ga[], node Gb[], bool withForbiddensRestriction, const char* desc) {
	while(1) {
		assert(!Qa.empty()) ;
		
		queueElement el = *(--Qa.end()) ; Qa.erase(--Qa.end()) ;
		CERR("sciaglem", el.u) ;
		bool hasNeight1 = false ;
		FOREACH(it, Ga[el.u].neight)
			hasNeight1 |= !Gb[*it].removed && Gb[*it].degree==1 ;
		
		if( ( withForbiddensRestriction && !el.isForbidden && hasNeight1)
		||	(!withForbiddensRestriction && !el.isForbidden && hasNeight1 && el.degree==1)
																							){
			Qa.insert(queueElement(el.u, Ga[el.u].degree, true)) ;
			CERR("niebezpieczne") ;
		}
		else {
			CERR("  ", desc, ": wybieram", el.u) ;
			Ga[el.u].removed=true ;
			Ga[el.u].degree=-1 ;
			FOREACH(it, Ga[el.u].neight) {
				if(Gb[*it].removed) continue ;
				
				for(int f=0 ; f<=1 ; f++) {
					auto pointer = Qb.find(queueElement(*it, Gb[*it].degree, f)) ;
					if(pointer != Qb.end()) {
						Qb.erase(pointer) ;
						Gb[*it].degree-- ;
						Qb.insert(queueElement(*it, Gb[*it].degree, f)) ;
						
						break ;
					}
				}
			}
			return ;
		}
	}
}

void showDegree(int n) {
	if(DFLAG) {
		for(int i=1 ; i<=n ; i++) cerr << Ga[i].degree << " " ;
		cerr << endl ;
		for(int i=1 ; i<=n ; i++) cerr << Gb[i].degree << " " ;
		cerr << endl ;
	}
}

// czy moze wygrac gracz A, ktory chce zostawic krawedz
bool canWin(int n, bool nextTurn) {
	set<queueElement, sortA> Qa ;
	set<queueElement, sortB> Qb ;
	for(int i=1 ; i<=n ; i++) {
		Qa.insert(queueElement(i, Ga[i].degree)) ;
		CERR("klade A", i, Ga[i].degree) ;
		Qb.insert(queueElement(i, Gb[i].degree)) ;
		CERR("klade B", i, Gb[i].degree) ;
	}
	
	showDegree(n) ;
	
	for(int moves=2*n-2 ; moves>0 ; moves--, nextTurn=!nextTurn) {
		if(nextTurn==1)
			process(Qb, Qa, Gb, Ga, false, "B") ;
		else
			process(Qa, Qb, Ga, Gb, true, "A") ;
		
		showDegree(n) ;
		DEBUG(Qa.size(), Qb.size()) ;
	}
	assert(Qa.size()==1 && Qb.size()==1) ;
	assert(Ga[Qa.begin()->u].degree == Gb[Qb.begin()->u].degree) ;
	DEBUG(Ga[Qa.begin()->u].degree) ;
	return Ga[Qa.begin()->u].degree ;
}

void heura(int n, int m) {
//	cerr << "heura " << n << " " << m << endl ;
	int a, b ;
	vector<PII> rel1, rel2 ;
	char c ;
	while(m--) {
		cin >> a >> c >> b ;
		if(c=='>')      rel1.PB(MP(a,b)) ;
		else if(c=='<') rel2.PB(MP(a,b)) ;
		else assert(false) ;
	}
	
	clearAllGraphs(n) ;
	FOREACH(it, rel1) {
		Ga[it->SE].insert(it->FI) ;
		Gb[it->FI].insert(it->SE) ;
	}
	if(canWin(n, 0)) {
		cout << "WYGRANA" << endl ;
		return ;
	}
	
	clearAllGraphs(n) ;
	FOREACH(it, rel2) {
		Ga[it->FI].insert(it->SE) ;
		Gb[it->SE].insert(it->FI) ;
	}
	if(canWin(n, 1))
		cout << "PRZEGRANA" << endl ;
	else
		cout << "REMIS" << endl ;
}

///////////////////////////////////////////////////////////////////////////////

const int maxn = 10 ;
short t[1<<maxn][1<<maxn] ;
short len[1<<maxn] ;

void brut(int n, int m) {
//	cerr << "brut " << n << " " << m << endl ;
	CLEAR(t) ;
	
	int a, b ;
	char c ;
	while(m--) {
		cin >> a >> c >> b ;
		a-- ; b-- ;
		if(c=='>')      t[1<<a][1<<b]=1 ;
		else if(c=='<') t[1<<a][1<<b]=-1 ;
		else assert(false) ;
	}
	for(int mask=1 ; mask<(1<<n) ; mask++)
		len[mask] = __builtin_popcount(mask) ;
	for(int mask1=1 ; mask1<(1<<n) ; mask1++) {
		for(int mask2=1 ; mask2<(1<<n) ; mask2++) {
			if(len[mask1]==1 && len[mask2]==1) continue ;
			if(len[mask1]==len[mask2]) {
				t[mask1][mask2]=-1 ;
				REP(i, n)
					if(mask2&(1<<i))
						t[mask1][mask2] = max(t[mask1][mask2], t[mask1][mask2^(1<<i)]) ;
			}
			else if(len[mask1]==len[mask2]+1) {
				t[mask1][mask2]=1 ;
				REP(i,n)
					if(mask1&(1<<i))
						t[mask1][mask2] = min(t[mask1][mask2], t[mask1^(1<<i)][mask2]) ;
			}
		}
	}
	int ret=t[(1<<n)-1][(1<<n)-1] ;
	if(ret==1) cout << "WYGRANA" << endl ;
	else if(ret==0) cout << "REMIS" << endl ;
	else cout << "PRZEGRANA" << endl ;
}

void __main() {
	int n, m ;
	cin >> n >> m ;
	if(n<=10) brut(n,m) ;
	else heura(n,m) ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	int tests ;
	cin >> tests ;
	while(tests--) {
		__main() ;
//		return 0 ;
	}
}