Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef Watson uses a social network called ChefBook, which has a new feed consisting of posts by his friends. Each post can be characterized by f - the identifier of the friend who created the post, p - the popularity of the post(which is pre-calculated by ChefBook platform using some machine learning algorithm) and s - the contents of the post which is a string of lower and uppercase English alphabets.
Also, Chef has some friends, which he has marked as special.

The algorithm used by ChefBook for determining the order of posts in news feed is as follows:
Posts of special friends should be shown first, irrespective of popularity. Among all such posts the popular ones should be shown earlier.
Among all other posts, popular posts should be shown earlier.
Given, a list of identifiers of Chef's special friends and a list of posts, you have to implement this algorithm for engineers of ChefBook and output the correct ordering of posts in the new feed. 

------ Input ------ 

First line contains N, number of special friends of Chef and M, the number of posts. Next line contains N integers A_{1}, A_{2}, ..., A_{N} denoting the identifiers of special friends of Chef. Each of the next M lines contains a pair of integers and a string denoting f, p and s, identifier of the friend who created the post, the popularity of the post and the contents of the post, respectively. It is guaranteed that no two posts have same popularity, but the same friend might make multiple posts.

------ Output ------ 

Output correct ordering of posts in news feed in M lines. Output only the contents of a post.

------ Constraints ------ 

$1 ≤ N, M ≤ 10^{3}$
$1 ≤ A_{i}, f, p ≤  10^{5}$
$1 ≤ length(s) ≤  100$

----- Sample Input 1 ------ 
2 4

1 2

1 1 WhoDoesntLoveChefBook

2 2 WinterIsComing

3 10 TheseViolentDelightsHaveViolentEnds

4 3 ComeAtTheKingBestNotMiss
----- Sample Output 1 ------ 
WinterIsComing

WhoDoesntLoveChefBook

TheseViolentDelightsHaveViolentEnds

ComeAtTheKingBestNotMiss
----- explanation 1 ------ 
Friends $1$ and $2$ are special. Among them, friend $2$'s post has more popularity. Thus, the first two posts of the feed would be of friend $2$ and $1$ respectively.  
From remaining friends, i.e., friends $3$ and $4$, friend $4$'s post has more popularity. Thus, it would be shown earlier in the feed.  
The final ordering of posts is $2\rightarrow 1\rightarrow 4\rightarrow 3$.

Difficulty: MEDIUM_HARD

Solutions:
import heapq
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
frenq = []
nonfrenq = []
for _ in range(m):
	cn = input().split()
	(f, p, s) = (int(cn[0]), int(cn[1]), cn[2])
	if f in a:
		heapq.heappush(frenq, (-p, s))
	else:
		heapq.heappush(nonfrenq, (-p, s))
while frenq:
	(v, cont) = heapq.heappop(frenq)
	print(cont)
while nonfrenq:
	(v, cont) = heapq.heappop(nonfrenq)
	print(cont)


import sys
import copy
import math
import heapq
import bisect
import statistics
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from heapq import heapify, heappush, heappop
from itertools import permutations, combinations
from collections import Counter, OrderedDict, deque
from bisect import bisect_left as bl, bisect_right as br
from functools import lru_cache, reduce
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strr = lambda : input().strip()
jl = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
muli = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 1000000007
(n, m) = map(int, input().split())
a = list(map(str, input().split()))
fposts = []
nposts = []
for _ in range(m):
	(f, p, s) = map(str, input().split())
	if f in a:
		fposts.append([int(p), s])
	else:
		nposts.append([int(p), s])
fposts.sort(reverse=True)
nposts.sort(reverse=True)
for line in fposts + nposts:
	print(line[1])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
l1 = []
l2 = []
for i in range(m):
	(f, p, s) = map(str, input().split())
	if int(f) in a:
		l1.append((int(p), s))
	else:
		l2.append((int(p), s))
l1.sort()
l1.reverse()
l2.sort()
l2.reverse()
ans = l1 + l2
for i in range(m):
	print(ans[i][1])


(n, tc) = map(int, input().split())
a = list(map(int, input().split()))
special = []
non_ = []
for _ in range(tc):
	(f, p, s) = map(str, input().split())
	f = int(f)
	p = int(p)
	if f in a:
		special.append((f, p, s))
	else:
		non_.append((f, p, s))

def po(i):
	return i[1]
special.sort(reverse=True, key=po)
non_.sort(reverse=True, key=po)
for i in special:
	print(i[2])
for i in non_:
	print(i[2])


