#include <string>
#include <vector>
#include <iostream>
using namespace std;
bool match(string s, string t) {
	for (int i = 0; i < 8; i++) {
		if (s[i] != '?' && s[i] != t[i]) return false;
	}
	return true;
}
int n, m; bool x[1111]; string r1[1111], r2[1111], a, b, c;
int main() {
	while (cin >> n >> m, n | m) {
		for (int i = 0; i < n; i++) {
			cin >> a >> r1[i] >> r2[i];
			if (a == "permit") x[i] = true;
			else x[i] = false;
		}
		int ret = 0; vector<string> v1, v2, v3;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> c;
			bool p1 = false, p2 = false;
			for (int j = n - 1; j >= 0; j--) {
				if (match(r1[j], a) && match(r2[j], b)) {
					if (x[j]) p1 = true;
					else p2 = true;
					break;
				}
			}
			if (p1) {
				v1.push_back(a);
				v2.push_back(b);
				v3.push_back(c);
				ret++;
			}
		}
		cout << ret << endl;
		for (int i = 0; i < ret; i++) {
			cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << endl;
		}
	}
}