A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.

Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?

More formally, if we denote the handle of the i-th person as h_{i}, then the following condition must hold: $\forall i, j(i < j) : h_{p_{i}} < h_{p_{j}}$.


-----Input-----

The first line contains an integer n (1 ≤ n ≤ 10^5) — the number of people.

The next n lines each contains two strings. The i-th line contains strings f_{i} and s_{i} (1 ≤ |f_{i}|, |s_{i}| ≤ 50) — the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.

The next line contains n distinct integers: p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n).


-----Output-----

If it is possible, output "YES", otherwise output "NO".


-----Examples-----
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3

Output
NO

Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2

Output
YES

Input
2
galileo galilei
nicolaus copernicus
2 1

Output
YES

Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10

Output
NO

Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10

Output
YES



-----Note-----

In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.

In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright.

Difficulty: MEDIUM
Tags: Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
l = []
for _ in range(int(input())):
	k = tuple(input().split())
	l.append(k)
o = list(map(int, input().split()))
c = ''
ans = True
for j in o:
	i = j - 1
	if l[i][0] > c and l[i][1] > c:
		if l[i][0] < l[i][1]:
			c = l[i][0]
		else:
			c = l[i][1]
	elif l[i][0] > c:
		c = l[i][0]
	elif l[i][1] > c:
		c = l[i][1]
	else:
		ans = False
		break
if ans:
	print('YES')
else:
	print('NO')


n = int(input())
a = [input().split() for i in range(n)]
b = list(map(int, input().split()))
b = [e - 1 for e in b]
a = [sorted(e) for e in a]
s = a[b[0]][0]
v = True
for i in range(n):
	if s <= a[b[i]][0]:
		s = a[b[i]][0]
	elif s <= a[b[i]][1]:
		s = a[b[i]][1]
	else:
		v = False
if v:
	print('YES')
else:
	print('NO')


n = int(input())
s = []
for i in range(n):
	s.append(input().split())
p = list(map(lambda x: int(x) - 1, input().split()))
prev = min(s[p[0]])
ok = True
for i in range(1, n):
	j = p[i]
	if prev < min(s[j]):
		prev = min(s[j])
	elif prev < max(s[j]):
		prev = max(s[j])
	else:
		ok = False
		break
print('YES' if ok else 'NO')


n = int(input())
l = [input().split() for i in range(n)]
p = [int(x) - 1 for x in input().split()]
cur = ''
poss = True
for i in range(n):
	per = l[p[i]]
	if per[0] > cur:
		if per[1] > cur:
			cur = min(per[0], per[1])
		else:
			cur = per[0]
	elif per[1] > cur:
		cur = per[1]
	else:
		poss = False
		break
print('YES' if poss else 'NO')


def main():
	n = int(input())
	t = []
	for i in range(n):
		t.append([elem for elem in input().split()])
	p = [int(num) - 1 for num in input().split()]
	last = min(t[p[0]])
	for i in range(1, n):
		if last < t[p[i]][0] and last < t[p[i]][1]:
			last = min(t[p[i]])
		elif last < t[p[i]][0] or last < t[p[i]][1]:
			if last < t[p[i]][0]:
				last = t[p[i]][0]
			elif last < t[p[i]][1]:
				last = t[p[i]][1]
		else:
			print('NO')
			return 0
	print('YES')
main()


from sys import stdin

def main():
	(l, a) = (stdin.read().splitlines(), '')
	for (b, c) in map(str.split, map(l.__getitem__, map(int, l.pop().split()))):
		if b < a > c:
			print('NO')
			return
		a = (b if a < b else c) if b < c else c if a < c else b
	print('YES')
main()


person_count = int(input())
person_list = [input().split(' ') for _ in range(person_count)]
order = [int(x) - 1 for x in input().split(' ')]
string = ''
for number in order:
	if string <= person_list[number][0] and string <= person_list[number][1]:
		string = min(person_list[number])
	elif string <= person_list[number][0]:
		string = person_list[number][0]
	elif string <= person_list[number][1]:
		string = person_list[number][1]
	else:
		print('NO')
		break
else:
	print('YES')


n = int(input())
l_n = list([input().split() for _ in range(n)])
o = list(map(int, input().split()))
l = 'A'
for n in range(n):
	l_n[o[n] - 1].sort()
	if l_n[o[n] - 1][0] > l:
		l = l_n[o[n] - 1][0]
	elif l_n[o[n] - 1][1] > l:
		l = l_n[o[n] - 1][1]
	else:
		print('NO')
		exit()
print('YES')


n = int(input())
names = []
for i in range(n):
	names.append(input().split())
p = [int(i) - 1 for i in input().split()]
last = min(names[p[0]][0], names[p[0]][1])
for i in range(1, n):
	a = names[p[i]]
	v = []
	if last < a[0]:
		v.append(a[0])
	if last < a[1]:
		v.append(a[1])
	if len(v) == 0:
		print('NO')
		break
	else:
		last = min(v)