(N, M) = map(int, input().split())
S = set(map(int, input().split()))
SL = []
NL = []
for m in range(M):
	parts = input().split()
	f = int(parts[0])
	pop = int(parts[1])
	pos = parts[2]
	if f in S:
		SL.append((pop, f, pos))
	else:
		NL.append((pop, f, pos))
SL.sort(reverse=True)
NL.sort(reverse=True)
SO = [pos for (pop, f, pos) in SL]
NO = [pos for (pop, f, pos) in NL]
print('\n'.join(SO))
print('\n'.join(NO))


(n, m) = list(map(int, input().split()))
special = set(list(map(int, input().split())))
(sp_list, rem, ans) = ([], [], [])
for _ in range(m):
	(f, p, s) = input().split()
	(f, p) = (int(f), int(p))
	if f in special:
		sp_list.append([p, s])
	else:
		rem.append([p, s])
sp_list.sort(reverse=True)
rem.sort(reverse=True)
ans = sp_list + rem
for i in ans:
	print(i[1])


(n, m) = list(map(int, input().split()))
sp_friends = list(map(int, input().split()))
sp_friends_posts = []
other_posts = []
for _ in range(m):
	inp = input().split()
	f = int(inp[0])
	p = int(inp[1])
	s = inp[2]
	if f in sp_friends:
		sp_friends_posts.append((p, s))
	else:
		other_posts.append((p, s))
popularity = lambda x: x[0]
sp_friends_posts.sort(key=popularity, reverse=True)
other_posts.sort(key=popularity, reverse=True)
for (p, s) in sp_friends_posts + other_posts:
	print(s)


class NewsFeed:

	def __init__(self, id, popularity, post):
		self.id = id
		self.popularity = popularity
		self.post = post

	def getPost(self):
		return self.post

	def getPopularity(self):
		return self.popularity
(N, M) = map(int, input().strip().split())
specialFriends = input().split()
specialPosts = []
otherPosts = []
for i in range(M):
	(id, popularity, post) = input().strip().split()
	if id in specialFriends:
		specialPosts.append(NewsFeed(id, popularity, post))
	else:
		otherPosts.append(NewsFeed(id, popularity, post))
for result in sorted(specialPosts, key=lambda x: int(x.getPopularity()), reverse=True):
	print(result.post)
for result in sorted(otherPosts, key=lambda x: int(x.getPopularity()), reverse=True):
	print(result.post)


(n, m) = map(int, input().split())
f = list(map(int, input().split()))
m_ele = []
f_ele = []
for i in range(m):
	(a, b, c) = input().split()
	a = int(a)
	b = int(b)
	if a in f:
		f_ele.append([b, c])
	else:
		m_ele.append([b, c])
f_ele.sort(reverse=True)
m_ele.sort(reverse=True)
for i in f_ele:
	print(i[1])
for i in m_ele:
	print(i[1])


(n, m) = map(int, input().split())
spl = list(map(int, input().split()))
l = []
spll = []
for i in range(m):
	(a, b, c) = map(str, input().split())
	a = int(a)
	b = int(b)
	if a in spl:
		spll.append([b, c])
	else:
		l.append([b, c])
spll = sorted(spll, reverse=True)
l = sorted(l, reverse=True)
for i in spll:
	print(i[1])
for i in l:
	print(i[1])


(n, m) = map(int, input().split())
a = set(input().split())
friends = []
others = []
for i in range(m):
	(f, p, s) = list(input().split())
	p = int(p)
	if f in a:
		friends.append([p, s])
	else:
		others.append([p, s])
friends.sort(reverse=True)
others.sort(reverse=True)
for i in friends + others:
	print(i[1])


def criteria(l):
	return -int(l[1])
(n, m) = map(int, input().split(' '))
sf = list(input().split(' '))
sfl = list()
nl = list()
for i in range(m):
	temp = list(input().split(' '))
	if temp[0] in sf:
		sfl.append(temp)
	else:
		nl.append(temp)
sfl.sort(key=criteria)
nl.sort(key=criteria)
for i in sfl:
	print(i[-1])
for i in nl:
	print(i[-1])


(n, m) = map(int, input().split())
a = list(map(str, input().split()))
fposts = []
nposts = []
for _ in range(m):
	(f, p, s) = map(str, input().split())
	if f in a:
		fposts.append([int(p), s])
	else:
		nposts.append([int(p), s])
fposts.sort(reverse=True)
nposts.sort(reverse=True)
for line in fposts + nposts:
	print(line[1])


