n, m = map(int, raw_input().split()) a = [int(x) for x in raw_input().split()] special = [] normal = [] for i in range(m): f, p, s = map(str, raw_input().split()) f = int(f); p = int(p) if f in a: special.append((p, f, s)) else: normal.append((p, f, s)) special.sort(reverse = True) normal.sort(reverse = True) for post in special: print post[2] for post in normal: print post[2]