else:
	print('YES')


def main():
	(n,) = read()
	names = []
	for i in range(n):
		names.extend([(x, i + 1) for x in read(1)])
	names.sort()
	p = read()
	i = 0
	j = 0
	while i < n and j < 2 * n:
		if names[j][1] == p[i]:
			i += 1
		j += 1
	if i == n:
		print('YES')
	else:
		print('NO')

def read(mode=2):
	inputs = input().strip()
	if mode == 0:
		return inputs
	if mode == 1:
		return inputs.split()
	if mode == 2:
		return list(map(int, inputs.split()))

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s, end='')
write(main())


n = int(input())
arr = []
for i in range(n):
	arr.append(sorted(input().split()))
to_get = [i - 1 for i in list(map(int, input().split()))]
ok = True
checked = ''
for i in to_get:
	if arr[i][0] > checked:
		curr = arr[i][0]
	else:
		curr = arr[i][1]
	if curr < checked:
		ok = False
		break
	checked = curr
if ok:
	print('YES')
else:
	print('NO')


from collections import defaultdict, deque, Counter, OrderedDict
from heapq import heappop, heappush

def main():
	n = int(input())
	(a, prev, check) = ([('', '')] * n, '', True)
	for i in range(n):
		a[i] = tuple(input().split())
	p = [int(i) - 1 for i in input().split()]
	for (i, v) in enumerate(p):
		if i == 0:
			prev = min(a[v][0], a[v][1])
		elif min(a[v][0], a[v][1]) > prev:
			prev = min(a[v][0], a[v][1])
		elif max(a[v][0], a[v][1]) > prev:
			prev = max(a[v][0], a[v][1])
		else:
			check = False
	print('YES' if check else 'NO')
main()


import sys
n = int(input())
a = [[''] * 2 for i in range(n)]
for i in range(n):
	(a[i][0], a[i][1]) = map(str, input().split())
p = list(map(int, input().split()))
fin = [''] * n
fin[0] = min(a[p[0] - 1][0], a[p[0] - 1][1])
for i in range(1, n):
	f = a[p[i] - 1][0]
	s = a[p[i] - 1][1]
	if f < fin[i - 1] and s < fin[i - 1]:
		print('NO')
		exit(0)
	elif min(f, s) < fin[i - 1] and max(f, s) > fin[i - 1]:
		fin[i] = max(f, s)
	elif f >= fin[i - 1] and s >= fin[i - 1]:
		fin[i] = min(f, s)
print('YES')


n = int(input())
fullnames = [[x for x in input().split(' ')] for _ in range(n)]
permutation = [int(x) for x in input().split()]
prevHandle = ''
validPermutation = True
for idx in permutation:
	name = fullnames[idx - 1][0]
	lastname = fullnames[idx - 1][1]
	if name > prevHandle and lastname > prevHandle:
		prevHandle = name if name < lastname else lastname
	elif name > prevHandle:
		prevHandle = name
	elif lastname > prevHandle:
		prevHandle = lastname
	else:
		validPermutation = False
		break
print('YES' if validPermutation else 'NO')


n = int(input())
inp = []
for i in range(n):
	inp += [list(map(str, input().rstrip().split()))]
p = list(map(int, input().rstrip().split()))
last = min(inp[p[0] - 1])
for i in range(1, n):
	minm = min(inp[p[i] - 1])
	maxm = max(inp[p[i] - 1])
	if minm >= last:
		last = minm
	elif maxm >= last:
		last = maxm
	else:
		print('NO')
		exit()
print('YES')


import sys
import queue
line = sys.stdin.readline()
n = int(line)
first = ['' for x in range(n)]
second = ['' for x in range(n)]
for i in range(0, n):
	line = sys.stdin.readline().split()
	first[i] = line[0]
	second[i] = line[1]
permutation = [int(x) for x in sys.stdin.readline().split()]
p = permutation[0] - 1
last = min(first[p], second[p])
can = True
for i in range(1, n):
	p = permutation[i] - 1
	if last < min(first[p], second[p]):
		last = min(first[p], second[p])
	elif last < max(first[p], second[p]):
		last = max(first[p], second[p])
	else:
		can = False
if can:
	print('YES')
else:
	print('NO')


n = int(input())
l = [None] * n
i = 0
while i < n:
	l[i] = input().split()
	i += 1
z = [int(px) for px in input().split()]
x = 1
y = True
a = min(l[z[0] - 1])
while x < n:
	u = l[z[x] - 1][0]
	v = l[z[x] - 1][1]
	if u < a and v < a:
		y = False
		break
	elif u > a and v > a:
		a = min(u, v)
	else:
		a = max(u, v)
	x += 1
if y:
	print('YES')
else:
	print('NO')


n = int(input())
name = ['' for i in range(n)]
sname = ['' for i in range(n)]
for i in range(n):
	(name[i], sname[i]) = input().split()