(n, t) = map(int, input().split())
l = list(map(int, input().strip().split()))[:n]
sp = []
nm = []
while t != 0:
	(f, p, s) = input().split()
	f = int(f)
	p = int(p)
	if f in l:
		sp.append((p, f, s))
	else:
		nm.append((p, f, s))
	t -= 1
sp.sort(reverse=True)
nm.sort(reverse=True)
for i in sp:
	print(i[2])
for i in nm:
	print(i[2])


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
sfl = []
nfl = []
for i in range(m):
	(k, l, s) = input().split()
	f = int(k)
	p = int(l)
	if f in a:
		sfl.append((f, p, s))
	else:
		nfl.append((f, p, s))
sfl.sort(key=lambda x: x[1], reverse=True)
nfl.sort(key=lambda x: x[1], reverse=True)
for i in sfl:
	print(i[2])
for i in nfl:
	print(i[2])


(n, m) = map(int, input().split())
special = list(map(int, input().split()))
lst = [input().split() for _ in range(m)]
for i in lst:
	(i[0], i[1]) = (int(i[0]), int(i[1]))
lst.sort(key=lambda x: x[1], reverse=True)
a1 = []
a2 = []
for i in lst:
	if i[0] in special:
		a1.append(i[2])
	else:
		a2.append(i[2])
for i in a1 + a2:
	print(i)


(n, m) = [int(i) for i in input().split()]
sp = [int(i) for i in input().split()]
x = []
y = []
for i in range(m):
	(f, p, s) = [int(i) if i.isdigit() else i for i in input().split()]
	if f in sp:
		x.append((f, p, s))
	else:
		y.append((f, p, s))
x = sorted(x, key=lambda z: z[1], reverse=True)
y = sorted(y, key=lambda z: z[1], reverse=True)
ans = x + y
for i in ans:
	print(i[2])


import sys
(N, M) = map(int, input().split())
F = [int(f) for f in input().strip().split(' ')]
(spf, ot) = ([], [])
for m in range(M):
	P = [m for m in input().strip().split(' ')]
	if int(P[0]) in F:
		spf.append([P[1], P[2]])
	else:
		ot.append([P[1], P[2]])
for i in range(len(spf) - 1):
	for j in range(i + 1, len(spf)):
		if int(spf[i][0]) < int(spf[j][0]):
			t = spf[i][0]
			T = spf[i][1]
			spf[i][0] = spf[j][0]
			spf[i][1] = spf[j][1]
			spf[j][0] = t
			spf[j][1] = T
for i in range(len(ot) - 1):
	for j in range(i + 1, len(ot)):
		if int(ot[i][0]) < int(ot[j][0]):
			t = ot[i][0]
			T = ot[i][1]
			ot[i][0] = ot[j][0]
			ot[i][1] = ot[j][1]
			ot[j][0] = t
			ot[j][1] = T
for s in spf:
	print(s[1])
for o in ot:
	print(o[1])


(m, n) = map(int, input().split())
l = list(map(int, input().split()))
(d1, d2, d3) = ({}, {}, {})
(l2, l3) = ([], [])
for i in l:
	d1[i] = True
for __ in range(n):
	(a1, a2, a3) = map(str, input().split())
	(a1, a2) = (int(a1), int(a2))
	if a1 in d1:
		if a2 in d2:
			d2[a2].append(a3)
		else:
			d2[a2] = [a3]
		l2.append(a2)
	else:
		if a2 in d3:
			d3[a2].append(a3)
		else:
			d3[a2] = [a3]
		l3.append(a2)
l2.sort(reverse=True)
l3.sort(reverse=True)
for i in l2:
	for j in d2[i]:
		print(j)
for i in l3:
	for j in d3[i]:
		print(j)


(n, m) = map(int, input().split())
spf = list(input().split())
post = []
ans1 = []
ans2 = []
for _ in range(m):
	(x, y, z) = input().split()
	x = int(x)
	y = int(y)
	post.append((x, y, z))
post.sort(key=lambda x: x[1], reverse=True)
for f in post:
	if str(list(f)[0]) in spf:
		ans1.append(f[2])
	else:
		ans2.append(f[2])
for a in ans1:
	print(a)
for a in ans2:
	print(a)


(a, n) = map(int, input().split())
s1 = list(map(int, input().split()))
arr = []
for i in range(n):
	(e, f, g) = list(input().split())
	b = [int(e), int(f), g]
	if b[0] in s1:
		b[1] += 100000
	arr.append(b)
arr.sort(key=lambda x: x[1], reverse=True)
for el in arr:
	print(el[2])


