while True:

    N, M = list(map(int, input().split()))

    if (N, M) == (0, 0):

        break

    rules = []

    for _ in range(N):

        f, p1, p2 = input().split()

        rules += [(f, p1+p2)]

    rules = rules[::-1]

    def check(s):

        def match(pattern, address):

            for p, a in zip(pattern, address):

                if p == "?":

                    continue

                if p != a:

                    return False

            return True

        a1, a2, _ = s.split()

        for flag, pattern in rules:

            if flag == "permit" and match(pattern, a1+a2):

                return True

            if flag == "deny" and match(pattern, a1+a2):

                return False

    ans = list(filter(check, [input() for _ in range(M)]))

    print(len(ans))

    if len(ans) != 0:

        print("\n".join(ans))
import re

while True:

    N, M = list(map(int, input().split()))

    if (N, M) == (0, 0):

        break

    rules = []

    for _ in range(N):

        f, p1, p2 = input().replace("?", "\d").split()        

        rules += [(f, re.compile(p1+p2))]

    rules = rules[::-1]

    def check(s):

        a1, a2, _ = s.split()

        a = a1 + a2

        for f, r in rules:

            if f == "permit" and r.match(a):

                return True

            if f == "deny" and r.match(a):

                return False

    ans = list(filter(check, [input() for _ in range(M)]))

    print(len(ans))

    if len(ans) != 0:

        print("\n".join(ans))
while True:
    N, M = list(map(int, input().split()))
    if (N, M) == (0, 0):
        break
    rules = []
    for _ in range(N):
        f, p1, p2 = input().split()
        rules += [(f, p1 + p2)]
    rules = rules[::-1]

    def check(s):
        def match(pattern, address):
            for p, a in zip(pattern, address):
                if p == "?":
                    continue
                if p != a:
                    return False
            return True

        a1, a2, _ = s.split()
        for flag, pattern in rules:
            if flag == "permit" and match(pattern, a1 + a2):
                return True
            if flag == "deny" and match(pattern, a1 + a2):
                return False

    ans = list(filter(check, [input() for _ in range(M)]))
    print(len(ans))
    if len(ans) != 0:
        print("\n".join(ans))

import re

while True:
    N, M = list(map(int, input().split()))
    if (N, M) == (0, 0):
        break
    rules = []
    for _ in range(N):
        f, p1, p2 = input().replace("?", "\d").split()
        rules += [(f, re.compile(p1 + p2))]
    rules = rules[::-1]

    def check(s):
        a1, a2, _ = s.split()
        a = a1 + a2
        for f, r in rules:
            if f == "permit" and r.match(a):
                return True
            if f == "deny" and r.match(a):
                return False

    ans = list(filter(check, [input() for _ in range(M)]))
    print(len(ans))
    if len(ans) != 0:
        print("\n".join(ans))

+import re + -        f, p1, p2 = input().split() -        rules += [(f, p1 + p2)] +        f, p1, p2 = input().replace("?", "\d").split() +        rules += [(f, re.compile(p1 + p2))] -        def match(pattern, address): -            for p, a in zip(pattern, address): -                if p == "?": -                    continue -                if p != a: -                    return False -            return True - -        for flag, pattern in rules: -            if flag == "permit" and match(pattern, a1 + a2): +        a = a1 + a2 +        for f, r in rules: +            if f == "permit" and r.match(a): -            if flag == "deny" and match(pattern, a1 + a2): +            if f == "deny" and r.match(a):