p = list(map(int, input().split()))
for i in range(n):
	p[i] -= 1
nm = [False for i in range(n)]
snm = [False for i in range(n)]
nm[0] = snm[0] = True
for i in range(1, n):
	if nm[i - 1]:
		if name[p[i]] > name[p[i - 1]]:
			nm[i] = True
		if sname[p[i]] > name[p[i - 1]]:
			snm[i] = True
	if snm[i - 1]:
		if name[p[i]] > sname[p[i - 1]]:
			nm[i] = True
		if sname[p[i]] > sname[p[i - 1]]:
			snm[i] = True
print('YES' if snm[-1] or nm[-1] else 'NO')


import sys
n = int(input())
name = []
for i in range(n):
	s = input().split()
	name.append(s)
p = list(map(int, input().split()))
for i in range(n):
	p[i] -= 1
new = [0] * n
for i in range(n):
	new[i] = name[p[i]]
now = min(new[0][0], new[0][1])
for i in range(1, n):
	tmp1 = min(new[i][0], new[i][1])
	tmp2 = max(new[i][0], new[i][1])
	if now < tmp1:
		now = tmp1
	elif now < tmp2:
		now = tmp2
	else:
		print('NO')
		sys.exit(0)
print('YES')


n = int(input())
person = []
person_tup = []
for i in range(n):
	(name, surname) = input().split()
	person_tup.append((name, surname))
p = list(map(int, input().split()))
flag = True
i = 0
for elem in p:
	person.append(person_tup[elem - 1][0])
	person.append(person_tup[elem - 1][1])
tmp = min(person[0], person[1])
for i in range(2, 2 * n - 1, 2):
	if tmp < person[i] and tmp < person[i + 1]:
		tmp = min(person[i], person[i + 1])
	elif tmp < person[i + 1]:
		tmp = person[i + 1]
	elif tmp < person[i]:
		tmp = person[i]
	else:
		flag = False
		break
if flag:
	print('YES')
else:
	print('NO')


def selectLeastGreater(p, prec):
	fn = p[0]
	ln = p[1]
	if fn >= prec and fn <= ln:
		return fn
	elif ln >= prec and ln <= fn:
		return ln
	elif fn >= prec:
		return fn
	elif ln >= prec:
		return ln
	else:
		return ''
n = int(input())
names = []
for _ in range(n):
	(fn, ln) = input().split()
	names.append((fn, ln))
A = list(map(int, input().split()))
names2 = []
for i in A:
	names2.append(names[i - 1])
prec = ''
YES = True
for i in range(n):
	new = selectLeastGreater(names2[i], prec)
	if len(new) == 0:
		print('NO')
		YES = False
		break
	else:
		prec = new
if YES:
	print('YES')


n = int(input())
l = []
f = 0
s3 = ''
for i in range(n):
	(s1, s2) = map(str, input().strip().split(' '))
	l.append([s1, s2])
lst = list(map(int, input().strip().split(' ')))
for j in range(n):
	if j == 0:
		s3 = min(l[lst[j] - 1][0], l[lst[j] - 1][1])
	else:
		s4 = l[lst[j] - 1][0]
		s5 = l[lst[j] - 1][1]
		if s4 > s5:
			(s4, s5) = (s5, s4)
		if s3 > s4 and s3 > s5:
			f = 1
			print('NO')
			break
		elif s3 < s4:
			s3 = s4
		else:
			s3 = s5
if f == 0:
	print('YES')


a = int(input())
l = []
l1 = []
for i in range(a):
	l.append('')
for i in range(a):
	lol = input().split()
	l1.append([lol[0], lol[1]])
l2 = input().split()
for i in range(a):
	l[i] = l1[int(l2[i]) - 1]
m = ''
work = 'YES'
for i in range(a):
	if min(l[i]) >= m:
		m = min(l[i])
	elif max(l[i]) >= m:
		m = max(l[i])
	else:
		work = 'NO'
		break
print(work)


n = int(input())
(first, last) = ([], [])
for i in range(n):
	(a, b) = map(str, input().split())
	first.append(a)
	last.append(b)
l = list(map(int, input().split()))
c = 1
s = min(first[l[0] - 1], last[l[0] - 1])
for i in range(1, n):
	if first[l[i] - 1] > s and last[l[i] - 1] > s:
		c += 1
		s = min(first[l[i] - 1], last[l[i] - 1])
	elif first[l[i] - 1] > s:
		c += 1
		s = first[l[i] - 1]
	elif last[l[i] - 1] > s:
		c += 1
		s = last[l[i] - 1]
	else:
		c = -1
		print('NO')
		exit()
print('YES')


n = int(input())
name = []
for i in range(n):
	name.append(sorted(input().split()))
p = [int(i) for i in input().split()]
sort = [name[p[0] - 1][0]]
for index in p[1:]:
	if name[index - 1][0] > sort[-1]:
		sort.append(name[index - 1][0])
	elif name[index - 1][1] > sort[-1]:
		sort.append(name[index - 1][1])
	else:
		print('NO')
		break