def solver(normal_post_list, special_post_list):
	special_post_list.sort(key=lambda x: int(x[1]), reverse=True)
	normal_post_list.sort(key=lambda x: int(x[1]), reverse=True)
	for element in special_post_list:
		print(element[2])
	for element in normal_post_list:
		print(element[2])
(n, m) = map(int, input().split())
special_friends = list(map(int, input().split()))
normal_post_list = []
special_post_list = []
for i in range(m):
	post_info = input().split()
	if int(post_info[0]) in special_friends:
		special_post_list.append(post_info)
	else:
		normal_post_list.append(post_info)
solver(normal_post_list, special_post_list)


from collections import OrderedDict
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in range(m):
	s.append(input())
specialDict = dict()
nonSpecialDict = dict()
for item in s:
	x = item.split()
	if int(x[0]) in a:
		specialDict[int(x[1])] = x[2]
	else:
		nonSpecialDict[int(x[1])] = x[2]
sort_data_special = sorted(specialDict.items(), reverse=True)
sort_data_nonspecial = sorted(nonSpecialDict.items(), reverse=True)
for i in sort_data_special:
	print(i[1])
for i in sort_data_nonspecial:
	print(i[1])


from functools import cmp_to_key
(n, m) = [int(x) for x in input().split()]
a = set([int(x) for x in input().split()])
b = []
for i in range(m):
	b.append([x for x in input().split()])
	b[i][0] = int(b[i][0])
	b[i][1] = int(b[i][1])

def comp(x, y):
	if x[0] in a:
		if y[0] in a:
			return y[1] - x[1]
		return -1
	elif y[0] in a:
		return 1
	return y[1] - x[1]
b.sort(key=cmp_to_key(comp))
for x in b:
	print(x[2])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
special = []
nonSpecial = []
for i in range(m):
	(iid, pop, post) = map(str, input().split())
	iid = int(iid)
	pop = int(pop)
	if iid in a:
		special.append([pop, post])
	else:
		nonSpecial.append([pop, post])
special.sort(reverse=True)
nonSpecial.sort(reverse=True)
for i in special:
	print(i[1])
for i in nonSpecial:
	print(i[1])


(n, m) = map(int, input().split())
sf = list(map(int, input().split()))
dic = []
for i in range(m):
	sl = input().split()
	dic.append((int(sl[0]), int(sl[1]), sl[2]))
dic.sort(key=lambda x: x[1], reverse=True)
for i in dic:
	if i[0] in sf:
		print(i[2])
for i in dic:
	if i[0] not in sf:
		print(i[2])


import heapq
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a = {e: True for e in a}
special = []
other = []
for i in range(m):
	(f, p, s) = input().split()
	if int(f) in a:
		heapq.heappush(special, (-int(p), s))
	else:
		heapq.heappush(other, (-int(p), s))
for i in range(m):
	if len(special) > 0:
		(p, s) = heapq.heappop(special)
	else:
		(p, s) = heapq.heappop(other)
	print(s)


(n, m) = map(int, input().split())
spl = list(map(int, input().split()))
d = {k: True for k in spl}
ds = {}
dn = {}
for _ in range(m):
	l = input().split()
	if int(l[0]) in d:
		ds[int(l[1])] = l[2]
	else:
		dn[int(l[1])] = l[2]
ks = list(ds.keys())
kn = list(dn.keys())
ks.sort()
kn.sort()
ks.reverse()
kn.reverse()
for k in ks:
	print(ds[k])
for k in kn:
	print(dn[k])


(n, m) = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
arr1 = []
arr2 = []
for i in range(m):
	(f, p, s) = input().split()
	f = int(f)
	p = int(p)
	if f in arr:
		arr1.append((p, f, s))
	else:
		arr2.append((p, f, s))
arr1.sort(reverse=True)
arr2.sort(reverse=True)
for item in arr1:
	print(item[2])
for item in arr2:
	print(item[2])


(n, m) = list(map(int, input().split()))
a = list(input().split())
friend_posts = []
other_posts = []
for _ in range(m):
	(f, p, s) = list(input().split())
	if f in a:
		friend_posts.append([p, s])
	else:
		other_posts.append([p, s])
res = sorted(friend_posts, key=lambda x: int(x[0]), reverse=True) + sorted(other_posts, key=lambda y: int(y[0]), reverse=True)
for ele in res:
	print(ele[1])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
d1 = {}
d2 = {}
for i in range(m):
	(f, p, s) = input().split()
	(f, p) = (int(f), int(p))
	if f in a:
		d1[p] = s
	else:
		d2[p] = s
