import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class twocaptains_vd {

	class Plan {
		String[] queries;
		int[] l;
		int[] r;
		int m;

		public Plan(int m) {
			this.m = m;
			queries = new String[m];
			l = new int[m];
			r = new int[m];
		}
	}

	void readPlan(Plan pl) throws IOException {
		for (int i = 0; i < pl.m; i++) {
			pl.queries[i] = nextToken();
			if (!pl.queries[i].equals("rum")) {
				pl.l[i] = nextInt();
				pl.r[i] = nextInt();
			}
		}
	}

	class IntervalTree {
		boolean[] taken;
		boolean[] hasone;
		int n;

		public IntervalTree(int n) {
			this.n = n;
			hasone = new boolean[4 * n + 1];
			taken = new boolean[4 * n + 1];
			taken[1] = true;
		}

		void set1(int l, int r) {
			update(1, n, l, r, 1, 1);
		}

		void set0(int l, int r) {
			update(1, n, l, r, 1, 0);
		}

		boolean check(int l, int r, int l1, int r1, int i) {
			if (l == l1 && r == r1 || taken[i]) {
				return !hasone[i];
			}
			int x = (l + r) / 2;
			if (r1 <= x) {
				return check(l, x, l1, r1, i * 2);
			} else {
				if (l1 > x) {
					return check(x + 1, r, l1, r1, i * 2 + 1);
				} else {
					return check(l, x, l1, x, i * 2)
							&& check(x + 1, r, x + 1, r1, i * 2 + 1);
				}
			}
		}

		void update(int l, int r, int l1, int r1, int i, int what) {
			if (l == l1 && r == r1) {
				taken[i] = true;
				hasone[i] = (what == 1);
				return;
			}
			if (taken[i]) {
				taken[i * 2] = true;
				taken[i * 2 + 1] = true;
				taken[i] = false;
				hasone[i * 2] = hasone[i];
				hasone[i * 2 + 1] = hasone[i];
			}
			int x = (l + r) / 2;
			if (r1 <= x) {
				update(l, x, l1, r1, i * 2, what);
			} else {
				if (l1 > x) {
					update(x + 1, r, l1, r1, i * 2 + 1, what);
				} else {
					update(l, x, l1, x, i * 2, what);
					update(x + 1, r, x + 1, r1, i * 2 + 1, what);
				}
			}
			hasone[i] = hasone[i * 2] | hasone[i * 2 + 1];
		}

		boolean check(int l, int r) {
			return check(1, n, l, r, 1);
		}

	}

	int n;

	boolean[][] getCan(Plan pl1, Plan pl2) {
		boolean[][] can = new boolean[pl1.m][pl2.m + 1];
		IntervalTree tree = new IntervalTree(n);
		for (int i = 0; i < pl1.m; i++) {
			if (pl1.queries[i].equals("send")) {
				can[i][0] = true;
				for (int j = 0; j < pl2.m; j++) {
					if (pl2.queries[j].equals("send")) {
						tree.set1(pl2.l[j], pl2.r[j]);
					} else {
						if (pl2.queries[j].equals("back")) {
							tree.set0(pl2.l[j], pl2.r[j]);
						}
					}
					can[i][j + 1] = tree.check(pl1.l[i], pl1.r[i]);
				}
			} else {
				Arrays.fill(can[i], true);
			}
		}
		return can;
	}

	String tostr(Plan p, int i) {
		if (p.queries[i].equals("rum")) {
			return p.queries[i];
		}
		return p.queries[i] + " " + p.l[i] + " " + p.r[i];
	}

	void count() throws IOException {
		n = nextInt();
		Plan pl1 = new Plan(nextInt());
		readPlan(pl1);
		Plan pl2 = new Plan(nextInt());
		readPlan(pl2);
		boolean[][] can1 = getCan(pl1, pl2);
		boolean[][] can2 = getCan(pl2, pl1);
		int[][] res = new int[pl1.m + 1][pl2.m + 1];
		if (pl1.m * pl2.m == 0) {
			out.println(0);
			return;
		}
		int[][] par = new int[pl1.m + 1][pl2.m + 1];

		for (int i = 0; i <= pl1.m; i++) {
			res[i][0] = i;
			par[i][0] = 1;
		}
		for (int i = 0; i <= pl2.m; i++) {
			res[0][i] = i;
			par[0][i] = 2;
		}

		for (int i = 1; i <= pl1.m; i++) {
			for (int j = 1; j <= pl2.m; j++) {
				res[i][j] = Integer.MAX_VALUE / 2;
				if (can2[j - 1][i]) {
					int add = res[i][j - 1] + ((i == pl1.m) ? 0 : 1);
					if (add < res[i][j]) {
						res[i][j] = add;
						par[i][j] = 2;
					}
				}
				if (can1[i - 1][j]) {
					int add = res[i - 1][j] + ((j == pl2.m) ? 0 : 1);
					if (add < res[i][j]) {
						res[i][j] = add;
						par[i][j] = 1;
					}
				}
				if (can1[i - 1][j] && can2[j - 1][i]) {
					if (res[i][j] > res[i - 1][j - 1]) {
						res[i][j] = res[i - 1][j - 1];
						par[i][j] = 3;
					}
				}
			}
		}
		out.println(res[pl1.m][pl2.m]);
		if (true) {
			return;
		};
		out.print(n + "\\n");
		out.print(pl1.m + "\\n");
		for (int i = 0; i < pl1.m; i++) {
			out.print(tostr(pl1, i) + "\\n");
		}
		out.print(pl2.m + "\\n");
		for (int i = 0; i < pl2.m; i++) {
			out.print(tostr(pl2, i) + "\\n");
		}
		out.println();
		ArrayList<String> res1 = new ArrayList<>();
		ArrayList<String> res2 = new ArrayList<>();
		int n = pl1.m;
		int m = pl2.m;
		while ((n > 0) || (m > 0)) {
			if (par[n][m] == 1) {
				res1.add(tostr(pl1, n - 1));
				if (m != pl2.m) {
					res2.add("rum");
				}
				n--;
			} else if (par[n][m] == 2) {
				res2.add(tostr(pl2, m - 1));
				if (n != pl1.m) {
					res1.add("rum");
				}
				m--;
			} else {
				res1.add(tostr(pl1, n - 1));
				res2.add(tostr(pl2, m - 1));
				n--;
				m--;
			}
		}
		out.println(res1.size());
		for (int i = res1.size() - 1; i >= 0; --i) {
			out.println(res1.get(i));
		}
		out.println(res2.size());
		for (int i = res2.size() - 1; i >= 0; --i) {
			out.println(res2.get(i));
		}
		Collections.reverse(res1);
		Collections.reverse(res2);
		if (true) {
			return;
		}
		if (res1.get(0) != "rum" && res2.get(0) != "rum") {
			int k = -pl1.m % 2 - pl2.m % 2;
			for (int i = 0; i < res1.size() - 1; i++) {
				if (!res1.get(i).equals("rum") && res1.get(i + 1).equals("rum")) {
					k++;
				}
			}
			for (int i = 0; i < res2.size() - 1; i++) {
				if (!res2.get(i).equals("rum") && res2.get(i + 1).equals("rum")) {
					k++;
				}
			}
			if (k > 5 && res[pl1.m][pl2.m] != 3) {
//				throw new AssertionError();
			}
		}

	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer str;
	String readline;

	String nextToken() throws IOException {
		while (str == null || !str.hasMoreTokens()) {
			readline = in.readLine();
			if (readline == null) {
				return null;
			} else {
				str = new StringTokenizer(readline);
			}
		}
		return str.nextToken();
	}

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

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

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

	void run() throws IOException {
		in = new BufferedReader(new FileReader("twocaptains.in"));
		out = new PrintWriter("twocaptains.out");
		count();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new twocaptains_vd().run();
	}
}