else:
	print('YES')


from itertools import product

def main():
	n = int(input())
	a = [[i, j] for _ in range(n) for (i, j) in (input().split(),)]
	pi = map(int, input().split())
	res = (True, True)
	prev = a[next(pi) - 1]
	l = lambda i, j: prev[i] < a[p - 1][j]
	for p in pi:
		res = tuple((any((l(i, j) for i in (0, 1) if res[i])) for j in (0, 1)))
		prev = a[p - 1]
		if not any(res):
			print('NO')
			return
	print('YES')
main()


(n, names) = (int(input()), [])
for i in range(n):
	x = sorted(input().split())
	names.append(x)
p = list(map(int, input().split()))
w = ''
for i in range(n):
	if names[p[i] - 1][0] > w:
		w = names[p[i] - 1][0]
	elif names[p[i] - 1][1] > w:
		w = names[p[i] - 1][1]
	else:
		print('NO')
		exit()
print('YES')


n = int(input())
i = 1
l = []
while i <= n:
	l.append(input().split())
	i += 1
a = [int(p) for p in input().split()]
i = 1

def t(l, a):
	w = min(l[a[0] - 1][0], l[a[0] - 1][1])
	del a[0]
	for p in a:
		k = min(l[p - 1][0], l[p - 1][1])
		if k > w:
			w = k
		elif max(l[p - 1][0], l[p - 1][1]) > w:
			w = max(l[p - 1][0], l[p - 1][1])
		else:
			return 'NO'
	return 'YES'
print(t(l, a))


n = int(input())
namelist = []
for i in range(n):
	namelist.append([str(x) for x in input().split()])
plist = [int(x) for x in input().split()]
limit = min(namelist[plist[0] - 1])
for i in range(n - 1):
	if limit < min(namelist[plist[i + 1] - 1]):
		limit = min(namelist[plist[i + 1] - 1])
	elif limit < max(namelist[plist[i + 1] - 1]):
		limit = max(namelist[plist[i + 1] - 1])
	else:
		print('NO')
		quit()
print('YES')


n = int(input())
arr = []
for i in range(n):
	arr.append(sorted(input().split()))
order = [int(x) for x in input().split()]
for (index, num) in enumerate(order):
	arr[num - 1] += [index]
arr.sort(key=lambda x: x[2])

def check(arr):
	cs = ''
	for (index, person) in enumerate(arr):
		if person[0] > cs:
			cs = person[0]
		elif person[1] > cs:
			cs = person[1]
		else:
			return 'NO'
	return 'YES'
print(check(arr))


n = int(input())
a = []
for i in range(n):
	s = list(input().split(' '))
	s.sort()
	a.append(s)
c = [int(x) - 1 for x in input().split()]
so = ''
err = 0
for i in range(n):
	if so == '':
		so = a[c[i]][0]
	elif a[c[i]][0] > so:
		so = a[c[i]][0]
	elif a[c[i]][1] > so:
		so = a[c[i]][1]
	else:
		err = 1
		break
if err:
	print('NO')
else:
	print('YES')


n = int(input())
names = [input().split() for _ in range(n)]
order = [int(i) - 1 for i in input().split()]
prev = 'a'
for i in order:
	handle = (n for n in sorted(names[i]) if n >= prev)
	prev = next(handle, None)
	if not prev:
		print('NO')
		break
else:
	print('YES')


n = int(input())
s = []
for i in range(n):
	s.append(input().split())
	if s[i][0] > s[i][1]:
		(s[i][0], s[i][1]) = (s[i][1], s[i][0])
x = [int(i) - 1 for i in input().split()]
tag = False
for i in range(1, n):
	if s[x[i - 1]][0] >= s[x[i]][1]:
		print('NO')
		tag = True
		break
	if s[x[i - 1]][0] >= s[x[i]][0]:
		s[x[i]][0] = s[x[i]][1]
if tag == False:
	print('YES')


from sys import *
n = int(input())
names = []
for i in range(n):
	names.append(input().split())
p = [names[x - 1] for x in list(map(int, input().split()))]
curr_handle = min(p[0][0], p[0][1])
for i in range(1, n):
	if curr_handle > p[i][0] and curr_handle > p[i][1]:
		print('NO')
		exit(0)
	else:
		min_curr = min(p[i][0], p[i][1])
		curr_handle = min_curr if curr_handle <= min_curr else max(p[i][0], p[i][1])
print('YES')


n = int(input())
d = {}
from sys import stdin
for i in range(n):
	(f, s) = map(str, stdin.readline().split())
	d[i + 1] = sorted([f, s])
lst = [*map(int, stdin.readline().split())]
x = min(d[lst[0]][0], d[lst[0]][1])
for (i, item) in enumerate(lst[1:]):
	(mn, mx) = (d[item][0], d[item][1])
	if mn > x:
		x = mn
	elif mx > x:
		x = mx
	else:
		from sys import exit
		print('NO')
		exit()