d1 = sorted(d1.items(), key=lambda kv: (kv[0], kv[1]), reverse=True)
d2 = sorted(d2.items(), key=lambda kv: (kv[0], kv[1]), reverse=True)
for v in d1:
	print(v[1])
for v in d2:
	print(v[1])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
gg = {}
for i in a:
	gg[i] = 0
all = []
for i in range(m):
	(f, p, s) = input().split()
	f = int(f)
	p = int(p)
	all.append([f, p, s])
popular = []
other = []
for people in all:
	if people[0] in gg:
		popular.append(people)
	else:
		other.append(people)
popular.sort(key=lambda x: x[1])
other.sort(key=lambda x: x[1])
popular = popular[::-1]
other = other[::-1]
for i in popular:
	print(i[2])
for i in other:
	print(i[2])


def takeSec(elem):
	return int(elem[1])
(n, m) = map(int, input().split())
a = input().split()
ap = []
an = []
for _ in range(m):
	x = input().split()
	if x[0] in a:
		ap.append(x)
	else:
		an.append(x)
ap.sort(key=takeSec, reverse=True)
an.sort(key=takeSec, reverse=True)
for i in ap:
	print(i[2])
for j in an:
	print(j[2])


from audioop import reverse
import sys
import collections
(n, m) = map(int, input().split(' '))
special_friend = set(map(int, input().split(' ')))
(special_dict, common_dict) = ({}, {})
for i in range(m):
	(f, p, s) = input().split(' ')
	(f, p) = (int(f), int(p))
	if f in special_friend:
		special_dict[p] = s
	else:
		common_dict[p] = s
special_dict = collections.OrderedDict(sorted(special_dict.items(), reverse=True))
common_dict = collections.OrderedDict(sorted(common_dict.items(), reverse=True))
for message in special_dict.values():
	print(message)
for message in common_dict.values():
	print(message)


(n, m) = list(map(int, input().split()))
fr = input().split()
dict1 = dict()
dict2 = dict()
for _ in range(m):
	(f, p, s) = input().split()
	if f in fr:
		dict1[int(p)] = s
	else:
		dict2[int(p)] = s
dict1 = dict(sorted(dict1.items(), key=lambda x: x[0]))
dict2 = dict(sorted(dict2.items(), key=lambda x: x[0]))
print(*list(dict1.values())[::-1], sep='\n')
print(*list(dict2.values())[::-1], sep='\n')


import heapq
(n, m) = list(map(int, input().split()))
specials = set(map(int, input().split()))
(specPosts, normPosts) = ([], [])
for i in range(m):
	(f, p, s) = input().split()
	if int(f) in specials:
		heapq.heappush(specPosts, (-int(p), s))
	else:
		heapq.heappush(normPosts, (-int(p), s))
while len(specPosts) != 0:
	(k, content) = heapq.heappop(specPosts)
	print(content)
while len(normPosts) != 0:
	(k, content) = heapq.heappop(normPosts)
	print(content)


(n, m) = map(int, input().split())
an = [int(item) for item in input().split()]
post = [[item for item in input().split()] for j in range(m)]
post.sort(key=lambda x: int(x[1]))
feed = []
for i in range(m - 1, -1, -1):
	for j in range(n):
		if str(an[j]) == post[i][0]:
			post[i][1] = -1
			feed.append(post[i][2])
for i in range(m - 1, -1, -1):
	if post[i][1] != -1:
		feed.append(post[i][2])
for i in range(m):
	print(feed.pop(0))


(n, m) = map(int, input().split())
a = set(list(map(int, input().split())))
arr = []
for i in range(m):
	z = list(input().split())
	f = int(z[0])
	p = int(z[1])
	s = z[2]
	arr.append([f, p, s])
arr = sorted(arr, key=lambda x: x[1], reverse=True)
for (f, p, s) in arr:
	if f in a:
		print(s)
for (f, p, s) in arr:
	if f not in a:
		print(s)


def func(x):
	return x[1]
(n, m) = map(int, input().split())
sfrnd = list(map(int, input().split()))
(sfrnd_post, frnd_post) = ([], [])
for i in range(m):
	pst = list(input().split())
	(pst[0], pst[1]) = (int(pst[0]), int(pst[1]))
	if pst[0] in sfrnd:
		sfrnd_post.append(pst)
	else:
		frnd_post.append(pst)
sfrnd_post = sorted(sfrnd_post, key=func, reverse=True)
frnd_post = sorted(frnd_post, key=func, reverse=True)
for x in sfrnd_post:
	print(x[2])
