from collections import defaultdict
n = int(input())
disciplines = {}
for i in range(n):
name, partic = input().split(',')
disciplines[name] = int(partic)
disciplines[name] = {
'max': int(partic),
'players': defaultdict(dict),
'count': 0,
'maxScore': None,
'minScore': None,
}
def addPlayer(disc, name, score, fine):
disc['players'][score].update({fine : name})
disc['count'] += 1
if disc['count'] > 1:
disc['maxScore'] = max(disc['maxScore'], score)
disc['minScore'] = min(disc['minScore'], score)
else:
disc['maxScore'] = score
disc['minScore'] = score
m = int(input())
players = {}
for i in range(m):
name, discName, score, fine = input().split(',')
score = int(score)
fine = int(fine)
dis = disciplines[discName]
players = dis['players']
if dis['count'] < dis['max']:
addPlayer(dis, name, score, fine)
else:
if dis['minScore'] < score:
weak = players[dis['minScore']]
if len(weak) == 1:
dis['count'] -= 1
del players[dis['minScore']]
scores = sorted(players.keys())
dis['minScore'] = scores[0]
dis['maxScore'] = scores[-1]
addPlayer(dis, name, score, fine)
else:
fines = players[dis['minScore']].keys()
exfine = sorted(fines)[-1]
dis['count'] -= 1
del players[dis['minScore']][exfine]
addPlayer(dis, name, score, fine)
elif dis['minScore'] == score:
fines = players[dis['minScore']].keys()
exfine = sorted(fines)[-1]
if exfine > fine:
dis['count'] -= 1
del players[dis['minScore']][exfine]
addPlayer(dis, name, score, fine)
disciplines[discName] = dis
winners = []
for d in disciplines.values():
for s in d['players'].values():
for p in s.values():
winners.append(p)
winners.sort()
for w in winners:
print(w)