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
#include <iostream>
#include <cstdio>
#include <cassert>
#include <map>
#include <set>

int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; ++i) {
    int n, m;
    std::map<int, std::set<int>> aAttacks, bAttacks;
    std::map<int, std::set<int>> aAttacked, bAttacked;
    std::set<int> aNotAttacked, bNotAttacked;
    std::set<int> A, B;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
      A.insert(i+1);
      B.insert(i+1);
      aNotAttacked.insert(i+1);
      bNotAttacked.insert(i+1);
    }
    for (int i = 0; i < m; ++i) {
      int a, b;
      char c;
      scanf("%d %c %d", &a, &c, &b);
      if (c == '<') {
	bAttacks[b].insert(a);
	aAttacked[a].insert(b);
	aNotAttacked.erase(a);
      } else {
	assert(c == '>');
	aAttacks[a].insert(b);
	bAttacked[b].insert(a);
	bNotAttacked.erase(b);
      }
    }
    for (int i = 0; i < n - 1; ++i) {
      {
	while (!bAttacks.empty() && bAttacks.begin()->second.empty()) {
	  bAttacks.erase(bAttacks.begin());
	}
	const int bToRemove = !bAttacks.empty() ? bAttacks.begin()->first : (!bNotAttacked.empty() ? *bNotAttacked.begin() : *B.begin());
	//	std::cerr << "removing: " << bToRemove << "\n";
	if (bAttacked.count(bToRemove)) {
	  for (int a : bAttacked[bToRemove]) {
	    const int numErased = aAttacks[a].erase(bToRemove);
	    assert(numErased == 1);
	    if (aAttacks[a].empty()) {
	      aAttacks.erase(a);
	    }
	  }
	  bAttacked.erase(bToRemove);
	}
	if (bAttacks.count(bToRemove)) {
	  for (int a : bAttacks[bToRemove]) {
	    const int numErased = aAttacked[a].erase(bToRemove);
	    assert(numErased == 1);
	    if (aAttacked[a].empty()) {
	      aAttacked.erase(a);
	      aNotAttacked.insert(a);
	    }
	  }
	  bAttacks.erase(bToRemove);
	}
	bNotAttacked.erase(bToRemove);
	const int numErased = B.erase(bToRemove);
	assert(numErased == 1);
      }

      {
	while (!aAttacks.empty() && aAttacks.begin()->second.empty()) {
	  aAttacks.erase(aAttacks.begin());
	}
	const int aToRemove = !aAttacks.empty() ? aAttacks.begin()->first : (!aNotAttacked.empty() ? *aNotAttacked.begin() : *A.begin());
	//	std::cerr << "removing: " << aToRemove << "\n";
	if (aAttacked.count(aToRemove)) {
	  for (int b : aAttacked[aToRemove]) {
	    const int numErased = bAttacks[b].erase(aToRemove);
	    assert(numErased == 1);
	    if (bAttacks[b].empty()) {
	      bAttacks.erase(b);
	    }
	  }
	  aAttacked.erase(aToRemove);
	}
	if (aAttacks.count(aToRemove)) {
	  for (int b : aAttacks[aToRemove]) {
	    const int numErased = bAttacked[b].erase(aToRemove);
	    assert(numErased == 1);
	    if (bAttacked[b].empty()) {
	      bAttacked.erase(b);
	      bNotAttacked.insert(b);
	    }
	  }
	  aAttacks.erase(aToRemove);
	}
	aNotAttacked.erase(aToRemove);
	const int numErased = A.erase(aToRemove);
	assert(numErased == 1);
      }
    }
    assert(A.size() == 1);
    assert(B.size() == 1);
    if (!aAttacks.empty()) {
      assert(bAttacks.empty());
      //      std::cerr << aAttacks.begin()->first << " -> " << aAttacks.begin()->second.size() << "\n";
      printf("WYGRANA\n");
    } else if (!bAttacks.empty()) {
      assert(aAttacks.empty());
      printf("PRZEGRANA\n");
    } else {
      printf("REMIS\n");
    }
  }
}