for x in frnd_post:
	print(x[2])


(n, m) = map(int, input().split())
spf = list(map(int, input().split(' ')))
dict = {}
for i in range(m):
	(f, p, s) = input().split(' ')
	dict[int(f), int(p)] = s
li = []
li2 = []
for i in spf:
	for j in dict:
		if j[0] == i:
			li2.append((j[1], j[0]))
li2.sort(reverse=True)
for k in li2:
	print(dict[k[1], k[0]])
	dict.pop((k[1], k[0]))
for j in dict:
	if dict[j] == None:
		continue
	else:
		li.append((j[1], j[0]))
li.sort(reverse=True)
for k in li:
	print(dict[k[1], k[0]])


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
special = []
other = []
for i in range(m):
	(f, p, s) = [x for x in input().split()]
	if int(f) in a:
		special.append([int(p), s])
	else:
		other.append([int(p), s])
special.sort()
other.sort()
for i in range(len(special)):
	print(special[len(special) - 1 - i][1])
for i in range(len(other)):
	print(other[len(other) - 1 - i][1])


(n, m) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
special = {}
sparr = []
other = {}
otarr = []
for i in range(m):
	(f, p, s) = input().split()
	f = int(f)
	p = int(p)
	if f in arr:
		special[p] = s
		sparr.append(p)
	else:
		other[p] = s
		otarr.append(p)
otarr.sort(reverse=True)
sparr.sort(reverse=True)
for i in sparr:
	print(special[i])
for i in otarr:
	print(other[i])


(n, m) = map(int, input().split())
N = list(map(int, input().split()))
friend = []
popularity = []
while m > 0:
	l1 = input().split()
	f = int(l1[0])
	p = int(l1[1])
	msg = l1[2]
	if f in N:
		friend.append([p, msg])
	else:
		popularity.append([p, msg])
	m -= 1
friend.sort(reverse=True)
popularity.sort(reverse=True)
for i in friend:
	print(i[1])
for i in popularity:
	print(i[1])


(n, m) = map(int, input().split())
arr_n = list(map(int, input().split()))
lst_popu = []
lst_non_popu = []
dct = {}
dct_non = {}
while m > 0:
	lst = input().split()
	iden = int(lst[0])
	popu = int(lst[1])
	string = lst[2]
	if iden in arr_n:
		dct[popu] = string
		lst_popu.append(popu)
	else:
		dct_non[popu] = string
		lst_non_popu.append(popu)
	m -= 1
lst_popu.sort(reverse=True)
lst_non_popu.sort(reverse=True)
for i in range(len(lst_popu)):
	print(dct[lst_popu[i]])
for i in range(len(lst_non_popu)):
	print(dct_non[lst_non_popu[i]])


(n, m) = map(int, input().split())
nli = list(map(int, input().split()))
fdict = dict()
odict = dict()
for _ in range(m):
	(f, p, s) = map(str, input().split())
	if int(f) in nli:
		fdict[int(p)] = s
	else:
		odict[int(p)] = s
resf = {val[0]: val[1] for val in sorted(fdict.items(), key=lambda x: (-x[0], x[1]))}
reso = {val[0]: val[1] for val in sorted(odict.items(), key=lambda x: (-x[0], x[1]))}
for key in resf.values():
	print(key)
for key in reso.values():
	print(key)


(N, M) = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
new = []
for i in range(M):
	(f, p, s) = map(str, input().split())
	new.append([int(p), int(f), s])
k = sorted(new, reverse=True)
newest = []
for (p, f, s) in k:
	if f in arr:
		print(s)
for (p, f, s) in k:
	if f not in arr:
		print(s)


for _ in range(1):
	(N, M) = list(map(int, input().split()))
	s_friends = set(map(str, input().split()))
	s_post = []
	n_post = []
	for i in range(M):
		(f, p, s) = list(map(str, input().split()))
		if f in s_friends:
			s_post.append([int(p), s])
		else:
			n_post.append([int(p), s])
	s_post.sort(reverse=True)
	n_post.sort(reverse=True)
	for ele in s_post:
		print(ele[1])
	for ele in n_post:
		print(ele[1])


from collections import defaultdict
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
dic = defaultdict(list)
for _ in range(m):
	(f, p, t) = map(str, input().split())
	dic[int(f)].append((int(p), t))
priority = []
for x in l:
	priority += dic[x]
	del dic[x]
priority.sort(reverse=True)
rest = []
for x in dic:
	rest += dic[x]
