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
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5+10, constt = 21;

int t,n,m,a,b,M=1,x,z;
char w;
vector <int> wektor1[constt][maxn], wektor2[constt][maxn], odwrot1[constt][maxn], odwrot2[constt][maxn];
pair<int,int> drzewo1[constt][maxn*2+1], drzewo2[constt][maxn*2+1];
vector <int> xd1[constt], xd2[constt];
bitset <maxn> zajete1[constt], zajete2[constt];

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> t;
    for(int k=0;k<t;k++)
    {
        cin >> n >> m;
        while(M<n) M*=2;
        for(int i=0;i<m;i++)
        {
            cin >> a >> w >> b;
            if(w=='<')
            {
                wektor2[k][b].push_back(a);
                odwrot2[k][a].push_back(b);
                if(!zajete2[k][b])
                {
                    zajete2[k][b]=1;
                    xd2[k].push_back(b);
                }
            }
            if(w=='>')
            {
                wektor1[k][a].push_back(b);
                odwrot1[k][b].push_back(a);
                if(!zajete1[k][a])
                {
                    zajete1[k][a]=1;
                    xd1[k].push_back(a);
                }
            }
        }
        for(int i=0;i<xd1[k].size();i++)
        {
            drzewo1[k][M+xd1[k][i]-1] = make_pair(wektor1[k][xd1[k][i]].size(),xd1[k][i]);
            x = M+xd1[k][i]-1;
            x/=2;
            while(x>0)
            {
                drzewo1[k][x] = make_pair(0,0);
                if(drzewo1[k][2*x].first > drzewo1[k][x].first)
                {
                    drzewo1[k][x] = drzewo1[k][2*x];
                }
                if(drzewo1[k][2*x+1].first > drzewo1[k][x].first)
                {
                    drzewo1[k][x] = drzewo1[k][2*x+1];
                }
                x/=2;
            }
        }
        for(int i=0;i<xd2[k].size();i++)
        {
            drzewo2[k][M+xd2[k][i]-1] = make_pair(wektor2[k][xd2[k][i]].size(),xd2[k][i]);
            x = M+xd2[k][i]-1;
            x/=2;
            while(x>0)
            {
                drzewo2[k][x] = make_pair(0,0);
                if(drzewo2[k][2*x].first > drzewo2[k][x].first)
                {
                    drzewo2[k][x] = drzewo2[k][2*x];
                }
                if(drzewo2[k][2*x+1].first > drzewo2[k][x].first)
                {
                    drzewo2[k][x] = drzewo2[k][2*x+1];
                }
                x/=2;
            }
        }
        for(int i=0;i<2*n-2;i++)
        {
            if(i%2==0)
            {
                x = drzewo2[k][1].second;
                z = x;
                drzewo2[k][M+x-1].first = 0;
                x = M+x-1;
                x/=2;
                while(x>0)
                {
                    drzewo2[k][x] = make_pair(0,0);
                    if(drzewo2[k][2*x].first > drzewo2[k][x].first)
                    {
                        drzewo2[k][x] = drzewo2[k][2*x];
                    }
                    if(drzewo2[k][2*x+1].first > drzewo2[k][x].first)
                    {
                        drzewo2[k][x] = drzewo2[k][2*x+1];
                    }
                    x/=2;
                }
                for(int j=0;j<odwrot1[k][z].size();j++)
                {
                    x = M + odwrot1[k][z][j] - 1;
                    drzewo1[k][x].first--;
                    x/=2;
                    while(x>0)
                    {
                        drzewo1[k][x] = make_pair(0,0);
                        if(drzewo1[k][2*x].first > drzewo1[k][x].first)
                        {
                            drzewo1[k][x] = drzewo1[k][2*x];
                        }
                        if(drzewo1[k][2*x+1].first > drzewo1[k][x].first)
                        {
                            drzewo1[k][x] = drzewo1[k][2*x+1];
                        }
                        x/=2;
                    }
                }
            }
            if(i%2==1)
            {
                x = drzewo1[k][1].second;
                z = x;
                drzewo1[k][M+x-1].first = 0;
                x = M+x-1;
                x/=2;
                //cout << drzewo1[k][1].second << " " << drzewo1[k][1].first << endl;
                while(x>0)
                {
                    drzewo1[k][x] = make_pair(0,0);
                    if(drzewo1[k][2*x].first > drzewo1[k][x].first)
                    {
                        drzewo1[k][x] = drzewo1[k][2*x];
                    }
                    if(drzewo1[k][2*x+1].first > drzewo1[k][x].first)
                    {
                        drzewo1[k][x] = drzewo1[k][2*x+1];
                    }
                    x/=2;
                }
                //cout << drzewo1[k][1].second << " " << drzewo1[k][1].first << endl;
                for(int j=0;j<odwrot2[k][z].size();j++)
                {
                    x = M + odwrot2[k][z][j] - 1;
                    drzewo2[k][x].first--;
                    x/=2;
                    while(x>0)
                    {
                        drzewo2[k][x] = make_pair(0,0);
                        if(drzewo2[k][2*x].first > drzewo2[k][x].first)
                        {
                            drzewo2[k][x] = drzewo2[k][2*x];
                        }
                        if(drzewo2[k][2*x+1].first > drzewo2[k][x].first)
                        {
                            drzewo2[k][x] = drzewo2[k][2*x+1];
                        }
                        x/=2;
                    }
                }
            }
        }
        //cout << drzewo1[k][1].first << " " << drzewo2[k][1].first << endl;
        if(drzewo1[k][1].first == drzewo2[k][1].first)
        {
            cout << "REMIS" << endl;
        }
        else
        {
            if(drzewo1[k][1].first > drzewo2[k][1].first)
            {
                cout << "WYGRANA" << endl;
            }
            else
            {
                if(drzewo1[k][1].first < drzewo2[k][1].first)
                {
                    cout << "PRZEGRANA" << endl;
                }
            }
        }
    }
}