print('YES')


n = int(input())
arr = []
for i in range(n):
	(f, l) = list(map(str, input().split()))
	arr.append([f, l])
perm = list(map(int, input().split()))
prev = ''
f = 0
for i in perm:
	x = arr[i - 1][0]
	y = arr[i - 1][1]
	if x >= prev and y >= prev:
		prev = min(x, y)
	elif x >= prev and y < prev:
		prev = x
	elif x < prev and y >= prev:
		prev = y
	else:
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


n = int(input())
tmp = [input().split() for _ in range(n)]
p = list(map(int, input().split()))
l = [None] * n
for (i, v) in enumerate(p):
	l[i] = tmp[v - 1]
l[0] = min(l[0])
for i in range(1, n):
	if min(l[i]) > l[i - 1]:
		l[i] = min(l[i])
	elif max(l[i]) > l[i - 1]:
		l[i] = max(l[i])
	else:
		print('NO')
		break
else:
	print('YES')


n = int(input())
names = []
for i in range(n):
	names.append(input().split())
a = list(map(int, input().split()))
for i in range(n):
	a[i] -= 1
p = [0] * n
for i in range(n):
	p[a[i]] = i
ans = 'YES'
choice = 0
for i in range(n - 1):
	if choice == 0:
		if min(names[a[i]][0], names[a[i]][1]) > max(names[a[i + 1]][0], names[a[i + 1]][1]):
			ans = 'NO'
		elif min(names[a[i]][0], names[a[i]][1]) > names[a[i + 1]][0]:
			choice = 2
		elif min(names[a[i]][0], names[a[i]][1]) > names[a[i + 1]][1]:
			choice = 1
	elif choice == 1:
		if names[a[i]][0] > max(names[a[i + 1]][0], names[a[i + 1]][1]):
			ans = 'NO'
		elif names[a[i]][0] > names[a[i + 1]][0]:
			choice = 2
		elif names[a[i]][0] > names[a[i + 1]][1]:
			choice = 1
		else:
			choice = 0
	elif choice == 2:
		if names[a[i]][1] > max(names[a[i + 1]][0], names[a[i + 1]][1]):
			ans = 'NO'
		elif names[a[i]][1] > names[a[i + 1]][0]:
			choice = 2
		elif names[a[i]][1] > names[a[i + 1]][1]:
			choice = 1
		else:
			choice = 0
print(ans)


def greater(a, b):
	n = [b, a]
	m = sorted(n)
	if n[0] == m[0]:
		return True
	else:
		return False
n = int(input())
name = []
for i in range(n):
	name.append(sorted(list(input().split())))
p = list(map(int, input().split()))
r = 'YES'
result = [name[p[0] - 1][0]]
for i in range(1, n):
	j = p[i] - 1
	if greater(name[j][0], result[i - 1]):
		result.append(name[j][0])
	elif greater(name[j][1], result[i - 1]):
		result.append(name[j][1])
	else:
		r = 'NO'
		break
print(r)


n = int(input())
names = []
for i in range(n):
	names.append(input().split())
arr = list(map(int, input().split()))
prev = min(names[arr[0] - 1])
for i in range(1, n):
	if min(names[arr[i] - 1]) >= prev:
		prev = min(names[arr[i] - 1])
	elif max(names[arr[i] - 1]) >= prev:
		prev = max(names[arr[i] - 1])
	else:
		print('NO')
		break
else:
	print('YES')


def main():
	n = int(input())
	names = []
	for i in range(n):
		(first, last) = map(str, input().split())
		names.append((first, last))
	order = list(map(int, input().split()))
	last = ''
	for i in order:
		if not last:
			last = min(names[i - 1][0], names[i - 1][1])
		else:
			curr = min(names[i - 1][0], names[i - 1][1])
			if curr > last:
				last = curr
			else:
				curr = max(names[i - 1][0], names[i - 1][1])
				if curr > last:
					last = curr
				else:
					print('NO')
					return
	print('YES')
main()


def find_k_smallest(A, k):
	temp = A
	for j in range(k):
		min_el = A[0]
		min_ind = 0
		for (ind, el) in enumerate(temp):
			if el > min_el:
				min_el = el
				min_ind = ind
		temp[min_ind] = -float('inf')
	return (min_ind, min_el)

def sol():
	n = int(input())
	names = []
	for i in range(n):
		name = input()
		names.append(name)
	vec = [int(x) for x in input().split(' ')]
	handles = []
	for (ind, v) in enumerate(vec):
		if handles:
			handle1 = min(names[v - 1].split(' '))
			handle2 = max(names[v - 1].split(' '))
			if handle1 > handles[-1]:
				handles.append(handle1)
			elif handle2 > handles[-1]:
				handles.append(handle2)
			else:
				print('NO')
				return 'NO'
		else:
			handle = min(names[v - 1].split(' '))
			handles.append(handle)
	print('YES')
	return 'YES'
