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
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.IntStream;

public class kar {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int t = scanner.nextInt();
			for (int i = 0; i < t; i++) {
				Boolean score = eval(scanner);
				System.out.println(score != null ? (score ? "WYGRANA" : "PRZEGRANA") : "REMIS");
			}
		}
	}

	private static Boolean eval(Scanner scanner) {
		int a, b;
		Boolean w;
		int n = scanner.nextInt();
		int m = scanner.nextInt();

		Boolean[][] cards = new Boolean[n][n];
		for (int i = 0; i < m; i++) {
			a = scanner.nextInt();
			w = parse(scanner.next());
			b = scanner.nextInt();
			cards[a - 1][b - 1] = w;
		}

		return eval(n, cards);
	}

	private static Boolean eval(int n, Boolean[][] cards) {
		int[] scores = new int[n];
		int deck, max;

		Set<Integer> decks1 = new HashSet<>();
		IntStream.range(0, n).forEach(decks1::add);
		Set<Integer> decks2 = new HashSet<>(decks1);

		for (int t = 1; t < n; t++) {
			
			for (int i = 0; i < n; i++) {
				scores[i] = 0;
				for (int j = 0; j < n; j++) {
					if (decks1.contains(j) && decks2.contains(i)) {
						scores[i] -= val(cards[j][i]);
					}
				}
			}
			
			deck = decks2.iterator().next();
			max = scores[deck];
			
			for (int i = 1; i < n; i++) {
				if (decks2.contains(i) && scores[i] > max) {
					max = scores[i];
					deck = i;
				}
			}
			
			decks2.remove(deck);
			
			for (int i = 0; i < n; i++) {
				scores[i] = 0;
				for (int j = 0; j < n; j++) {
					if (decks1.contains(i) && decks2.contains(j)) {
						scores[i] += val(cards[i][j]);
					}
				}
			}
			
			deck = decks1.iterator().next();
			max = scores[deck];
			
			for (int i = 1; i < n; i++) {
				if (decks1.contains(i) && scores[i] > max) {
					max = scores[i];
					deck = i;
				}
			}

			decks1.remove(deck);

		}

		int d1 = decks1.iterator().next();
		int d2 = decks2.iterator().next();
		return cards[d1][d2];
	}

	private static int val(Boolean b) {
		return b != null ? (b ? 1 : -1) : 0;
	}

	private static Boolean parse(String s) {
		switch (s) {
		case ">":
			return true;
		case "<":
			return false;
		}
		return null;
	}
}