#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n, m;
vector<pair<int, pair<string, string> > >rule;

int judge(string src, string dst){
  for(int i = n-1; i >= 0; i--){
    int p;
    for(p = 0; p < 8; p++){
      if(rule[i].S.F[p] != '?' && src[p] != rule[i].S.F[p]) break;
      if(rule[i].S.S[p] != '?' && dst[p] != rule[i].S.S[p]) break;
    }
    if(p == 8) return rule[i].F;
  }
  return 0;
}

int main(){
  while(scanf("%d%d", &n, &m) && n+m){
    rule.clear();
    string r, src, dst, mes;
    rep(i, n){
      cin >> r >> src >> dst;
      rule.PB(MP((r[0]=='p')?1:0, MP(src, dst))); // permit 1, deny 0
    }

    vector<pair<string, pair<string, string> > >res;
    rep(i, m){
      cin >> src >> dst >> mes;
      if(judge(src, dst)) res.PB(MP(src, MP(dst, mes)));
    }

    printf("%d\n", (int)res.size());
    rep(i, res.size()) cout << res[i].F << " " << res[i].S.F << " " << res[i].S.S << endl;
  }
  return 0;
}