rest.sort(reverse=True)
ans = []
for (x, y) in priority:
	ans.append(y)
for (x, y) in rest:
	ans.append(y)
for x in ans:
	print(x)


try:
	(n, m) = map(int, input().split())
	id = list(map(int, input().split()))
	a = []
	res = []
	temp = []
	for _ in range(m):
		(s, p, f) = input().split()
		a.append([int(p), int(s), f])
	a.sort(reverse=True)
	for i in range(m):
		if a[i][1] in id:
			res.append(a[i][2])
		else:
			temp.append(a[i])
	for val in temp:
		res.append(val[2])
	for v in res:
		print(v)
except:
	pass


def sk(pst):
	return pst[1]
(n, m) = map(int, input().split())
sf = list(map(int, input().split()))
psl = []
spsl = []
for i in range(m):
	(f, p, s) = map(str, input().split())
	(f, p) = (int(f), int(p))
	l = [f, p, s]
	if sf.count(f):
		spsl.append(l)
	else:
		psl.append(l)
spsl.sort(reverse=True, key=sk)
psl.sort(reverse=True, key=sk)
for i in range(len(spsl)):
	print(spsl[i][2])
for i in range(len(psl)):
	print(psl[i][2])


(n, m) = map(int, input().split())
sf = list(map(int, input().split()))
(F, P, S) = ([], [], [])
(SF, SP, SS) = ([], [], [])
for i in range(m):
	(f, p, s) = map(str, input().split())
	if int(f) in sf:
		SF.append(f)
		SP.append(int(p))
		SS.append(s)
	else:
		F.append(f)
		P.append(int(p))
		S.append(s)
SP_sorted = sorted(SP, reverse=True)
special_list = []
for i in range(len(SP)):
	ind = SP.index(SP_sorted[i])
	special_list.append(SS[ind])
normal_list = []
P_sorted = sorted(P, reverse=True)
for i in range(len(P)):
	ind = P.index(P_sorted[i])
	normal_list.append(S[ind])
ans = special_list + normal_list
for i in ans:
	print(i)


(n_fav, n_post) = input().split(' ')
n_fav = int(n_fav)
n_post = int(n_post)
fav_posts = []
posts = []
fav_friends = input().split(' ')
for i in range(n_post):
	c = input()
	(f, p, s) = c.split(' ')
	f = int(f)
	p = int(p)
	if str(f) in fav_friends:
		fav_posts.append({'friend': f, 'popularity': p, 'content': s})
	else:
		posts.append({'friend': f, 'popularity': p, 'content': s})
for i in sorted(fav_posts, key=lambda x: x['popularity'], reverse=True):
	print(i['content'])
for i in sorted(posts, key=lambda x: x['popularity'], reverse=True):
	print(i['content'])


(n, m) = map(int, input().split())
sp = set(input().split())
posts = []
for x in range(m):
	posts.append(input().split())
posts.sort(key=lambda x: int(x[1]), reverse=True)
for p in posts:
	if p[0] in sp:
		print(p[2])
for p in posts:
	if p[0] not in sp:
		print(p[2])


(n, m) = map(int, input().split())
c = set(list(map(int, input().split())))
l = []
for _ in range(m):
	(f, p, t) = input().split()
	l.append([int(f), int(p), t])
l = sorted(l, key=lambda x: x[1], reverse=True)
for x in l:
	if x[0] in c:
		print(x[2])
for x in l:
	if x[0] not in c:
		print(x[2])


(n, m) = list(map(int, input().split()))
sFId = list(map(int, input().split()))
specialPosts = []
normalPosts = []
i = 0
while i < m:
	tempPost = list(input().split())
	tempPost[0] = int(tempPost[0])
	tempPost[1] = int(tempPost[1])
	if tempPost[0] in sFId:
		specialPosts.append(tempPost)
	else:
		normalPosts.append(tempPost)
	i += 1
specialPosts.sort(reverse=True, key=lambda item: item[1])
normalPosts.sort(reverse=True, key=lambda item: item[1])
for post in specialPosts:
	print(post[2])
for post in normalPosts:
	print(post[2])


l = []
l1 = []
l2 = []
(n, m) = input().split(' ')
n = int(n)
m = int(m)
l = list(map(int, input().split(' ')))
count = 0
count1 = 0
for i in range(m):
	(f, p, s) = input().split(' ')
	f = int(f)
	p = int(p)
	if f in l:
		l1.append([p, s])
	else:
		l2.append([p, s])

def keys(it):
	return it[0]
