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
/*
 * Potyczki Algorytmiczne 2016, runda 1
 * Zadanie: KAR
 * Autor: Tomasz 'Xupicor' Wota <tomasz.wota@gmail.com> 2016-11-22
 */
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <utility>


class Deck {
    std::unordered_set<int> targets;
    int targeted_by = 0;
public:
    int value() const {
        return targets.size() - targeted_by;
    }
    bool defeats(int n) const {
        auto found = targets.find(n);
        return found != targets.cend();
    }
    void add_target(int n) {
        targets.emplace(n);
    }
    void add_danger() {
        ++targeted_by;
    }
    void remove_danger() {
        --targeted_by;
    }
    
};

class Player {
    std::unordered_map<int,Deck> worthy_decks;
    std::unordered_set<int> unworthy;
public:
    Player(int decks) {
        for (int i = 1; i <= decks; ++i) {
            unworthy.emplace(i);
        }
    }
    Player(const Player&) = delete;
    
    void add_loser(int my_deck) {
        auto found = worthy_decks.find(my_deck);
        if (found == worthy_decks.end()) {
            unworthy.erase(unworthy.find(my_deck));
        } 
        worthy_decks[my_deck].add_danger();            
    }
    void add_winner(int my_deck, int sure_loser) {
        auto found = worthy_decks.find(my_deck);
        if (found == worthy_decks.end()) {
            unworthy.erase(unworthy.find(my_deck));
        } 
        worthy_decks[my_deck].add_target(sure_loser);
    }
    int decks() const {
        return unworthy.size() + worthy_decks.size();
    }
    int remove_most_valuable_deck() {
        int removed = 0;
        if (!worthy_decks.empty()) {
            auto mvd = worthy_decks.begin();
            auto i = worthy_decks.begin();
            for (++i; i != worthy_decks.end(); ++i) {
                if (mvd->second.value() < i->second.value()) {
                    mvd = i;
                }
            }
            if (mvd->second.value() > 0 || unworthy.empty()) {
                removed = mvd->first;
                worthy_decks.erase(mvd);
            }
        } else {
            removed = *unworthy.begin();
            unworthy.erase(unworthy.begin());
        }
        
        return removed;
    }
    void remove_target(int n) {
        worthy_decks[n].remove_danger();
    }
    std::pair<Deck,int> last_deck() { 
        if (worthy_decks.empty()) {
            Deck d;
            return { d, *unworthy.begin() };
        } else {
            auto& d = worthy_decks.begin()->second;
            return { d, worthy_decks.begin()->first };
        }
    }
};

class Game {
    Player bajtek, bitek;
    int pairs;
    
    void process_input() {
        for (int i = 0; i < pairs; ++i) {
            int a, b;
            char rel;
            std::cin >> a >> rel >> b;
            if (rel == '>') {
                bajtek.add_winner(a, b);
                bitek.add_loser(b);
            } else {
                bitek.add_winner(b, a);
                bajtek.add_loser(a);
            }
        }
    }

    void play() {
        while (bajtek.decks() > 1) {
            bitek.remove_most_valuable_deck();
            bajtek.remove_most_valuable_deck();
        }
    }
    
public:
    Game(int amount_of_decks, int pairs)
            : bajtek(amount_of_decks), bitek(amount_of_decks), pairs(pairs) { }
    Game(const Game&) = delete;
    
    std::string run() {
        process_input();
        play();
        
        // the final showdown
        auto a = bajtek.last_deck();
        auto b = bitek.last_deck();
        if (a.first.defeats(b.second)) {
            return "WYGRANA";
        } else if (b.first.defeats(a.second)) {
            return "PRZEGRANA";
        } else {
            return "REMIS";
        }
    }
    
};


int main() {
    std::ios::sync_with_stdio(false);
    
    int t = 0;
    std::cin >> t;
    
    for (int test_no = 0; test_no < t; ++test_no) {
        int amount_of_decks, pairs;
        std::cin >> amount_of_decks >> pairs;
        Game game {amount_of_decks, pairs};
        std::cout << game.run() << '\n';
    }
}