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
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef struct Vertex{
	int id;
	set<int> outgoing;
	set<int> incoming;
	bool removed;
}Vertex;

typedef vector<Vertex> Graph;

void init_graph(Graph & g, int n){
	for(int i=0; i<n;i++){
		Vertex v1;
		v1.id = i;
		v1.removed = false;
		g.push_back(v1);
	}
}
void eliminate(Graph &graph, int n, Graph &enemy_graph){
	
	unsigned int max_out=0,min_in=0xffffffff;
	int to_remove=-1;
	for(int i=0; i<n; i++){
		if(graph[i].removed) continue;
		if(max_out<graph[i].outgoing.size())
			max_out = graph[i].outgoing.size();
	}
	for(int i=0; i<n; i++){
		if(graph[i].removed || graph[i].outgoing.size()<max_out) continue;
		if(min_in>graph[i].incoming.size()){
			min_in = graph[i].incoming.size();
			to_remove=i;
		}
	}
	graph[to_remove].removed=true;
	for(set<int>::iterator it = graph[to_remove].incoming.begin(); it != graph[to_remove].incoming.end();++it){
		enemy_graph[*it].outgoing.erase(to_remove);
	}
	for(set<int>::iterator it = graph[to_remove].outgoing.begin(); it != graph[to_remove].outgoing.end();++it){
		enemy_graph[*it].incoming.erase(to_remove);
	}
}
int test(){
	int n,m,a,b;
	char c;
	cin>>n>>m;
	Graph bajtek, bitek;
	init_graph(bajtek,n);
	init_graph(bitek,n);
	for(int i = 0; i < m ; i++){
		cin>>a>>c>>b;
		a--;b--;
		if(c == '<'){
			bajtek[a].incoming.insert(b);
			bitek[b].outgoing.insert(a);
		} else {
			bajtek[a].outgoing.insert(b);
			bitek[b].incoming.insert(a);
		}
	}
	for(int i=0; i < n-1 ; i++){
		eliminate(bitek,n,bajtek);
		eliminate(bajtek,n,bitek);
	}
	for(Graph::iterator it = bajtek.begin(); it != bajtek.end();++it){
		if(it->removed)continue;
		for(Graph::iterator itt = bitek.begin(); itt != bitek.end();++itt){
			if(itt->removed)continue;
			if(it->outgoing.count(itt->id)==1)return 1;
			if(it->incoming.count(itt->id)==1)return -1;
			else return 0;
		}
	}
	return 0;
}

int main(){
	#ifndef HESOYAM
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	#endif
	int T;
	cin>>T;
	while(T-->0){
		int ans = test();
		switch(ans){
			case 1:
				cout<<"WYGRANA\n";
				break;
			case 0:
				cout<<"REMIS\n";
				break;
			case -1:
				cout<<"PRZEGRANA\n";
				break;
			default:
			break;
		};
	}
	return 0;	
}