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
#include <cstdio>
#include <set>
#include <vector>
#include <ios>

using std::set;
using std::vector;
using std::ios_base;

struct Card
{
  int winCount;
  int loseCount;
  bool deleted;
  vector<int> win;
  vector<int> lose;

  Card()
  {
    winCount = 0;
    loseCount = 0;
    deleted = false;
  }
};

Card * cards1;
Card * cards2;

bool comp(Card * cards, const int & id1, const int & id2)
{
  if (cards[id1].winCount != cards[id2].winCount)
    return cards[id1].winCount > cards[id2].winCount;
  if (cards[id1].loseCount != cards[id2].loseCount)
    return cards[id1].loseCount < cards[id2].loseCount;
  return id1 < id2;
}

struct Compare1
{
  bool operator()(const int & id1, const int & id2) const
  {
    return comp(cards1, id1, id2);
  }
};

struct Compare2
{
  bool operator()(const int & id1, const int & id2) const
  {
    return comp(cards2, id1, id2);
  }
};

template <typename T1, typename T2>
void removeCard(set<int, T1> & thisHand, set<int, T2> & otherHand, Card * thisCards, Card * otherCards)
{
  set<int>::iterator bestit = thisHand.begin();
  int best = *bestit;
  thisHand.erase(bestit);
  Card & thisCard = thisCards[best];
  thisCard.deleted = true;
  for (vector<int>::iterator it = thisCard.win.begin(); it != thisCard.win.end(); it++)
    if (!otherCards[*it].deleted)
      {
        otherHand.erase(*it);
        otherCards[*it].loseCount--;
        otherHand.insert(*it);
      }
  for (vector<int>::iterator it = thisCard.lose.begin(); it != thisCard.lose.end(); it++)
    if (!otherCards[*it].deleted)
      {
        otherHand.erase(*it);
        otherCards[*it].winCount--;
        otherHand.insert(*it);
      }
}

int main()
{
  ios_base::sync_with_stdio(false);

  int t;
  scanf("%d", &t);
  while (t--)
  {
    Card newcards1[100001];
    Card newcards2[100001];
    cards1 = newcards1;
    cards2 = newcards2;
    int n;
    int m;
    scanf("%d%d", &n, &m);
    int a;
    int b;
    char op[2];
    while (m--)
    {
      scanf("%d%s%d", &a, op, &b);
      if (op[0] == '<')
      {
        cards1[a].lose.push_back(b);
        cards1[a].loseCount++;
        cards2[b].win.push_back(a);
        cards2[b].winCount++;
      }
      else if (op[0] == '>')
      {
        cards1[a].win.push_back(b);
        cards1[a].winCount++;
        cards2[b].lose.push_back(a);
        cards2[b].loseCount++;
      }
      else
      {
        printf("EEEERRRRRRRRROOOOOOOOORRRR %s", op);
        return -1;
      }
    } //end while (m--)

    set<int, Compare1> hand1;
    set<int, Compare2> hand2;
    for (int i = 1; i <= n; i++)
    {
      hand1.insert(i);
      hand2.insert(i);
    }

    while (hand1.size() > 1)
    {
      removeCard<Compare2, Compare1>(hand2, hand1, cards2, cards1);
      removeCard<Compare1, Compare2>(hand1, hand2, cards1, cards2);
    }

    int last1id = *(hand1.begin());
    int last2id = *(hand2.begin());
    Card & lastCard1 = cards1[last1id];
    bool win = false;
    for (vector<int>::iterator it = lastCard1.win.begin(); it != lastCard1.win.end(); it++)
      if (*it == last2id)
      {
        win = true;
        break;
      }
    if (win)
    {
      printf("WYGRANA\n");
      continue;
    }
    bool lose = false;
    for (vector<int>::iterator it = lastCard1.lose.begin(); it != lastCard1.lose.end(); it++)
      if (*it == last2id)
      {
        lose = true;
        break;
      }
    if (lose)
    {
      printf("PRZEGRANA\n");
      continue;
    }
    printf("REMIS\n");
  }

  return 0;
}