l1.sort(key=keys, reverse=True)
l2.sort(key=keys, reverse=True)
for i in l1:
	print(i[1])
for i in l2:
	print(i[1])


from sys import stdin, stdout
import math
from collections import Counter
import functools
import time

def tr():
	return range(int(line()))

def sm():
	return map(int, line().split())

def ln():
	return list(sm())

def nl():
	return int(line())

def ssm():
	return map(str, line().split())

def line():
	return stdin.readline().rstrip()

def b(x):
	return bin(x).replace('0b', '')

def o(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + '\n')

def osp(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + ' ')

def ol(x):
	stdout.write(' '.join(map(str, x)))

def wline():
	return stdin.read().split()

def main():
	(n, m) = sm()
	friends = ln()
	posts = []
	friends_post = []
	for i in range(m):
		(f, p, c) = ssm()
		f = int(f)
		p = int(p)
		if f in friends:
			friends_post.append([f, p, c])
		else:
			posts.append([f, p, c])
	friends_post.sort(key=lambda x: x[1], reverse=True)
	posts.sort(key=lambda x: x[1], reverse=True)
	for c in friends_post:
		print(c[-1])
	for c in posts:
		print(c[-1])
main()


(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()))
list1 = []

def sorting(a):
	return a[1]
for i in range(m):
	(f, p, s) = list(map(str, input().split()))
	list1.append([int(f), int(p), s])
list1.sort(reverse=True, key=sorting)
special = []
for i in list1:
	if i[0] in l:
		print(i[2])
	else:
		special.append(i[2])
for i in special:
	print(i)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
t = []
for _ in range(m):
	(f, p, s) = map(str, input().split())
	t.append([int(f), int(p), s])
t.sort(reverse=True, key=lambda x: x[1])
special = []
for i in t:
	if i[0] in l:
		print(i[2])
	else:
		special.append(i)
for i in special:
	print(i[2])


(N, M) = map(int, input().split())
special_friend = list(map(int, input().strip().split()))[:N]
L = []
for i in range(M):
	(f, p, s) = map(str, input().split())
	f = int(f)
	p = int(p)
	L.append([f, p, s])
L.sort(key=lambda x: x[1], reverse=True)
for (f, p, s) in L:
	if f in special_friend:
		print(s)
for (f, p, s) in L:
	if f not in special_friend:
		print(s)


(n, m) = map(int, input().split())
spl = list(map(int, input().split()))
popa = []
for _ in range(m):
	(f, p, s) = map(str, input().split())
	f = int(f)
	p = int(p)
	popa.append([f, p, s])
popa = sorted(popa, key=lambda x: x[1], reverse=True)
for (f, p, s) in popa:
	if f in spl:
		print(s)
for (f, p, s) in popa:
	if f not in spl:
		print(s)


(sf, posts) = input().split()
sf = int(sf)
posts = int(posts)
sf_identifiers = list(map(int, input().split()))
special_posts = {}
normal_posts = {}
for i in range(posts):
	(f, p, s) = input().split()
	if int(f) in sf_identifiers:
		special_posts[int(p)] = s
	else:
		normal_posts[int(p)] = s
for values in sorted(special_posts.keys())[::-1]:
	print(special_posts[values])
for vals in sorted(normal_posts.keys())[::-1]:
	print(normal_posts[vals])


(n, m) = map(int, input().split())
sparr = set(list(map(int, input().split())))
parr = []
for _ in range(m):
	(f, p, s) = map(str, input().split())
	f = int(f)
	p = int(p)
	parr.append([f, p, s])
parr = sorted(parr, key=lambda x: x[1], reverse=True)
for (f, p, s) in parr:
	if f in sparr:
		print(s)
for (f, p, s) in parr:
	if f not in sparr:
		print(s)


(nf, np) = map(int, input().split())
f_list = set(list(map(int, input().split())))
d_list = []
for i in range(np):
	(f, p, s) = map(str, input().split())
	f = int(f)
	p = int(p)
	d_list.append([f, p, s])
d_list = sorted(d_list, key=lambda x: x[1], reverse=True)
for (f, p, s) in d_list:
	if f in f_list:
		print(s)
for (f, p, s) in d_list:
	if f not in f_list:
		print(s)


(n, m) = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a = []
for i in range(m):
	(f, p, s) = input().split()
	f = int(f)
	p = int(p)
	a.append([f, p, s])
a = sorted(a, key=lambda x: x[1], reverse=True)
for (f, p, s) in a:
	if f in c:
		print(s)
for (f, p, s) in a:
	if f not in c:
		print(s)