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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class kar {

	private static class BitekCardsData {

		private int numberOfCards;
		private boolean[] beatsAny;
		private int[] beatenBy;

		private BitekCardsData(int numberOfCards) {
			this.numberOfCards = numberOfCards;
			this.beatenBy = new int[numberOfCards];
			this.beatsAny = new boolean[numberOfCards];
		}

	}

	public static void main(String[] args) throws NumberFormatException, IOException {

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		int numberOfTests = Integer.parseInt(reader.readLine());
		for (int j = 0; j < numberOfTests; ++j) {
			BitekCardsData bitekCardsData = readNextTest(reader);
			System.out.println(answer(bitekCardsData));
		}
	}

	public static BitekCardsData readNextTest(BufferedReader reader) throws IOException {
		StringTokenizer st = new StringTokenizer(reader.readLine());
		int numberOfCards = Integer.parseInt(st.nextToken());
		BitekCardsData bitekCardsData = new BitekCardsData(numberOfCards);
		int numberOfPairs = Integer.parseInt(st.nextToken());
		for (int j = 0; j < numberOfPairs; ++j) {
			st = new StringTokenizer(reader.readLine());
			st.nextToken(); // bajtek card
			char fightResult = st.nextToken().charAt(0);
			int bitekCard = Integer.parseInt(st.nextToken());
			if (fightResult == '<')
				bitekCardsData.beatsAny[bitekCard - 1] = true;
			else
				++bitekCardsData.beatenBy[bitekCard - 1];
		}
		return bitekCardsData;
	}

	public static String answer(BitekCardsData bitekCardsData) {
		if (isBajtekWinning(bitekCardsData)) {
			return "WYGRANA";
		} else if (isBitekWinning(bitekCardsData)) {
			return "PRZEGRANA";
		} else {
			return "REMIS";
		}
	}

	public static boolean isBajtekWinning(BitekCardsData bitekCardsData) {
		int maxBeatenBy = bitekCardsData.beatenBy[0];
		for (int j = 1; j < bitekCardsData.numberOfCards; ++j) {
			if (maxBeatenBy < bitekCardsData.beatenBy[j]) {
				maxBeatenBy = bitekCardsData.beatenBy[j];
			}
		}
		return maxBeatenBy == bitekCardsData.numberOfCards;
	}

	public static boolean isBitekWinning(BitekCardsData bitekCardsData) {
		for (int j = 0; j < bitekCardsData.numberOfCards; ++j) {
			if (!bitekCardsData.beatsAny[j]) {
				return false;
			}
		}
		return true;
	}

}