import java.io.*;
import java.util.*;

public class coincidence_ats implements Runnable {
	public static void main(String[] args) {
		new Thread(new coincidence_ats()).start();
	}

	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}
		return in.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public class Contestant implements Comparable<Contestant> {
		long ip;
		String name;
		int rating;
		int count;
		int[] results;
		int level;
		int place;

		public Contestant(String name, int curRating) {
			this.name = name;
			count = 0;
			rating = curRating;
			ip = -1;
			results = new int[m];
			Arrays.fill(results, -1);
			level = 0;
			place = -1;
		}

		public void update(int delta) {
			rating += delta;
			rating = Math.min(10, rating);
			rating = Math.max(0, rating);
		}

		public void addResult(int contestID, int res) {
			results[contestID] = res;
			count++;
		}

		public void setIP(String address) {
			StringTokenizer ipTokenizer = new StringTokenizer(address, ".");
			long curIP = 0;
			for (int i = 0; i < 4; i++) {
				curIP = curIP * (long) 256
						+ Integer.parseInt(ipTokenizer.nextToken());
			}
			assert (this.ip == -1 || this.ip == curIP);
			ip = curIP;
			assert (!ipTokenizer.hasMoreTokens());
		}

		public int compareTo(Contestant o) {
			return this.name.compareTo(o.name);
		}
	}

	ArrayList<Integer>[] contests;
	int[][] highest;
	int n, m;

	boolean[][] sus;

	final int MAXN = 200;
	final int MAXR = 10;
	final int MAXRES = 2000;
	final int MAXLEN = 20;

	public void solve() throws IOException {
		n = nextInt();
		m = nextInt();

		assert (1 <= n && n <= MAXN);
		assert (1 <= m && m <= MAXN);

		contests = new ArrayList[m];
		sus = new boolean[n][n];
		for (int i = 0; i < n; i++) {
			Arrays.fill(sus[i], true);
		}
		highest = new int[m][11];
		for (int i = 0; i < m; i++) {
			Arrays.fill(highest[i], -1);
		}
		ArrayList<Contestant> users = new ArrayList<Contestant>();
		HashMap<String, Integer> place = new HashMap<String, Integer>();

		for (int i = 0; i < n; i++) {
			String name = nextToken();
			assert (name.length() <= MAXLEN);

			int rating = nextInt();
			assert (0 <= rating && rating <= MAXR);

			users.add(new Contestant(name, rating));
		}
		// Collections.sort(users);
		for (int i = 0; i < n; i++) {
			place.put(users.get(i).name, i);
		}

		for (int i = 0; i < m; i++) {
			ArrayList<Integer> contestants = new ArrayList<Integer>();
			int k = nextInt();
			assert (1 <= k && k <= n);

			for (int j = 0; j < k; j++) {

				String name = nextToken();
				assert (place.containsKey(name));

				int result = nextInt();
				assert (0 <= result && result <= MAXRES);

				int delta = nextInt();
				assert (-MAXR <= delta && delta <= MAXR);

				String adr = nextToken();

				int pos = place.get(name);
				contestants.add(pos);
				users.get(pos).update(delta);
				users.get(pos).setIP(adr);
				users.get(pos).addResult(i, result);
			}
			for (int j = 0; j < k; j++) {
				for (int l = j; l < k; l++) {
					sus[contestants.get(j)][contestants.get(l)] = false;
					sus[contestants.get(l)][contestants.get(j)] = false;
				}
			}
		}

		for (int i = 0; i < n; i++) {
			updateScores(users.get(i));
		}
		for (int i = 0; i < m; i++) {
			for (int j = 1; j < 11; j++) {
				highest[i][j] = Math.max(highest[i][j], highest[i][j - 1]);
			}
		}

		for (int i = 0; i < n; i++) {
			if (users.get(i).count == 0) {
				users.get(i).level = 0;
				continue;
			}
			users.get(i).level = 10;
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < 10; k++) {
					if (highest[j][k] > users.get(i).results[j]
							&& users.get(i).results[j] != -1) {
						users.get(i).level = Math.min(users.get(i).level, k);
					}
				}
			}

		}
		ArrayList<String> answer = new ArrayList<String>();
		boolean[] villain = new boolean[n];
		for (int i = 0; i < n; i++) {
			Contestant first = users.get(i);
			for (int j = 0; j < n; j++) {
				Contestant second = users.get(j);
				if (sus[i][j] && first.ip == second.ip
						&& first.rating > second.rating
						&& first.rating == second.level) {
					// System.err.println(first.name + " " + second.name);
					villain[i] = true;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			if (villain[i]) {
				answer.add(users.get(i).name);
			}
		}
		Collections.sort(answer);
		out.println(answer.size());
		for (int i = 0; i < answer.size(); i++) {
			out.println(answer.get(i));
		}
	}

	public void updateScores(Contestant c) {
		for (int i = 0; i < m; i++) {
			highest[i][c.rating] = Math.max(highest[i][c.rating], c.results[i]);
		}
	}

	public void run() {
		try {
			Locale.setDefault(Locale.US);
			br = new BufferedReader(new FileReader("coincidence.in"));
			out = new PrintWriter("coincidence.out");

			solve();

			out.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}
