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
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class kar
{
    private static final Pattern LINE_PATTERN = Pattern.compile("(\\d+) ([<>]) (\\d+)");

    public final static String solve(int[] bajtek, int[] bitek)
    {
        if (contains(bajtek, bajtek.length))
        {
            return "WYGRANA";
        }
        if (!contains(bitek, 0))
        {
            return "PRZEGRANA";
        }
        return "REMIS";
    }

    private final static boolean contains(int[] array, int x)
    {
        for (int i = 0; i < array.length; i++)
        {
            if (array[i] == x)
            {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int setsNumber = in.nextInt();
        in.nextLine();
        for (int t = 0; t < setsNumber; t++)
        {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] bajtek = new int[n];
            int[] bitek = new int[n];
            in.nextLine();
            for (int i = 0; i < m; i++)
            {
                String line = in.nextLine();
                Matcher matcher = LINE_PATTERN.matcher(line);
                if (matcher.matches())
                {
                    Integer bajtekCards = Integer.valueOf(matcher.group(1)) - 1;
                    Integer bitekCards = Integer.valueOf(matcher.group(3)) - 1;
                    boolean bajtekCardsWin = matcher.group(2).contains(">");
                    if (bajtekCardsWin)
                    {
                        bajtek[bitekCards]++;
                    }
                    else
                    {
                        bitek[bitekCards]++;
                    }
                }
            }
            System.out.println(solve(bajtek, bitek));
        }
    }
}