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
#include <cstdio>
#include <vector>
using namespace std;

int t, n, m, a, b;
char w;

struct talia;
void echo(talia *t);
void echo(const talia &t);

struct talia {
	int nr;
	int wins;
	int loses;
	vector<int> winWith, loseWith;
	const bool operator< (const talia &r) const {
        if (wins != r.wins) return wins > r.wins;
        else return loses < r.loses;
    }
    const bool operator> (const talia &r) const {
        if (wins != r.wins) return wins < r.wins;
        else return loses > r.loses;
    }
};

int p1heapIdx[100001], p2heapIdx[100001];
talia *p1[100001], *p2[100001];

void heap_down(talia *heap[], int heapIdx[], int n, int i) {
	int left = i * 2, right = i * 2 + 1;
	int smallest = i;
	if (left <= n && *heap[left] < *heap[smallest]) {
		smallest = left;
	}
	if (right <= n && *heap[right] < *heap[smallest]) {
		smallest = right;
	}
	if (i != smallest) {
		talia* tmp = heap[i];
		heap[i] = heap[smallest];
		heap[smallest] = tmp;

		heapIdx[heap[i]->nr] = i;
		heapIdx[heap[smallest]->nr] = smallest;

		heap_down(heap, heapIdx, n, smallest);
	}
}

void heap_up(talia *heap[], int heapIdx[], int n, int i) {
	int parent = i / 2;
	if (parent > 0 && *heap[parent] > *heap[i]) {
		talia* tmp = heap[i];
		heap[i] = heap[parent];
		heap[parent] = tmp;

		heapIdx[heap[i]->nr] = i;
		heapIdx[heap[parent]->nr] = parent;

		heap_up(heap, heapIdx, n, parent);
	}
}

void heap_pop(talia *heap[], int heapIdx[], int n) {
	heapIdx[heap[1]->nr] = -1;

	talia *temp = heap[1];
	heap[1] = heap[n];
	heap[n] = temp;
	heapIdx[heap[1]->nr] = 1;

	heap_down(heap, heapIdx, n - 1, 1);
}

void build_heap(talia *heap[], int heapIdx[], int n) {
	for (int i = n; i > 0; --i) heap_down(heap, heapIdx, n, i);
}

int main() {
	for (int i=1; i<=100001; ++i) {
		p1[i] = new talia;
		p2[i] = new talia;
	}
	
	scanf ("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &m);
		for (int i=1; i<=n; ++i) {
			p1[i]->nr = p2[i]->nr = i;
			p1[i]->wins = p2[i]->wins = 0;
			p1[i]->loses = p2[i]->loses = 0;
			p1[i]->winWith.clear();
			p1[i]->loseWith.clear();
			p2[i]->winWith.clear();
			p2[i]->loseWith.clear();
			p1heapIdx[i] = p2heapIdx[i] = i;
		}
		while (m--) {
			scanf("%d %c %d", &a, &w, &b);
			if (w == '>') {
				p1[a]->wins++;
				p1[a]->winWith.push_back(b);
				p2[b]->loses++;
				p2[b]->loseWith.push_back(a);
			} else {
				p1[a]->loses++;
				p1[a]->loseWith.push_back(b);
				p2[b]->wins++;
				p2[b]->winWith.push_back(a);
			}
		}
		
		build_heap(p1, p1heapIdx, n);
		build_heap(p2, p2heapIdx, n);
		
		while (n > 1) {
			talia *top = p2[1];
			heap_pop(p2, p2heapIdx, n);
			for (auto it = top->loseWith.begin(); it != top->loseWith.end(); ++it) {
				if (p1heapIdx[*it] <= 0) continue;
				talia* modify = p1[p1heapIdx[*it]];
				modify->wins--;
				heap_down(p1, p1heapIdx, n, p1heapIdx[*it]);
			}
			for (auto it = top->winWith.begin(); it != top->winWith.end(); ++it) {
				if (p1heapIdx[*it] <= 0) continue;
				talia* modify = p1[p1heapIdx[*it]];
				modify->loses--;
				heap_up(p1, p1heapIdx, n, p1heapIdx[*it]);
			}

			top = p1[1]; 
			heap_pop(p1, p1heapIdx, n);
			for (auto it = top->loseWith.begin(); it != top->loseWith.end(); ++it) {
				if (p2heapIdx[*it] <= 0) continue;
				talia* modify = p2[p2heapIdx[*it]];
				modify->wins--;
				heap_down(p2, p2heapIdx, n - 1, p2heapIdx[*it]);
			}
			for (auto it = top->winWith.begin(); it != top->winWith.end(); ++it) {
				if (p2heapIdx[*it] <= 0) continue;
				talia* modify = p2[p2heapIdx[*it]];
				modify->loses--;
				heap_up(p2, p2heapIdx, n - 1, p2heapIdx[*it]);
			}
			n--;
		}
		talia *top = p1[1];
		if (top->wins == 1) printf("WYGRANA\n");
		else if (top->loses == 1) printf("PRZEGRANA\n");
		else printf("REMIS\n");
	}
	return 0;	
}