sol()


n = int(input())
name = [''] * n
surname = [''] * n
for i in range(n):
	(name[i], surname[i]) = input().split()
a = list(map(int, input().split()))
new_name = []
new_surname = []
for i in a:
	new_name.append(name[i - 1])
	new_surname.append(surname[i - 1])
dp = [[False] * 2 for i in range(n)]
dp[0][0] = True
dp[0][1] = True
for i in range(n - 1):
	if dp[i][0]:
		if new_name[i] < new_name[i + 1]:
			dp[i + 1][0] = True
		if new_name[i] < new_surname[i + 1]:
			dp[i + 1][1] = True
	if dp[i][1]:
		if new_surname[i] < new_name[i + 1]:
			dp[i + 1][0] = True
		if new_surname[i] < new_surname[i + 1]:
			dp[i + 1][1] = True
if dp[n - 1][0] or dp[n - 1][1]:
	print('YES')
else:
	print('NO')


false = False
curr = []
names = int(input())
ppl = [input().split(' ') for i in range(names)]
order = list(map(str, input().split(' ')))
for i in range(names):
	if i == 0:
		curr.append(min(ppl[int(order[i]) - 1]))
	else:
		a = ppl[int(order[i]) - 1][0]
		b = ppl[int(order[i]) - 1][1]
		if curr[-1] <= min(a, b):
			curr.append(min(a, b))
		elif curr[-1] <= max(a, b):
			curr.append(max(a, b))
		else:
			false = True
if false:
	print('NO')
else:
	print('YES')


def main():
	n = int(input())
	names = tuple((input() for _ in range(n)))
	a = ''
	for i in map(int, input().split()):
		l = names[i - 1].split()
		l.append(a)
		l.sort()
		if l[2] == a:
			print('NO')
			return
		a = l[(l[1] == a) + 1]
	print('YES')
main()


l = []
for item in range(int(input())):
	(f, last) = input().split()
	l.append((f, last))
order = list(map(int, input().split()))

def solve(l, o):
	minName = min(l[o[0] - 1])
	for item in o[1:]:
		currentmin = min(l[item - 1])
		currentmax = max(l[item - 1])
		if currentmin > minName:
			minName = currentmin
		elif currentmax > minName:
			minName = currentmax
		else:
			return 'NO'
	return 'YES'
print(solve(l, order))


n = int(input())
persons = []
for i in range(n):
	(a, b) = input().split()
	persons.append([a, b])
p = list(map(int, input().split()))
if persons[p[0] - 1][0] > persons[p[0] - 1][1]:
	last = persons[p[0] - 1][1]
else:
	last = persons[p[0] - 1][0]
res = 'YES'
for i in range(1, n):
	persons[p[i] - 1].sort()
	if persons[p[i] - 1][0] > last:
		last = persons[p[i] - 1][0]
	elif persons[p[i] - 1][1] > last:
		last = persons[p[i] - 1][1]
	else:
		res = 'NO'
print(res)


def doJ(a, b):
	return [a, b]
n = int(input())
where = []
for i in range(n):
	where.append([])
a = []
for i in range(n):
	cur = list(map(str, input().split()))
	fi = doJ(cur[0], i)
	se = doJ(cur[1], i)
	a.append(fi)
	a.append(se)
a.sort(key=lambda x: x[0])
h = list(map(int, input().split()))
cur = 0
for i in a:
	where[i[1]].append(cur)
	cur += 1
pre = -1
fl = True
for i in range(n):
	h[i] -= 1
	if where[h[i]][0] > pre:
		pre = where[h[i]][0]
	elif where[h[i]][1] > pre:
		pre = where[h[i]][1]
	else:
		fl = False
if fl is False:
	print('NO')
else:
	print('YES')


def solve(arr, p):
	cur = 'A' * 51
	for rank in p:
		if min(arr[rank - 1]) > cur:
			cur = min(arr[rank - 1])
		elif max(arr[rank - 1]) > cur:
			cur = max(arr[rank - 1])
		else:
			return False
	return True
n = int(input())
arr = list()
for i in range(n):
	arr.append(input().split())
p = list(map(int, input().split()))
print('YES') if solve(arr, p) else print('NO')


b = int(input())
l = list()
for i in range(b):
	(n, s) = input().split()
	l.append([n, i + 1])
	l.append([s, i + 1])
p = list(map(int, input().split()))
l.sort()
ans = 'YES'
ind = 0
cur = 0
while ind < 2 * b and cur < b:
	while l[ind][1] != p[cur]:
		ind += 1
		if ind >= 2 * b:
			ans = 'NO'
			break
	cur += 1
print(ans)


n = int(input())
a = [''] * n
b = [''] * n
c = [''] * n
for i in range(n):
	s = input()
	k = s.find(' ')
	a[i] = s[:k]
	b[i] = s[k + 1:]
