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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#define NDEBUG
#include<bits/stdc++.h>
using namespace std;

#define DB 0

#define REP(I,N) for(int I=0;I<(N);I++)
#define FOR(I,A,B) for(int I=(A);I<=(B);I++)
#define PB push_back
#define FI first
#define SE second
#define SZ(X) ((int)X.size())

typedef vector<string> VS;

inline int toInt(const string &s){int x=0;istringstream i(s);i>>x;return x;}
inline VS parse(const string &s,const char ch=' '){string a;VS wyn;REP(i,(int)s.size()) if(s[i]!=ch) a+=s[i];else if(!a.empty()){wyn.PB(a);a="";} if(!a.empty()) wyn.PB(a);return wyn;}
template<class T> inline string stringify(const T &x,int p=9){ostringstream o;o.precision(p);o<<x;o.flush();return o.str();}

/*struct pq_comp{
  bool operator() (const tuple<int,int,int>& lhs, const tuple<int,int,int>& rhs) const{
    int pom=get<1>(lhs)-get<1>(rhs);
    if(pom>0) return true;
    else if(pom==0){
      pom=get<2>(lhs)-get<2>(rhs);
      if(pom<0) return true;
      else if(pom>0) return false;
      return get<0>(lhs)<get<0>(rhs);
    }
    return false;
  }
};*/

struct pq_comp{
  bool operator() (const tuple<int,int,int>& lhs, const tuple<int,int,int>& rhs) const{
    int pom=get<1>(lhs)-get<2>(lhs)-get<1>(rhs)+get<2>(rhs);
    if(pom>0) return true;
    else if(pom==0){
      return get<0>(lhs)<get<0>(rhs);
    }
    return false;
  }
};

int TT,n,m;
unordered_map<int,unordered_set<int>> Awin,Aloose,Bwin,Bloose;
set<tuple<int,int,int>,pq_comp> Apq,Bpq;
int An,Bn;

inline void Abests(int who, int whom){
  auto it=Awin.find(who);
  if(it==Awin.end()){
    unordered_set<int> s;
    Aloose[who]=s;
    s.insert(whom);
    Awin[who]=s;
  }else{
    Awin[who].insert(whom);
  }
  it=Bloose.find(whom);
  if(it==Bloose.end()){
    unordered_set<int> s;
    Bwin[whom]=s;
    s.insert(who);
    Bloose[whom]=s;
  }else{
    Bloose[whom].insert(who);
  }
}

inline void Bbests(int who, int whom){
  auto it=Bwin.find(who);
  if(it==Bwin.end()){
    unordered_set<int> s;
    Bloose[who]=s;
    s.insert(whom);
    Bwin[who]=s;
  }else{
    Bwin[who].insert(whom);
  }
  it=Aloose.find(whom);
  if(it==Aloose.end()){
    unordered_set<int> s;
    Awin[whom]=s;
    s.insert(who);
    Aloose[whom]=s;
  }else{
    Aloose[whom].insert(who);
  }
}

inline void updateA(int num, int e, bool win=true){
  int oldw=SZ(Awin[num]),oldp=SZ(Aloose[num]);
  Apq.erase(make_tuple(num,oldw,oldp));
  if(win){
    Awin[num].erase(e);
    oldw--;
  }else{
    Aloose[num].erase(e);
    oldp--;
  }
  if(oldw+oldp>0) Apq.emplace(num,oldw,oldp); else An++;
}

inline void updateB(int num, int e, bool win=true){
  int oldw=SZ(Bwin[num]),oldp=SZ(Bloose[num]);
  Bpq.erase(make_tuple(num,oldw,oldp));
  if(win){
    Bwin[num].erase(e);
    oldw--;
  }else{
    Bloose[num].erase(e);
    oldp--;
  }
  if(oldw+oldp>0) Bpq.emplace(num,oldw,oldp); else Bn++;
}

inline void removeB(){
  auto it=Bpq.begin();
  int w=get<1>(*it);
  if(w>0||Bn==0){
    int num=get<0>(*it);
    Bpq.erase(it);
    if(DB) cout<<"B rem "<<stringify(num)<<"\t";
    for(auto &e : Bwin[num]) {updateA(e,num,false);if(DB) cout<<"w "<<stringify(e)<<" ";}
    for(auto &e : Bloose[num]) {updateA(e,num,true);if(DB) cout<<"p "<<stringify(e)<<" ";}
    if(DB) cout<<"\n";
  }else{
    Bn--;
    if(DB) cout<<"Bn rem\n";
  }
}

inline void removeA(){
  auto it=Apq.begin();
  int w=get<1>(*it);
  if(w>0||An==0){
    int num=get<0>(*it);
    Apq.erase(it);
    if(DB) cout<<"A rem "<<stringify(num)<<"\t";
    for(auto &e : Awin[num]) {updateB(e,num,false);if(DB) cout<<"w "<<stringify(e)<<" ";}
    for(auto &e : Aloose[num]) {updateB(e,num,true);if(DB) cout<<"p "<<stringify(e)<<" ";}
    if(DB) cout<<"\n";
  }else{
    An--;
    if(DB) cout<<"An rem\n";
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin>>TT;
  string line;
  REP(i,TT){
    cin>>n>>m;
    cin.ignore();
    Awin.clear();Aloose.clear();Bwin.clear();Bloose.clear();Apq.clear();Bpq.clear();
    REP(i,m){
      getline(cin,line);
      VS p=parse(line);
      int a=toInt(p[0]),b=toInt(p[2]);
      if(p[1][0]=='>') Abests(a,b); else Bbests(b,a);
    }
    for(auto &e : Awin){
      int w=SZ(e.SE);
      int p=SZ(Aloose[e.FI]);
      Apq.emplace(e.FI,w,p);
    }
    for(auto &e : Bwin){
      int w=SZ(e.SE);
      int p=SZ(Bloose[e.FI]);
      Bpq.emplace(e.FI,w,p);
    }
    An=n-SZ(Awin);
    Bn=n-SZ(Bwin);
    while(n>1&&!Apq.empty()){
      removeB();
      removeA();
      n--;
    }
    int ret=0;
    if(!Apq.empty()){
      auto it=Apq.begin();
      int w=get<1>(*it);
      if(w>0) ret=1; else ret=-1;
    }
    switch(ret){
      case 1:
        cout<<"WYGRANA\n"; break;
      case -1:
        cout<<"PRZEGRANA\n"; break;
      default:
        cout<<"REMIS\n"; break;
    }
  }
  return 0;
}