### constant

ALEN = 8

R_P = 0
R_D = 1

### subroutines

def adr_match?(adr, pat)
  for i in (0...ALEN)
    return false if pat[i] != '?' && adr[i] != pat[i]
  end
  true
end

def filter(n, rules, packet)
  sadr, dadr = packet

  (n - 1).downto(0) do |i|
    op, spat, dpat = rules[i]
    if adr_match?(sadr, spat) && adr_match?(dadr, dpat)
      return op
    end
  end

  R_D
end

### main

loop do
  n, m = gets.split.map(&:to_i)
  break if (n | m) == 0

  rules = []
  n.times do
    op, spat, dpat = gets.strip.split
    rules << [op == 'permit' ? R_P : R_D, spat, dpat]
  end
  #p rules

  packets = []

  m.times do
    packet = gets.strip.split

    if filter(n, rules, packet) == R_P
      packets << packet
    end
  end

  puts packets.length
  packets.each{|pkt| puts pkt.join(' ')}
end