k = 0
p = list(map(int, input().split()))
for j in p:
	i = j - 1
	if k == 0:
		c[k] = min(a[i], b[i])
		k += 1
	else:
		if a[i] > c[k - 1] and (not (b[i] > c[k - 1] and b[i] < a[i])):
			c[k] = a[i]
		elif b[i] > c[k - 1]:
			c[k] = b[i]
		else:
			print('NO')
			break
		k += 1
else:
	print('YES')


n = int(input())
h = []
for i in range(n):
	h.append(sorted(input().split()))
prev = ''
for i in (int(x) - 1 for x in input().split()):
	cur = h[i][0] if h[i][0] > prev else h[i][1]
	if cur < prev:
		print('NO')
		exit()
	prev = cur
print('YES')


n = int(input())
f = [input().split() for i in range(n)]
m = [False, False]
sl = list(map(int, input().split()))
if n == 1:
	print('YES')
else:
	if f[sl[0] - 1][0] <= f[sl[1] - 1][0] or f[sl[0] - 1][1] <= f[sl[1] - 1][0]:
		m[0] = True
	if f[sl[0] - 1][0] <= f[sl[1] - 1][1] or f[sl[0] - 1][1] <= f[sl[1] - 1][1]:
		m[1] = True
	(a, b) = (False, False)
	for i in range(1, n - 1):
		if m[0] and f[sl[i] - 1][0] <= f[sl[i + 1] - 1][0] or (m[1] and f[sl[i] - 1][1] <= f[sl[i + 1] - 1][0]):
			a = True
		else:
			a = False
		if m[0] and f[sl[i] - 1][0] <= f[sl[i + 1] - 1][1] or (m[1] and f[sl[i] - 1][1] <= f[sl[i + 1] - 1][1]):
			b = True
		else:
			b = False
		(m[0], m[1]) = (a, b)
	if not m[0] and (not m[1]):
		print('NO')
	else:
		print('YES')


def main():
	n = int(input())
	names = tuple((input() for _ in range(n)))
	a = ''
	try:
		for i in map(int, input().split()):
			l = names[i - 1].split()
			l.append(a)
			l.sort()
			a = l[l.index(a) + 1]
	except IndexError:
		print('NO')
		return
	print('YES')
main()


n = int(input())
names = []
for _ in range(n):
	(a, b) = input().split()
	names.append((a, b))
order = list(map(lambda x: int(x) - 1, input().split()))
pre = None
for i in order:
	(a, b) = names[i]
	if pre is None:
		if a > b:
			pre = b
		else:
			pre = a
	elif a > pre and b > pre:
		if a > b:
			pre = b
		else:
			pre = a
	elif a > pre:
		if a > b > pre:
			pre = b
		else:
			pre = a
	elif b > pre:
		if b > a > pre:
			pre = a
		else:
			pre = b
	else:
		print('NO')
		break
else:
	print('YES')


n = int(input())
lis = []
for i in range(n):
	a = list(map(str, input().split()))
	lis.append(a)
aa = list(map(int, input().split()))
cur = min(lis[aa[0] - 1][0], lis[aa[0] - 1][1])
for i in range(1, n):
	(a, b) = sorted(lis[aa[i] - 1])
	if cur < a:
		cur = a
	elif cur < b:
		cur = b
	else:
		print('NO')
		exit()
print('YES')


n = int(input())
f = [''] * n
s = [''] * n
name = [''] * n
for i in range(n):
	name[i] = input().split()
	name[i].sort()
p = list(map(lambda x: x - 1, map(int, input().split())))
result = 'YES'
last = ''
for i in range(n):
	if name[p[i]][0] > last:
		last = name[p[i]][0]
	elif name[p[i]][1] > last:
		last = name[p[i]][1]
	else:
		result = 'NO'
		break
print(result)


from functools import reduce
from operator import *
from math import *
from sys import *
from string import *
setrecursionlimit(10 ** 7)
RI = lambda : list(map(int, input().split()))
RS = lambda : input().rstrip().split()
n = RI()[0]
inp = [0] * n
flag = 1
for i in range(n):
	inp[i] = RS()
p = RI()
minS = min(inp[p[0] - 1])
for i in range(1, n):
	if inp[p[i] - 1][0] > minS and inp[p[i] - 1][1] > minS:
		minS = min(inp[p[i] - 1])
	elif inp[p[i] - 1][0] > minS:
		minS = inp[p[i] - 1][0]
	elif inp[p[i] - 1][1] > minS:
		minS = inp[p[i] - 1][1]
	else:
		flag = 0
print(['NO', 'YES'][flag])


n = int(input())
t = [input() for i in range(n)]
s = [t[i - 1] for i in map(int, input().split())]
t[-1] = ''
for i in range(n):
	(a, b) = s[i].split()
	if a > b:
		(a, b) = (b, a)
	if a > t[i - 1]:
		t[i] = a
	elif b > t[i - 1]:
		t[i] = b
	else:
		print('NO')
		exit()
