#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int n,m;
string b[1024],r[1024],p[1024][3],s,s2,s3;

int main(){
	while(cin>>n>>m&&(n+m)){
		rep(i,n){
			cin>>b[i]>>s>>s2;
			r[i]=s+s2;
		}
		int ans=0;
		rep(i,m){
			cin>>s>>s2>>s3;
			s+=s2;
			int f=0;
			for(int j=n-1;j>=0;j--){
				int f2=1;
				rep(k,16)if(s[k]!=r[j][k]&&r[j][k]!='?'){f2=0;}
				if(f2){f=(b[j]=="permit");break;}
			}
			if(f){p[ans][0]=s.substr(0,8);p[ans][1]=s2;p[ans++][2]=s3;}
		}
		cout<<ans<<endl;
		rep(i,ans)cout<<p[i][0]<<" "<<p[i][1]<<" "<<p[i][2]<<endl;
	}
}