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
#include <iostream>
#include <set>
#include <limits>
#include <unordered_set>
#include <unordered_map>
#include <vector>

using namespace std;

struct entry {
  entry(int _index, int _value): index(_index), value(_value) {}
  entry(int _index) : entry(_index, std::numeric_limits<int>::lowest()) {}
  entry() : entry(-1, -1) {}
  int index = -1;
  int value = 0;
  
  bool operator<(const entry& rhs) const {
    if(index == rhs.index) return false;
    return value < rhs.value;
  }
};

void decrement(multiset<entry>& cards, int index) {
  entry x(index);
  auto found = cards.find(x);
  
  if(found == cards.end()) return;
  
  x = *found;
  cards.erase(found);
  x.value--;
  cards.insert(x);
}

int main()
{
  ios::sync_with_stdio(false);
  int t, n, m;
  int who, whom;
  char tmp;
  
  cin >> t;
  
  while(t--)
  {
    unordered_map<int, unordered_set<int> > bajtekKills, bitekKills;
    vector<entry> bajtekCards, bitekCards;
    cin >> n >> m;
    bajtekCards.resize(n); bitekCards.resize(n);
    for(int i = 0; i < n; ++i) {
      bajtekCards[i] = bitekCards[i] = entry(i, 0);
    }
    
    while(m--)
    {
      cin >> who >> tmp >> whom;
      --who;
      --whom;
      if(bajtekKills.find(who) == bajtekKills.end())
        bajtekKills.insert(make_pair(who, unordered_set<int>()));
      if(bitekKills.find(whom) == bitekKills.end())
        bitekKills.insert(make_pair(whom, unordered_set<int>()));
      
      if(tmp == '>') {
        bajtekKills[who].insert(whom);
        bitekCards[whom].value++;
        bajtekCards[who].value--;
      }
      else {
        bitekKills[whom].insert(who);
        bitekCards[whom].value--;
        bajtekCards[who].value++;
      }
    }
    
    multiset<entry> bajtekCardsSet(bajtekCards.begin(), bajtekCards.end()), bitekCardsSet(bitekCards.begin(), bitekCards.end());
    
    while(bajtekCardsSet.size() > 1) {
      auto removedBitekCard = bitekCardsSet.begin();
      bitekCardsSet.erase(removedBitekCard);
      
      for(int updateCard : bitekKills[removedBitekCard->value]) {
        decrement(bajtekCardsSet, updateCard);
      }
      
      auto removedBajtekCard = bajtekCardsSet.begin();
      bajtekCardsSet.erase(removedBajtekCard);
      
      for(int updateCard : bajtekKills[removedBajtekCard->value]) {
        decrement(bitekCardsSet, updateCard);
      }
    }
    
    auto bajtekCard = bajtekCardsSet.begin()->index;
    auto bitekCard = bitekCardsSet.begin()->index;
    
    if(bajtekKills[bajtekCard].find(bitekCard) != bajtekKills[bajtekCard].end())
      cout << "WYGRANA";
    else if(bitekKills[bitekCard].find(bajtekCard) != bitekKills[bitekCard].end())
      cout << "PRZEGRANA";
    else
      cout << "REMIS";
    
    cout << endl;
  }
  
  return 0;
}