public static void main(String[] args) throws IOException {
        class City {
            short a;
            int c;
        }
        System.setIn(new FileInputStream("in.small"));
        BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bfr.readLine());
        for (int i = 1; i <= t; i++) {
            String[] strings = bfr.readLine().split(" ");
            int[] sum = new int[2];
            int n = Integer.parseInt(strings[0]);
            int m = Integer.parseInt(strings[1]);
            City[] cities = new City[n + 1];
            boolean[] counted = new boolean[n + 1];
            for (int j = 1; j <= n; j++) {
                cities[j] = new City();
                strings = bfr.readLine().split(" ");
                if (strings[0].equals("A"))
                    cities[j].a = 1;
                else
                    cities[j].a = 0;
                cities[j].c = Integer.parseInt(strings[1]);
            }
            for (int j = 0; j < m; j++) {
                String str = bfr.readLine();
                strings = str.split(" ");
                int from = Integer.parseInt(strings[0]);
                int to = Integer.parseInt(strings[1]);
                if (cities[from].a != cities[to].a) {
                    if (!counted[from]) {
                        sum[cities[from].a] += cities[from].c;
                        counted[from] = true;
                    }
                    if (!counted[to]) {
                        sum[cities[to].a] += cities[to].c;
                        counted[to] = true;
                    }
                }
            }
            System.out.println(Math.min(sum[1], sum[0]));
        }
        bfr.close();
    }