print('YES')


def solve(n, names, p):
	first_idx = p[0] - 1
	last_handle = min(names[first_idx][0], names[first_idx][1])
	for i in range(1, n):
		curr_idx = p[i] - 1
		curr_handle = min(names[curr_idx][0], names[curr_idx][1])
		if curr_handle < last_handle:
			curr_handle = max(names[curr_idx][0], names[curr_idx][1])
			if curr_handle < last_handle:
				return 'NO'
		last_handle = curr_handle
	return 'YES'
n = int(input())
names = list()
for _ in range(0, n):
	name = input().split(' ')
	names.append(name)
p = list(map(int, input().split(' ')))
print(solve(n, names, p))


n = int(input())
a = [0] * n
b = [0] * n
c = [0] * n
for i in range(n):
	(a[i], b[i]) = input().split()
d = list(map(int, input().split()))
answer = 'YES'
c[0] = min(a[d[0] - 1], b[d[0] - 1])
for i in range(1, n):
	if c[i - 1] < min(a[d[i] - 1], b[d[i] - 1]):
		c[i] = min(a[d[i] - 1], b[d[i] - 1])
	elif c[i - 1] < max(a[d[i] - 1], b[d[i] - 1]):
		c[i] = max(a[d[i] - 1], b[d[i] - 1])
	else:
		answer = 'NO'
		break
print(answer)


n = int(input())
names = []
sortnames = []
result = 'YES'
for i in range(n):
	names.append(input().split(' '))
	sortnames.append('')
k = input().split(' ')
for i in range(n):
	k[i] = int(k[i])
	sortnames[i] = names[k[i] - 1]
	sortnames[i].sort()
sortnames[0] = sortnames[0][0]
for i in range(n - 1):
	if sortnames[i] < sortnames[i + 1][0]:
		sortnames[i + 1] = sortnames[i + 1][0]
	elif sortnames[i] < sortnames[i + 1][1]:
		sortnames[i + 1] = sortnames[i + 1][1]
	else:
		result = 'NO'
		break
print(result)


n = int(input())
people = []
for i in range(n):
	(first, last) = input().split(' ')
	d = {first: last}
	people.append(d)
p = [int(i) - 1 for i in input().split(' ')]
[(first, last)] = people[p[0]].items()
previous = min(first, last)
output = 'YES'
for i in range(1, n):
	[(first, last)] = people[p[i]].items()
	handle = min(first, last)
	if handle < previous:
		handle = max(first, last)
	if handle < previous:
		output = 'NO'
		break
	else:
		previous = handle
print(output)


n = int(input())
names = []
for i in range(n):
	names.append(list(sorted(input().split())))
perm = list(map(int, input().split()))
prev_handle = ''
failed = 0
for i in perm:
	name_set = names[i - 1]
	if name_set[0] > prev_handle:
		prev_handle = name_set[0]
	elif name_set[1] > prev_handle:
		prev_handle = name_set[1]
	else:
		failed = 1
		print('NO')
		break
if failed == 0:
	print('YES')


(n, prev) = (int(input()), '')
a = [input().split() for _ in range(n)]
p = [int(i) - 1 for i in input().split()]
for (i, v) in enumerate(p):
	if i == 0:
		prev = min(a[v][0], a[v][1])
	elif min(a[v][0], a[v][1]) > prev:
		prev = min(a[v][0], a[v][1])
	elif max(a[v][0], a[v][1]) > prev:
		prev = max(a[v][0], a[v][1])
	else:
		print('NO')
		exit()
print('YES')


n = int(input())
l = []
for i in range(n):
	(k, h) = input().split()
	l.append([k, i + 1])
	l.append([h, i + 1])
a = list(map(int, input().split()))
l.sort()
cur = 0
ind = 0
ans = 'YES'
while ind < 2 * n and cur < n:
	while l[ind][1] != a[cur]:
		ind += 1
		if ind >= 2 * n:
			ans = 'NO'
			break
	cur += 1
print(ans)


n = int(input())
name = []
for i in range(n):
	tmp = input().split()
	tmp.sort()
	name.append(tmp)
p = [int(x) for x in input().split()]
last = ''
for e in p:
	if name[e - 1][0] > last:
		last = name[e - 1][0]
	elif name[e - 1][1] > last:
		last = name[e - 1][1]
	else:
		print('NO')
		break
else:
	print('YES')


import sys
input = lambda : sys.stdin.readline()
MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : list(map(int, input().split()))
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
lstr = lambda : list(input().split())
n = ii()
l = []
arr = []
for _ in range(n):
	x = lstr()
	l.append(x)
	arr.extend(x)
lind = il()
arr.sort()
ind = 0
for i in arr:
	if ind < n and i in l[lind[ind] - 1]:
		ind += 1
print('YNEOS'[ind != n::2])