// Original C++ solution (slower):
#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cstring>

#include <map>

#include <utility>

#include <set>

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <sstream>

#include <complex>

#include <stack>

#include <queue>



using namespace std;

typedef long long LL;

typedef pair<string, string> PSS;

static const double EPS = 1e-8;

static const int INF = (int)1e8;



#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)

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

#define FOREQ(i,k,n) for (int i=(k); i<=(int)(n); ++i)

#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

#define FIND(m,w) ((m).find(w)!=(m).end())



bool eql(string s1,string s2){

  REP(i,s1.size()){

    if(s1[i]!=s2[i]&&s2[i]!='?'&&s1[i]!='?'){

      return false;

    }

  }

  return true;

}

struct Ans{

  string s1,s2,s3;

  Ans (string s1,string s2,string s3) : s1(s1),s2(s2),s3(s3) {}

};

int main(void){

  int n,m;

  while(cin>>n>>m,(n||m)){

    bool pord[1025];

    PSS adress[1025];

    REP(i,n){

      string tmp1,tmp2;

      cin>>tmp1;

      if(tmp1=="permit") pord[i] = true;

      else pord[i] = false;

      cin>>tmp1>>tmp2;

      adress[i] = PSS(tmp1,tmp2);

    }

    vector<Ans> ans;

    REP(i,m){

      string s1,s2,s3;

      cin>>s1>>s2>>s3;

      bool ligal = false;

      REP(i,n){

        if(eql(s1,adress[i].first)&&eql(s2,adress[i].second)){

          ligal = pord[i];

        }

      }

      if(ligal){

        ans.push_back(Ans(s1,s2,s3));

      }

    }

    cout<<ans.size()<<endl;

    REP(i,ans.size()){

      cout<<ans[i].s1<<" "<<ans[i].s2<<" "<<ans[i].s3<<endl;

    }

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <sstream>

#include <string>

#include <algorithm>

#include <vector>

#include <stack>

#include <queue>

#include <set>

#include <map>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>



using namespace std;



#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)

#define REP(i,n) FOR(i,0,n)

#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

bool eq(char* a, char* b, char* f, char* g){

  REP(i,8){

    if(a[i]!='?'&&a[i]!=f[i]){

      return false;

    }

    if(b[i]!='?'&&b[i]!=g[i]){

      return false;

    }

  }

  return true;

}



int main(){

  int n,m;

  while(scanf("%d %d",&n,&m),n||m){

    char a[1024][9];

    char b[1024][9];

    char c[1024][7];

    REP(i,n){

      scanf("%s %s %s",c[i],a[i],b[i]);

    }

    char f[1024][9],g[1024][9],h[1024][51];

    int count = 0;

    char* p[1024];

    char* q[1024];

    char* r[1024];

    REP(i,m){

      scanf("%s %s %s",f[i],g[i],h[i]);

      for(int j = n-1; j >= 0; j--){

        if(eq(a[j],b[j],f[i],g[i])){

          if(c[j][0]=='p'){

            p[count] = f[i];

            q[count] = g[i];

            r[count++] = h[i];

          }

          break;

        }

      }

    }



    printf("%d\n",count);

    REP(i,count){

      printf("%s %s %s\n",p[i],q[i],r[i]);

    }

  }

  return 0;

}

// Fastest known solution for this problem: