__author__ = 'ckboss'
team1 = raw_input()
team2 = raw_input()
n = int(raw_input())
play1 = {}
play2 = {}
for i in range(0,100):
play1[i]=0
play2[i]=0
for i in range(0,n):
a,b,c,d = raw_input().split(' ')
c = int(c)
if b == "h":
if d == "y":
play1[c]+=1
if play1[c] == 2 :
print "%s %d %s" %(team1,c,a)
elif d=="r":
if play1[c] < 2:
print "%s %d %s" %(team1,c,a)
play1[c] = 9
elif b == "a":
if d == "y":
play2[c]+=1
if play2[c] ==2 :
print "%s %d %s" %(team2,c,a)
elif d=="r":
if play2[c] <2 :
print "%s %d %s" %(team2,c,a)
play2[c]=9