As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.

Mu-mu's enemy Kashtanka wants to unlock Mu-mu's phone to steal some sensible information, but it can only bark n distinct words, each of which can be represented as a string of two lowercase English letters. Kashtanka wants to bark several words (not necessarily distinct) one after another to pronounce a string containing the password as a substring. Tell if it's possible to unlock the phone in this way, or not.


-----Input-----

The first line contains two lowercase English letters — the password on the phone.

The second line contains single integer n (1 ≤ n ≤ 100) — the number of words Kashtanka knows.

The next n lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct.


-----Output-----

Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise.

You can print each letter in arbitrary case (upper or lower).


-----Examples-----
Input
ya
4
ah
oy
to
ha

Output
YES

Input
hp
2
ht
tp

Output
NO

Input
ah
1
ha

Output
YES



-----Note-----

In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES".

In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" producing "http", but it doesn't contain the password "hp" as a substring.

In the third example the string "hahahaha" contains "ah" as a substring.

Difficulty: EASY
Tags: String algorithms

Implementation

Complete search
Skill types: Complete search

Starter code:

Solutions:
password = input()
N = int(input())
words = []
for i in range(N):
	words += [input()]
f = False
for i in range(N):
	for j in range(N):
		if password in words[i] + words[j]:
			f = True
			break
if f:
	print('YES')
else:
	print('NO')


password = input()
numWords = int(input())
front = False
back = False
for _ in range(numWords):
	word = input()
	if word == password:
		front = True
		back = True
	if word[0] == password[1]:
		front = True
	if word[1] == password[0]:
		back = True
if back == True and front == True:
	print('YES')
else:
	print('NO')


passwd = input()
words = []
for _ in range(int(input())):
	word = input()
	for i in passwd:
		if i in word:
			words.append(word)
			break
for i in words:
	for j in words:
		if passwd in i + j:
			print('YES')
			break
	else:
		continue
	break
else:
	print('NO')


s = input()
n = int(input())
l = []
for i in range(n):
	l.append(input())
for i in range(n):
	for j in range(n):
		if l[i][1] + l[j][0] == s or l[i] == s or l[j] == s:
			print('YES')
			exit()
print('NO')


pas = input()
n = int(input())
l_letters = set()
r_letters = set()
ans = False
for i in range(n):
	j = input()
	l_letters.add(j[0])
	r_letters.add(j[1])
	if j == pas:
		ans = True
	if j[0] == pas[1]:
		if pas[0] in r_letters:
			ans = True
	if j[1] == pas[0]:
		if pas[1] in l_letters:
			ans = True
if ans:
	print('YES')
else:
	print('NO')


def main():
	p = input()
	first = False
	second = False
	N = int(input())
	for n in range(N):
		r = input()
		if r == p:
			first = True
			second = True
		if r[0] == p[1]:
			second = True
		if r[1] == p[0]:
			first = True
	if first and second:
		print('YES')
	else:
		print('NO')
main()


right = input()
n = int(input())
p = []
s = ''
r = ''
flag = 0
for i in range(0, n):
	p.append(input())
for i in range(0, n):
	curr = p[i]
	for j in range(0, i):
		s = curr + p[j]
		if s.find(right) != -1:
			flag = 1
	for j in range(i, n):
		s = curr + p[j]
		if s.find(right) != -1:
			flag = 1
if flag == 1:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
dictnry = []
for i in range(n):
	dictnry.append(input())
first_letters = []
second_letters = []
for i in dictnry:
	first_letters.append(i[0])
	second_letters.append(i[1])
first_letters = str(first_letters)
second_letters = str(second_letters)
first_search = first_letters.find(password[1]) >= 0
second_search = second_letters.find(password[0]) >= 0
if first_search and second_search or dictnry.count(password) == 1:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
a = []
ans_1 = False
ans_2 = False
total_ans = False
for _ in range(n):
	st = input()
	if st[::-1] == s or s == st:
		total_ans = True
		break
	if st[0] == s[1]:
		ans_1 = True
	if st[1] == s[0]:
		ans_2 = True
if ans_1 * ans_2 or total_ans:
	print('YES')
else:
	print('NO')


n = input()
t = int(input())
beg = False
end = False
for __ in range(t):
	q = input()
	if q == n:
		beg = True
		end = True
		print('YES')
		break
	if q.endswith(n[0]):
		beg = True
	if q.startswith(n[1]):
		end = True
	if beg == end and beg:
		print('YES')
		break
if not beg or not end:
	print('NO')


s = input()
w = [input() for k in range(int(input()))]
poss = False
for a in w:
	if a == s:
		poss = True
		break
	for b in w:
		if a[1] + b[0] == s:
			poss = True
			break
if poss:
	print('YES')
else:
	print('NO')


def main():
	pwd = input()
	words_count = int(input())
	words = [input() for _ in range(words_count)]
	if is_password_hacked(pwd, words):
		print('YES')
	else:
		print('NO')

def is_password_hacked(pwd, words):
	if pwd in words:
		return True
	words_ends_with_first_letter_from_pwd = list(filter(lambda w: pwd[0] == w[1], words))
	words_starts_with_second_letter_from_pwd = list(filter(lambda w: pwd[1] == w[0], words))
	if words_ends_with_first_letter_from_pwd and words_starts_with_second_letter_from_pwd:
		return True
	return False
main()


password = input()
n = int(input())
k_list = [input() for i in range(n)]
if password in k_list:
	print('YES')
else:
	front = password[0]
	end = password[1]
	frontF = endF = False
	for letters in k_list:
		if letters[1] == front:
			frontF = True
		if letters[0] == end:
			endF = True
	if frontF and endF:
		print('YES')
	else:
		print('NO')


s = input()
n = int(input())
(flag1, flag2) = (False, False)
for i in range(n):
	a = input()
	if s == a:
		(flag1, flag2) = (True, True)
	if s[0] == a[1]:
		flag1 = True
	if s[1] == a[0]:
		flag2 = True
if flag1 and flag2:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
strings = []
for _ in range(n):
	strings.append(input())
ans = 'NO'
for i in range(n):
	for j in range(i, n):
		if (strings[i] + strings[j]).find(password) != -1:
			ans = 'YES'
		if (strings[j] + strings[i]).find(password) != -1:
			ans = 'YES'
print(ans)


import os
import math

def work():
	ans = input()
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	flag = False
	for i in range(n):
		for j in range(n):
			temp = a[i] + a[j]
			if temp.count(ans) > 0:
				flag = True
	if flag:
		print('YES')
	else:
		print('NO')
work()


s = input()
n = int(input())
S = []
for i in range(n):
	S.append(input())
flag = 0
for i in range(n):
	for j in range(n):
		if s in S[i] + S[j]:
			print('YES')
			flag = 1
			break
	if flag == 1:
		break
if flag == 0:
	print('NO')


s = input()
n = int(input())
x = 0
y = 0
k = ''
pd = 0
for i in range(0, n):
	k = input()
	if k == s:
		print('YES')
		pd = 1
		break
	else:
		if k[0] == s[1]:
			x = 1
		if k[1] == s[0]:
			y = 1
	if x == 1 and y == 1:
		print('YES')
		pd = 1
		break
if pd == 0:
	print('NO')


(a, b) = list(input())
a_set = set()
b_set = set()
match = False
for _ in range(int(input())):
	(x, y) = list(input())
	if x == a and y == b:
		match = True
	if a == x:
		a_set.add(0)
	if a == y:
		a_set.add(1)
	if b == x:
		b_set.add(0)
	if b == y:
		b_set.add(1)
if len(a_set) > 0 and len(b_set) > 0:
	if match or (1 in a_set and 0 in b_set):
		print('YES')
	else:
		print('NO')
else:
	print('NO')


(passwd, flag, L) = (input(), 0, [])
for _ in range(int(input())):
	s = input()
	if s == passwd:
		flag = 1
	L.append(s)
for x in L:
	for y in L:
		if x[1] + y[0] == passwd:
			flag = 1
print('YES' if flag else 'NO')


pas = input()
n = int(input())
ws = [input() for i in range(n)]

def f():
	for i in range(n):
		for j in range(n):
			if pas in ws[i] + ws[j]:
				print('YES')
				return
	print('NO')
	return
f()


s = input()
n = int(input())
l = []
(c, p) = (0, 0)
for i in range(n):
	l.append(input())
if s in l:
	print('Yes')
else:
	for i in l:
		if i[0] == s[1]:
			c += 1
			break
	for i in l:
		if i[1] == s[0]:
			p += 1
			break
	if c == 1 and p == 1:
		print('Yes')
	else:
		print('No')


password = input()
n = int(input())
first_letter_found = second_letter_found = 0
result = 'NO'
for i in range(n):
	word = input()
	if word == password:
		result = 'YES'
	elif result == 'NO':
		if word[1] == password[0]:
			first_letter_found = 1
		if word[0] == password[1]:
			second_letter_found = 1
if result == 'NO' and first_letter_found == 1 and (second_letter_found == 1):
	result = 'YES'
print(result)


def main():
	p = input()
	n = int(input())
	words = []
	for i in range(n):
		words.append(input())
	if any((w[0] == p[0] and w[1] == p[1] for w in words)):
		print('YES')
	elif any((w[1] == p[0] for w in words)) and any((w[0] == p[1] for w in words)):
		print('YES')
	else:
		print('NO')
main()


pas = input()
n = int(input())
x = []
for i in range(n):
	s = input()
	x.append(s)
cnt1 = 0
cnt2 = 0
a = pas[0]
b = pas[1]
for i in range(n):
	if x[i][1] == a:
		cnt1 += 1
	if x[i][0] == b:
		cnt2 += 1
if pas in x:
	print('YES')
elif cnt1 > 0 and cnt2 > 0:
	print('YES')
else:
	print('NO')


x = input()
n = int(input())
start = set()
end = set()
a = ''
hasFound = 0
for i in range(n):
	t = input()
	if x == t:
		hasFound = 1
		print('YES')
		break
	start.add(t[0])
	end.add(t[1])
start = list(start)
end = list(end)
if not hasFound:
	if x[0] in end and x[1] in start:
		print('YES')
	else:
		print('NO')


s = input()
n = int(input())
a = []
for i in range(n):
	a.append(input())
fc = 0
sc = 0
for i in range(n):
	fb = a[i][0]
	sb = a[i][1]
	if fb + sb == s or sb + fb == s:
		fc = 1
		sc = 1
		break
	if fb == s[1] and fc == 0:
		fc = 1
	if sb == s[0] and sc == 0:
		sc = 1
	if fc == 1 and sc == 1:
		break
if fc == 1 and sc == 1:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
a = []
for i in range(n):
	a.append(input())
b = []
for w in a:
	for w2 in a:
		b.append((w + w2)[:2])
		b.append((w + w2)[1:3])
		b.append((w + w2)[2:])
if password in b:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
f1 = f2 = False
l = []
for i in range(n):
	x = input()
	if x[0] == s[1]:
		f1 = True
	if x[1] == s[0]:
		f2 = True
	l.append(x)
if s in l:
	print('YES')
	exit()
elif f1 and f2:
	print('YES')
else:
	print('NO')


a = input()
y = 0
h = 0
g = 0
for i in range(int(input())):
	t = input()
	if t == a:
		print('YES')
		g += 1
		break
	else:
		if a[0] in t:
			if t[1] == a[0]:
				y += 1
		if a[1] in t:
			if t[0] == a[1]:
				h += 1
if g == 0:
	if min(y, h) >= 1:
		print('YES')
	else:
		print('NO')


i = list(input())
n = int(input())
a = 0
b = 0
s = 0
for k in range(n):
	li = list(input())
	if li[0] == i[0]:
		if li[1] == i[1]:
			s = 1
			break
	if li[0] == i[1]:
		if li[1] == i[0]:
			s = 1
			break
	if li[1] == i[0]:
		a = 1
	if li[0] == i[1]:
		b = 1
if b == 1 and a == 1 or s == 1:
	print('YES')
else:
	print('NO')


import sys
s = sys.stdin.readline().strip()
n = int(sys.stdin.readline())

def checkPass(s, n):
	words = []
	for t in range(n):
		words.append(str(sys.stdin.readline().strip()))
		if words[-1] == s:
			return 'YES'
	for word1 in range(n):
		for word2 in range(n):
			if words[word1][1] + words[word2][0] == s or words[word2][1] + words[word1][0] == s:
				return 'YES'
	return 'NO'
print(checkPass(s, n))


par = input()
n = int(input())
nach = []
kon = []
iskl = []
for i in range(n):
	bukva = input()
	buk = bukva[0]
	va = bukva[1]
	nach.append(buk)
	kon.append(va)
	iskl.append(bukva)
if par[1] in nach and par[0] in kon or par in iskl:
	print('YES')
else:
	print('NO')


Password = input()
Looper = int(input())
Bark = []
Flag = False
for i in range(Looper):
	Bark.append(input())
	if Bark[-1] == Password:
		Flag = True
		Looper = 0
		break
for i in range(Looper):
	for j in range(Looper):
		if Bark[i][1] + Bark[j][0] == Password:
			Flag = True
			break
print('YES' if Flag else 'NO')


(goal, n) = (input(), int(input()))
wans = [input() for _ in range(n)]
(first, second) = list(zip(*wans))
if goal[0] in second and goal[1] in first or goal in wans:
	print('YES')
else:
	print('NO')


inp = input()
n = int(input())
ans = 0
flag1 = 0
flag2 = 0
while n > 0:
	temp = input()
	if temp == inp:
		ans = 1
	if inp[1] == temp[0]:
		flag2 = 1
	if inp[0] == temp[1]:
		flag1 = 1
	n -= 1
if ans or (flag1 and flag2):
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
l = [input() for _ in range(n)]
res = any((s in l[i] + l[j] for i in range(n) for j in range(n)))
print('YES' if res else 'NO')


def pass1(p, n, kn, start):
	if p in kn:
		return 'YES'
	elif len(start) == 0:
		return 'NO'
	else:
		kill = 0
		for i in range(len(start)):
			for j in range(len(kn)):
				if p in start[i] + kn[j]:
					return 'YES'
		return 'NO'
p = input()
n = int(input())
kn = []
start = []
for x in range(n):
	s = input()
	if s[-1] == p[0]:
		start.append(s)
		kn.append(s)
	else:
		kn.append(s)
print(pass1(p, n, kn, start))


password = str(input())
n = int(input())
first = False
second = False
for i in range(n):
	a = str(input())
	if a == password:
		print('YES')
		first = True
		second = True
		break
	if a[1] == password[0]:
		first = True
	if a[0] == password[1]:
		second = True
	if first == True:
		if second == True:
			print('YES')
			break
if first == False:
	print('NO')
elif second == False:
	print('NO')


s = input()
n = int(input())
l = []
for u in range(n):
	l.append(input())
flag = 0
for i in range(n):
	for j in range(n):
		if (l[i] + l[j]).count(s) >= 1:
			flag = 1
			break
	if flag:
		break
if flag:
	print('YES')
else:
	print('NO')


from sys import exit
s = input()
n = int(input())
v = []
for i in range(n):
	v.append(input())
for i in range(n):
	for j in range(n):
		if (v[i] + v[j]).find(s) != -1:
			print('YES')
			exit(0)
print('NO')


password = input()
number = int(input())
S = []
k = 0

def check(x):
	if S[x] == password:
		return True
	tmp = S[x][0]
	for (j, s) in enumerate(S):
		if s[1] + tmp == password:
			return True
	tmp = S[x][1]
	for (j, s) in enumerate(S):
		if tmp + s[0] == password:
			return True
	return False
for i in range(number):
	tmp = input()
	S.append(tmp)
for i in range(number):
	if check(i) is True:
		k += 1
		print('YES')
		break
if k == 0:
	print('NO')


password = input()
n = int(input())
language = []
z = 0
for i in range(n):
	words = input()
	language.append(words)
for i in language:
	for k in language:
		a = i + k
		if password in a:
			z = 1
			break
	if z == 1:
		break
if z == 1:
	print('YES')
else:
	print('NO')


def main():
	pwrd = input()
	ww = {input() for _ in range(int(input()))}
	ww = ww | {a[1] + b[0] for a in ww for b in ww}
	print(('NO', 'YES')[pwrd in ww])
main()


ans = input()
a = []
b = []
n = int(input())
k = False
for i in range(n):
	temp = input()
	if temp == ans:
		k = True
	else:
		a.append(temp[0])
		b.append(temp[1])
if ans[0] in b:
	if ans[1] in a:
		k = True
if k:
	print('YES')
else:
	print('NO')


passw = input()
n = int(input())
l = []
for _ in range(n):
	l.append(input())
if passw in l:
	print('YES')
	exit()
for i in range(n):
	if l[i][1] == passw[0]:
		break
else:
	print('NO')
	exit()
for i in range(n):
	if l[i][0] == passw[1]:
		break
else:
	print('NO')
	exit()
print('YES')


p = input()
n = int(input())
s = []
for _ in range(n):
	s.append(input())
if p in s:
	print('YES')
	exit(0)
else:
	for i in range(n):
		if s[i][1] == p[0]:
			for j in range(n):
				if s[j][0] == p[1]:
					print('YES')
					exit(0)
print('NO')


s = input()
n = int(input())
(l1, l2, l) = ([], [], [])
for i in range(n):
	t = input()
	l.append(t)
	l1.append(t[0])
	l2.append(t[1])
if s in l or s[::-1] in l:
	print('YES')
elif s[0] in l2 and s[1] in l1:
	print('YES')
else:
	print('NO')


from sys import stdin
max_val = int(10000000000000.0)
min_val = int(-10000000000000.0)

def read_int():
	return int(stdin.readline())

def read_ints():
	return [int(x) for x in stdin.readline().split()]

def read_str():
	return input()

def read_strs():
	return [x for x in stdin.readline().split()]
need = input()
nb_barks = int(input())
maybe = []
ans = 'NO'
for _ in range(nb_barks):
	curr = input()
	if need == curr:
		ans = 'YES'
		break
	else:
		maybe.append(curr)
if ans != 'YES':
	for i in maybe:
		if i[1] == need[0]:
			for j in maybe:
				if j[0] == need[1]:
					ans = 'YES'
					break
print(ans)


slovo = input()
n = int(input())
bu1 = False
bu2 = False
k = 0
for i in range(n):
	sl = input()
	if sl == slovo:
		bu1 = True
		bu2 = True
	if sl[0] == slovo[1] and (not bu2):
		bu2 = True
	if sl[1] == slovo[0] and (not bu1):
		bu1 = True
if bu1 and bu2:
	print('YES')
else:
	print('NO')


(x, y) = list(input().strip())
flag = False
(f1, f2) = (False, False)
for _ in range(int(input().strip())):
	(i, j) = list(input().strip())
	if i == x and y == j:
		flag = True
	if i == y:
		f1 = True
	if j == x:
		f2 = True
if f1 == True and f2 == True:
	flag = True
if flag:
	print('YES')
else:
	print('NO')


def main():
	pwd = input()
	n = int(input())
	f_l = False
	s_l = False
	for _ in range(n):
		b = input()
		if b == pwd:
			print('YES')
			return
		if pwd[1] == b[0]:
			f_l = True
		if pwd[0] == b[1]:
			s_l = True
	if f_l and s_l:
		print('YES')
	else:
		print('NO')
main()


pas = str(input())
num = int(input())
list1 = []
count1 = 0
count2 = 0
if num == 1:
	s1 = str(input())
	if pas == s1:
		print('YES')
	else:
		s2 = s1 + s1
		if s2[1] == pas[0] and s2[2] == pas[1]:
			print('YES')
		else:
			print('NO')
else:
	for i in range(num):
		s = str(input())
		list1.append(s)
	for i in range(len(list1)):
		if list1[i][0] == pas[1]:
			count1 += 1
		if list1[i][1] == pas[0]:
			count2 += 1
	if count1 >= 1 and count2 >= 1:
		print('YES')
	elif pas in list1:
		print('YES')
	else:
		print('NO')


pw = input()
forw = [input() for i in range(int(input()))]
for forw1 in forw:
	for forw2 in forw:
		if pw in forw1 + forw2:
			print('YES')
			exit(0)
print('NO')


pas = input()
n = int(input())
flag1 = 0
flag2 = 0
for i in range(n):
	s = input()
	if s == pas:
		flag1 = 1
		flag2 = 1
		break
	if s[:1] == pas[1:]:
		flag1 = 1
	if s[1:] == pas[:1]:
		flag2 = 1
if flag1 == 1 and flag2 == 1:
	print('YES')
else:
	print('NO')


passw = list(input())
t = int(input())
barked = ''
done = 0
(first, second) = ([], [])
for x in range(t):
	l1 = list(input())
	if passw == l1:
		done = 1
		break
	first.append(l1[0])
	second.append(l1[1])
if done or (passw[0] in second and passw[1] in first):
	print('YES')
else:
	print('NO')


(first, last) = input()
num_words = int(input())
ending_first = False
starting_last = False
found = False
for _ in range(num_words):
	(wfirst, wlast) = input()
	if wfirst == first and wlast == last:
		print('YES')
		found = True
		break
	if wlast == first:
		ending_first = True
	if wfirst == last:
		starting_last = True
	if ending_first and starting_last:
		print('YES')
		found = True
		break
if not found:
	print('NO')


a = input()
b = int(input())
array = []
x = 0
y = 0
for i in range(b):
	array.append(input())
letter1 = a[0]
letter2 = a[1]
for i in array:
	b_letter1 = i[0]
	b_letter2 = i[1]
	if i == a:
		x = 1
		y = 1
	if letter1 == b_letter2:
		x = 1
	if letter2 == b_letter1:
		y = 1
if x == 1 and y == 1:
	print('YES')
else:
	print('NO')


(count1, count2) = (0, 0)
password = input()
n = int(input())
for i in range(n):
	podstr = input()
	if podstr == password or (podstr[0] == password[1] and podstr[1] == password[0]):
		print('YES')
		exit()
	elif podstr[1] == password[0]:
		count1 = 1
	elif podstr[0] == password[1]:
		count2 = 1
if count1 + count2 >= 2:
	print('YES')
else:
	print('NO')


a = input()
(b, c) = (0, 0)
for i in range(int(input())):
	d = input()
	if d == a:
		print('YES')
		exit()
	if d[1] == a[0]:
		b = 1
	if d[0] == a[1]:
		c = 1
if b and c:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
words = []
ans = False
for i in range(n):
	word = input()
	words.append(word)
	if word == password:
		ans = True
if ans:
	print('YES')
else:
	for a in words:
		first = a[1]
		for b in words:
			second = b[0]
			if first + second == password:
				ans = True
				break
		if ans:
			break
	if ans:
		print('YES')
	else:
		print('NO')


a = input()
n = int(input())
b = (*(input() for i in range(n)),)
c = (''.join(map(lambda x: b[i] + x, b)) for i in range(n))
print(('NO', 'YES')[any((a in i for i in c))])


password = input()
n = int(input())
words = [input() for i in range(n)]
first_letter = password[0]
second_letter = password[1]
if password in words:
	print('YES')
elif any([i[0] == second_letter for i in words]) and any([i[1] == first_letter for i in words]):
	print('YES')
else:
	print('NO')


pw = input()
ll = []
for i in range(int(input())):
	s = input()
	ll.append(s)
if pw in ll:
	print('YES')
else:
	status = False
	for i in ll:
		if pw[0] == i[-1]:
			for j in ll:
				if pw[-1] == j[0]:
					status = True
					break
		if status == True:
			print('YES')
			break
	if status == False:
		print('NO')


s = input()
sec = []
fir = []
n = int(input())
for i in range(n):
	s1 = input()
	sec.append(s1[1])
	fir.append(s1[0])
	if s1 == s:
		print('YES')
		break
else:
	if s[0] in sec and s[1] in fir:
		print('YES')
	else:
		print('NO')


X = input()
N = int(input())
(First, Second) = (False, False)
for i in range(N):
	Temp = input()
	if X == Temp:
		print('YES')
		exit()
	if X[0] == Temp[1]:
		First = True
	if X[1] == Temp[0]:
		Second = True
	if First and Second:
		print('YES')
		exit()
print('NO')


x = input()
n = int(input())
list = []
b = 0
for i in range(n):
	a = input()
	list.append(a)
for i in range(n):
	for j in range(n):
		if x in list[i] + list[j]:
			print('YES')
			b = 1
			break
	if b == 1:
		break
if b == 0:
	print('NO')


s = input()
n = int(input())
a = [input() for i in range(n)]
for i in range(n):
	for j in range(n):
		if a[i] == s or a[i][1] + a[j][0] == s:
			print('YES')
			return
print('NO')


password = input()
n = int(input())
first = 0
second = 0
for i in range(n):
	x = input()
	if x[0] == password[0] and x[1] == password[1]:
		first = 1
		second = 1
	elif x[0] == password[1]:
		second = 1
	if x[1] == password[0]:
		first = 1
	elif x[1] == password[1]:
		second = 1
if first == 1 and second == 1:
	print('YES')
else:
	print('NO')


def solve():
	password = input()
	n = int(input())
	words = [input() for i in range(n)]
	for i in range(n):
		for j in range(n):
			temp = words[i] + words[j]
			if password in temp:
				return 'YES'
	return 'NO'
print(solve())


s = input()
n = int(input())
(ll, lf, c, l, f) = ([], [], 0, 0, 0)
for i in range(n):
	j = input()
	if j == s or j == s[::-1]:
		c = 1
	elif f == 0 and j[0] == s[1]:
		lf.append(j)
		f = 1
	elif l == 0 and j[1] == s[0]:
		ll.append(j)
		l = 1
if c == 1:
	print('YES')
elif len(ll) >= 1 and len(lf) >= 1:
	print('YES')
else:
	print('NO')


(p, w) = (input(), [input() for i in range(int(input()))])
print('YES' if p in w or (p[0] in (wi[1] for wi in w) and p[1] in (wi[0] for wi in w)) else 'NO')


p = str(input())
n = int(input())
(f, b, c) = (False, False, False)
for _ in range(n):
	s = str(input())
	if s == p:
		c = True
		break
	if s[1] == p[0]:
		f = True
	if s[0] == p[1]:
		b = True
	if f and b:
		c = True
		break
if c == True:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
exec('b=[' + n * 'input(),' + ']')
c = [*zip(*b)]
print('NYOE S'[s in b or (s[0] in c[1] and s[1] in c[0])::2])


s = input()
n = int(input())
a = [input() for i in range(n)]
for x in a:
	for y in a:
		if s in x + y:
			print('YES')
			return
print('NO')


pas = input()
n = int(input())
x = 0
y = 0
flag = 0
for i in range(n):
	a = input()
	if a[1] == pas[0]:
		x = 1
	if a[0] == pas[1]:
		y = 1
	elif a[0] == pas[0] and a[1] == pas[1]:
		flag = 1
if x == 1 and y == 1 or flag == 1:
	print('YES')
else:
	print('NO')


p = input()
n = int(input())
a = []
for i in range(n):
	a.append(input())
(f1, f2) = (0, 0)
for i in a:
	if i == p or i[::-1] == p:
		print('YES')
		exit()
	elif i[0] == p[1]:
		f1 = 1
	elif i[1] == p[0]:
		f2 = 1
if f1 == f2 == 1:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
words = []
myset = set()
for _ in range(n):
	s = input()
	words.append(s)
	myset.update([s, s[::-1]])
for i in range(n):
	for j in range(i + 1, n):
		myset.update([words[i][1] + words[j][0], words[j][1] + words[i][0]])
if password in myset:
	print('YES')
else:
	print('NO')


pas = input()
n = int(input())
begin = False
end = False
same = False
for i in range(n):
	word = input()
	if word[0] == pas[1]:
		begin = True
	if word[1] == pas[0]:
		end = True
	if word == pas:
		same = True
if same:
	print('YES')
elif begin and end:
	print('YES')
else:
	print('NO')


password = input()
tries = int(input())
sec = 0
first = 0
whole = ''
for i in range(tries):
	one = input()
	if password == one:
		print('Yes')
		exit(0)
	if password[0] == one[1]:
		first = first + 1
	if password[1] == one[0]:
		sec = sec + 1
if min(first, sec) != 0:
	print('Yes')
else:
	print('No')


def unblock(lst, s):
	for i in range(len(lst)):
		for j in range(len(lst)):
			x = lst[i] + lst[j]
			if x.find(s) != -1:
				return 'YES'
	return 'NO'
t = input()
n = int(input())
a = list()
for z in range(n):
	y = input()
	a.append(y)
print(unblock(a, t))


s = input()
n = int(input())
start = s[0]
finish = s[1]
first = False
last = False
for i in range(n):
	t = input()
	if t[0] == finish:
		last = True
	if t[1] == start:
		first = True
	if t[0] == start and t[1] == finish or (first and last):
		print('YES')
		exit()
print('NO')


import sys
s = input()
n = int(input())
p = q = 0
for i in range(n):
	s2 = input()
	if n == 1:
		if s2 == s or s2 == s[::-1]:
			p = q = 1
	else:
		if s[0] == s2[1]:
			p = 1
		if s[1] == s2[0]:
			q = 1
		if s2 == s or s2 == s[::-1]:
			p = q = 1
if p + q == 2:
	print('YES')
else:
	print('NO')


pw = input()
n = int(input())
lista = []
aux = 0
for i in range(n):
	lista.append(input())
for i in lista:
	if pw[0] or pw[1] in i:
		for j in lista:
			if pw[0] or pw[1] in j:
				item = i + j
				if item.count(pw) > 0:
					aux = 1
					print('YES')
					break
	if aux == 1:
		break
if aux == 0:
	print('NO')


import itertools
s = input()
n = int(input())
a = []
for i in range(n):
	a.append(input())
for i in a:
	if i == s or s in i[::-1]:
		print('YES')
		exit()
for i in itertools.combinations(a, 2):
	if s in i[0] + i[1] or s in i[1] + i[0]:
		print('YES')
		exit()
print('NO')


s = input()
n = int(input())
a = []
b = []
for i in range(n):
	c = input()
	a.append(c[0])
	b.append(c[1])
	if c == s:
		print('YES')
		break
else:
	if s[1] in a and s[0] in b:
		print('YES')
	else:
		print('NO')


password = input()
n = int(input())
firstLetter = 0
secondLetter = 0
found = 0
for i in range(n):
	inputStr = input()
	if inputStr == password:
		found = 1
	if inputStr[1] == password[0]:
		firstLetter = 1
	if inputStr[0] == password[1]:
		secondLetter = 1
if found or (firstLetter and secondLetter):
	print('YES')
else:
	print('NO')


pas = input('')
n = int(input(''))
list = []
for i in range(n):
	list.append(input(''))
(bool1, bool2) = (False, False)
for word in list:
	if word[1].lower() == pas[0].lower():
		bool1 = True
	if word[0].lower() == pas[1].lower():
		bool2 = True
	if word.lower() == pas.lower():
		(bool1, bool2) = (True, True)
if bool1 and bool2:
	print('YES')
else:
	print('NO')


a = input()
(found, f1, f2) = (False, 0, 0)
for _ in range(int(input())):
	b = input()
	if a == b:
		found = True
	if a[0] == b[1]:
		f1 = 1
	if a[1] == b[0]:
		f2 = 1
if found or (f1 and f2):
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
data = []
for i in range(n):
	data.append(input())
mark = 0
for i in range(n):
	for j in range(n):
		if s in data[i] + data[j]:
			mark = 1
if mark:
	print('YES')
else:
	print('NO')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 15
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def main():
	s = S()
	n = I()
	a = [S() for _ in range(n)]
	if s in a or (s[0] in map(lambda x: x[1], a) and s[1] in map(lambda x: x[0], a)):
		return 'YES'
	return 'NO'
print(main())


(want, n) = (input(), int(input()))
words = []
for i in range(n):
	words.append(input())
can = False
if words.count(want) > 0:
	can = True
for i in range(n):
	if words[i][0] == want[1]:
		for j in range(n):
			if words[j][1] == want[0]:
				can = True
if can:
	print('YES')
else:
	print('NO')


ip = input()
n = int(input())
(flag1, flag2) = (False, False)
for _ in range(n):
	s = input()
	if s == ip:
		(flag1, flag2) = (True, True)
		break
	if ip[1] == s[0]:
		flag1 = True
	if ip[0] == s[1]:
		flag2 = True
if flag1 == True and flag2 == True:
	print('YES')
else:
	print('NO')


p = str(input())
s = []
e = []
ip = []
for i in range(int(input())):
	temp = str(input())
	ip.append(temp)
	s.append(temp[0])
	e.append(temp[1])
b = 0
if p in ip:
	b = 1
elif p[0] in e and p[1] in s:
	b = 1
if b == 1:
	print('YES')
else:
	print('NO')


key = input()
n = int(input())
keys = []
for i in range(0, n):
	keys.append(input())
ok = True
if keys[0] == key:
	ok = False
for i in keys:
	for j in keys:
		if i != j and i == key or i == key[::-1] or (i[1] == key[0] and j[0] == key[1]):
			ok = False
if ok == True:
	print('NO')
else:
	print('YES')


pw = input()
forw = [input() for i in range(int(input()))]
revs = [forw1[1] + forw2[0] for forw1 in forw for forw2 in forw]
print('YES') if pw in forw + revs else print('NO')


s = input()
f = b = False
for _ in range(int(input())):
	t = input()
	if t[0] == s[1]:
		f = True
	if t[1] == s[0]:
		b = True
	if s == t:
		f = b = True
		break
if f and b:
	print('Yes')
else:
	print('No')


password = input()
n = int(input())
k = []
for i in range(n):
	k.append(input())

def func(password, n, k):
	count1 = 0
	count2 = 0
	for i in range(n):
		if k[i] == password or k[i][::-1] == password:
			return 'YES'
		if password[1] == k[i][0]:
			count1 += 1
		elif password[0] == k[i][1]:
			count2 += 1
	if count1 >= 1 and count2 >= 1:
		return 'YES'
	return 'NO'
print(func(password, n, k))


from math import *

def chop():
	return (int(i) for i in input().split())
a = input()
b = int(input())
ab = []
for i in range(b):
	ab.append(input())
slog = []
for i in range(b):
	slog.append(ab[i])
for i in range(b):
	cur = ab[i][0]
	for j in range(b):
		if j != i:
			slog.append(ab[j][1] + cur)
for i in range(b):
	cur = ab[i][1]
	for j in range(b):
		slog.append(cur + ab[j][0])
print('YES' if a in slog else 'NO')


s = input()
s = list(s)
n = int(input())
count1 = 0
count2 = 0
c1 = 0
while n:
	flag = False
	s1 = input()
	s1 = list(s1)
	if s[0] == s1[0] and s[1] == s1[1]:
		c1 = 1
	if s[0] == s1[1]:
		count1 = 1
	if s[1] == s1[0]:
		count2 = 1
	n -= 1
if count1 + count2 == 2 or c1 == 1:
	print('YES')
else:
	print('NO')


a = input()
n = int(input())
pra = 0
pob = 0
if n == 1:
	c = input()
	if c == a or c == a[::-1]:
		print('YES')
	else:
		print('NO')
else:
	for i in range(n):
		c = input()
		if a[0] == c[1]:
			pra += 1
		if a[1] == c[0]:
			pob += 1
		if a == c:
			pra += 1
			pob += 1
	if pra >= 1 and pob >= 1:
		print('YES')
	else:
		print('NO')


p = str(input())
n = int(input())
l = []
result = 0
for i in range(n):
	l.append(str(input()))
if p in l:
	result = 1
else:
	for i in range(n):
		for j in range(i, n):
			x = l[i]
			y = l[j]
			w = x + y
			if w[1:3] == p:
				result = 1
				break
			w = y + x
			if w[1:3] == p:
				result = 1
				break
		if result == 1:
			break
if result == 1:
	print('YES')
else:
	print('NO')


n = input()
n1 = int(input())
li = []
for i in range(n1):
	li.append(input())
flag1 = 0
flag = 0
if n in li:
	print('YES')
	exit(0)
for i in range(n1):
	if n[0] == li[i][1]:
		flag = 1
		break
for j in range(n1):
	if n[1] == li[j][0]:
		flag1 = 1
		break
if flag1 == 1 and flag == 1:
	print('YES')
else:
	print('NO')


pas = input()
n = int(input())
one = False
two = False
three = False
for i in range(n):
	s = input()
	if s[0] == pas[1]:
		one = True
	if s[1] == pas[0]:
		two = True
	if s[0] == pas[0] and s[1] == pas[1]:
		three = True
if one and two:
	print('YES')
	exit(0)
if three:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
words = [input() for i in range(n)]
k = 0
for i in words:
	for j in words:
		if s in i + j:
			k += 1
			break
	if k >= 1:
		print('YES')
		break
if k == 0:
	print('NO')


s = input()
list = []
for i in range(int(input())):
	list.append(str(input()))
ans = 'NO'
for i in list:
	for j in list:
		if i[1] in s[0] and j[0] == s[1] or s in list:
			ans = 'YES'
print(ans)


a = input()
b = int(input())
c = []
for j in range(b):
	c.append(input())
p = 'NO'
for i in c:
	if i[::-1] == a or i == a:
		p = 'YES'
	if i[1] == a[0]:
		c.remove(i)
		for i in c:
			if i[0] == a[1]:
				p = 'YES'
print(p)


pas = input()
n = int(input())
mas = [[], []]
gas = []
for i in range(n):
	s = input()
	if s[0] == pas[1] and len(mas[1]) == 0:
		mas[1].append(s[0])
	elif s[1] == pas[0] and len(mas[0]) == 0:
		mas[0].append(s[1])
	gas.append(s)
for s in gas:
	if s == pas:
		mas = [[], []]
		mas[0].append('Kaino')
		mas[1].append('NUA_KAWAI)')
		break
	if s[0] == pas[1] and len(mas[1]) == 0:
		mas[1].append(s[0])
	elif s[1] == pas[0] and len(mas[0]) == 0:
		mas[0].append(s[1])
if len(mas[1]) + len(mas[0]) == 2:
	print('YES')
else:
	print('NO')


a = input()
n = int(input())
s = [input() for i in range(n)]

def go():
	print('YES')
	exit()
for x in s:
	if x == a:
		go()
	for y in s:
		if a in x + y:
			go()
print('NO')


import sys
s = input()
n = int(input())
l = []
for i in range(n):
	p = input()
	l.append(p)
for i in l:
	if s in i:
		print('YES')
		sys.exit()
	for j in l:
		if i[1] == s[0] and j[0] == s[1]:
			print('YES')
			sys.exit()
print('NO')


s = input()
n = int(input())
f = e = ''
for i in range(n):
	t = input()
	if t == s:
		exit(print('YES'))
	f += t[0]
	e += t[1]
	if s[0] in e and s[1] in f:
		exit(print('YES'))
print('NO')


n = input()
c = 0
v = 0
for i in range(int(input())):
	s = input()
	if n[0] == s[1]:
		c += 1
	if n[1] == s[0]:
		v += 1
	if n == s:
		c += 1
		v += 1
print('YES' if c and v else 'NO')


3
s = input()
n = int(input())
l = [input() for i in range(n)]
ans = False
if s in l:
	ans = True
else:
	for i in range(n):
		for j in range(n):
			if (l[i] + l[j])[1:3] == s:
				ans = True
if ans:
	print('YES')
else:
	print('NO')


(a, b) = list(input())
ca = cb = c = 0
for i in range(int(input())):
	(x, y) = list(input())
	if y == a:
		ca += 1
	if x == b:
		cb += 1
	if x == a and y == b:
		c += 1
if c > 0 or (ca > 0 and cb > 0):
	print('YES')
else:
	print('NO')


pw = input()
n = int(input())
(match1, match2) = (False, False)
for i in range(n):
	word = input()
	if pw == word:
		print('YES')
		exit()
	else:
		if pw[0] == word[1]:
			match1 = True
		if pw[1] == word[0]:
			match2 = True
if match1 and match2:
	print('YES')
else:
	print('NO')


def check(password, n, a):
	for i in range(n):
		if password[0] == a[i][1]:
			for j in range(n):
				if password[1] == a[j][0]:
					return True
	if password in a:
		return True
	return False
password = input()
n = int(input())
a = []
for i in range(n):
	a.append(input())
if check(password, n, a):
	print('YES')
else:
	print('NO')


def main():
	pw = input()
	n = int(input())
	match = False
	words = []
	for i in range(n):
		w = input()
		words.append(w)
	ca = 0
	cb = 0
	for w in words:
		if w == pw:
			match = True
		else:
			if w[0] == pw[1]:
				ca += 1
			if w[1] == pw[0]:
				cb += 1
	if match or (ca > 0 and cb > 0):
		print('YES')
	else:
		print('NO')
main()


s = input()
n = int(input())
ans = 0
flag1 = 0
flag2 = 0
for i in range(0, n):
	b = input()
	if b == s or b[::-1] == s:
		ans += 2
	elif flag1 == 0 and b[0] == s[1]:
		ans += 1
		flag1 = 1
	elif flag2 == 0 and b[1] == s[0]:
		ans += 1
		flag2 = 1
if ans >= 2:
	print('YES')
else:
	print('NO')


s = str(input())
n = int(input())
t = 0
z = 0
for i in range(n):
	a = str(input())
	if a == s:
		print('YES')
		break
	if a[1] == s[0] and t == 0:
		t += 1
	if a[0] == s[1] and z == 0:
		z += 1
	if z == 1 and t == 1:
		print('YES')
		break
else:
	print('NO')


def solution():
	w = input().strip()
	n = int(input())
	words = []
	for i in range(n):
		words.append(input().strip())
	for i in range(n):
		if words[i] == w:
			return True
		for j in range(n):
			if words[i][1] + words[j][0] == w:
				return True
	return False
print('YES' if solution() else 'NO')


a = input()
b = int(input())
c = list()
r = 0
while r < b:
	c.append(input())
	r = r + 1
r = 0
d = 0
e = 0
f = 0
if a in c:
	d = 2
else:
	while r < b:
		if a[0] == c[r][1] and e == 0:
			d = d + 1
			e = 1
		if a[1] == c[r][0] and f == 0:
			d = d + 1
			f = 1
		if d == 2:
			break
		r = r + 1
if d == 2:
	print('YES')
else:
	print('NO')


from sys import stdin
password = stdin.readline()
n = int(stdin.readline())
words = []
for i in range(0, n):
	words.append(stdin.readline())
firstAvailable = False
secondAvailable = False
for i in range(0, n):
	if words[i] == password:
		firstAvailable = True
		secondAvailable = True
	if words[i][1] == password[0]:
		firstAvailable = True
	if words[i][0] == password[1]:
		secondAvailable = True
if firstAvailable and secondAvailable:
	print('YES')
else:
	print('NO')


pas = input()
n = int(input())
(pl, lp) = (False, False)
for i in range(n):
	a = input()
	if a == pas:
		(pl, lp) = (True, True)
		break
	if a[0] == pas[1]:
		lp = True
	if a[1] == pas[0]:
		pl = True
if pl == True and lp == True:
	print('YES')
else:
	print('NO')


import sys
import math
import bisect
import itertools
import random
import re

def solve(A, s):
	if s in A:
		return True
	n = len(A)
	for i in range(n):
		for j in range(n):
			if A[i][1] + A[j][0] == s:
				return True
	return False

def main():
	s = input()
	A = []
	for _ in range(int(input())):
		A.append(input())
	if solve(A, s):
		print('YES')
	else:
		print('NO')
main()


s = input()
n = int(input())
r = []
for i in range(n):
	a = input()
	if a == s or a[::-1] == s:
		print('YES')
		exit()
	r.append(a)
(c1, c2) = (False, False)
for i in r:
	if i[1] == s[0]:
		c1 = True
	if i[0] == s[1]:
		c2 = True
	if c1 & c2:
		print('YES')
		exit()
print('NO')


p = input()
n = int(input())
lf = 0
rf = 0
ans = 'NO'
for i in range(n):
	a = input()
	if a[0] == p[1]:
		lf = 1
	if a[1] == p[0]:
		rf = 1
	if p == a:
		ans = 'YES'
if lf == 1 and rf == 1:
	ans = 'YES'
print(ans)


p = input()
n = int(input())
w = []
c = 0
for i in range(n):
	w.append(input())
for i in w:
	for j in w:
		if p in i + j:
			print('YES')
			exit()
print('NO')


s = input()
n = int(input())
l = []
for i in range(n):
	x = input()
	l.append(x)
for i in l:
	for j in l:
		if s in i + j:
			print('YES')
			exit()
print('NO')


s = input()
n = int(input())
l = []
for i in range(n):
	l.append(input())
r = s[1] + s[0]
f = 0
if s in l or r in l:
	print('YES')
else:
	(x, y) = (0, 0)
	for i in range(n):
		if l[i][1] == s[0]:
			x = 1
		if l[i][0] == s[1]:
			y = 1
	if x == 1 and y == 1:
		print('YES')
	else:
		print('NO')


pas = input()
count = int(input())
words = [input() for i in range(count)]
if pas in words:
	print('YES')
else:
	f1 = False
	f2 = False
	for word in words:
		if pas[0] == word[1]:
			f1 = True
		if pas[1] == word[0]:
			f2 = True
	if f1 and f2:
		print('YES')
	else:
		print('NO')


t = input()
n = int(input())
l = []
flag = 0
for i in range(n):
	l.append(input())
for i in range(n):
	for j in range(i, n):
		if t in l[i] + l[j] or t in l[j] + l[i]:
			print('YES')
			exit()
print('NO')


def main():
	p = input()
	l = [input() for i in range(int(input()))]
	for i in range(len(l)):
		for j in range(len(l)):
			c = l[i] + l[j]
			if p in c:
				return print('YES')
	print('NO')
main()


password = input()
n = int(input())
p = []
c = 0
for i in range(n):
	p.append(input())
for i in range(n):
	s = p[i]
	if s[::-1] == password or s == password:
		c = 1
		break
	for j in range(1, n):
		t = p[j]
		if password in s + t or password in t + s:
			c = 1
		elif t[::-1] == password or t == password:
			c = 1
print('YES' if c == 1 else 'NO')


password = input()
count = int(input())
has_start = False
has_end = False
for i in range(count):
	word = input()
	if word == password:
		has_start = True
		has_end = True
	if word[1] == password[0]:
		has_start = True
	if word[0] == password[1]:
		has_end = True
print('YES' if has_start and has_end else 'NO')


def solve(arr, s, n):
	for i in arr:
		for j in arr:
			if s in i + j or s in j + i:
				return 'YES'
	return 'NO'

def main():
	s = input()
	n = int(input())
	arr = []
	for _ in range(n):
		i = input()
		arr.append(i)
	print(solve(arr, s, n))
main()


p = input()
q = [input() for i in range(int(input()))]
r = [q1[1] + q2[0] for q1 in q for q2 in q]
print('Yes' if p in q + r else 'No')


password = input()
n = int(input())
know = []
for _ in range(n):
	know.append(input())

def hi():
	for x in know:
		for y in know:
			if password in x + y:
				return 'YES'
	return 'NO'
print(hi())
exit(0)


pw = input()
n = int(input())
kash = []
for i in range(n):
	kash.append(input())
flag = False
for w in kash:
	if w == pw:
		flag = True
		break
	if w[1] == pw[0]:
		for z in kash:
			if pw in w + z or pw in z + w:
				flag = True
				break
if flag == True:
	print('YES')
else:
	print('NO')


string = input().strip()
(flag, f1, f2) = (False, False, False)
ins = []
for _ in range(int(input())):
	ins.append(input().strip())
for i in ins:
	if i == string or i == string[::-1]:
		print('YES')
		exit()
	if i[0] == string[1]:
		f1 = True
	if i[1] == string[0]:
		f2 = True
if f1 and f2:
	print('YES')
else:
	print('NO')


(pw, words, first, second) = (input(), set(), set(), set())
for _ in range(int(input())):
	word = input()
	words.add(word)
	first.add(word[0])
	second.add(word[1])
print('YES' if pw in words or (pw[0] in second and pw[1] in first) else 'NO')


import sys
lines = sys.stdin.readlines()
password = lines[0]
options = lines[2:]
if password in options:
	print('YES')
else:
	can_first = any(map(lambda s: s[1] is password[0], options))
	can_second = any(map(lambda s: s[0] is password[1], options))
	if can_first and can_second:
		print('YES')
	else:
		print('NO')


def f():
	(a, n, s) = (list(input()), int(input()), [])
	for i in range(n):
		s.append(list(input()))
	for i in s:
		if i == a:
			return 'YES'
		elif i[1] == a[0]:
			for j in s:
				if j[0] == a[1]:
					return 'YES'
	return 'NO'
print(f())


from sys import *
p = stdin.readline().strip()
n = int(stdin.readline())
f = False
f1 = False
for _ in range(n):
	x = stdin.readline().strip()
	if p == x or p == x[1] + x[0]:
		f = True
		f1 = True
	if p[0] == x[1]:
		f = True
	if p[1] == x[0]:
		f1 = True
	if f1 and f:
		print('YES')
		break
if not (f1 and f):
	print('NO')


key = input()
n = int(input())
lst = []
for i in range(n):
	lst.append(input())
firstChar = False
secondChar = False
for s in lst:
	if s == key:
		print('YES')
		exit()
	if s[1] == key[0]:
		firstChar = True
	if s[0] == key[1]:
		secondChar = True
if firstChar and secondChar:
	print('YES')
else:
	print('NO')


pswd = input()
w = [input() for _ in range(int(input()))]
if pswd[1] in [x[0] for x in w] and pswd[0] in [x[1] for x in w] or pswd in w:
	print('YES')
else:
	print('NO')


s2 = []
fl = 0
fl1 = 0
s = str(input())
a = int(input())
for i in range(1, a + 1):
	s1 = str(input())
	if s1 == s:
		fl = 1
		fl1 = 1
		break
	if s1[1] == s[0]:
		fl = 1
	if s1[0] == s[1]:
		fl1 = 1
if fl == 1 and fl1 == 1:
	print('YES')
else:
	print('NO')


def judge(n, words, password):
	anyEndWith0 = anyStartWith1 = False
	for i in range(n):
		if words[i] == password:
			return True
		if words[i][0] == password[1]:
			anyStartWith1 = True
		if words[i][1] == password[0]:
			anyEndWith0 = True
	if anyEndWith0 and anyStartWith1:
		return True
	return False
password = input()
n = int(input())
words = [input() for _ in range(n)]
print('YES' if judge(n, words, password) else 'NO')


p = input()
n = int(input())
s = [input() for i in range(n)]
for i in range(n):
	for j in range(n):
		if p in s[j] + s[i]:
			print('YES')
			exit()
print('NO')


from sys import stdin, stdout
pwd = input()
n = stdin.readline()
n = int(n)
(check1, check2) = (0, 0)
for _ in range(n):
	p = input()
	if p[::-1] == pwd or p == pwd:
		stdout.write('YES\n')
		(check1, check2) = (1, 1)
		break
	else:
		if pwd[0] in p and (p.index(pwd[0]) == 1 or set(p) == set(pwd[0])) and (check1 == 0):
			check1 += 1
		if pwd[1] in p and (p.index(pwd[1]) == 0 or set(p) == set(pwd[1])) and (check2 == 0):
			check2 += 1
		if check1 + check2 == 2:
			stdout.write('YES\n')
			break
if check1 + check2 < 2:
	stdout.write('NO\n')


password = input()
n = int(input())
s = []
for i in range(n):
	s.append(input())
if password in s:
	print('YES')
else:
	front = False
	back = False
	for i in s:
		if password[0] == i[1]:
			front = True
			break
	for i in s:
		if password[1] == i[0]:
			back = True
	if front and back:
		print('YES')
	else:
		print('NO')


pas = input()
l = []
for i in range(int(input())):
	l.append(input())
for i in l:
	if i == pas:
		print('YES')
		exit(0)
knt1 = 0
knt2 = 0
for i in l:
	if i[1] == pas[0]:
		knt1 = 1
	if i[0] == pas[1]:
		knt2 = 1
	if knt1 + knt2 == 2:
		print('YES')
		exit(0)
print('NO')


password = input()
n = int(input())
strings = [input() for i in range(n)]
if password in strings:
	print('YES')
else:
	letters = list(password)
	strings0 = [k for k in strings if letters[0] in list(k)[1]]
	strings1 = [k for k in strings if letters[1] in list(k)[0]]
	if len(strings0) > 0 and len(strings1) > 0:
		print('YES')
	else:
		print('NO')


s = input()
n = int(input())
slist = []
true = False
for i in range(n):
	sinp = input()
	slist.append(sinp)
for i in range(n):
	for l in range(n):
		string = ''
		string += slist[i]
		string += slist[l]
		if string.find(s) != -1:
			true = True
if true:
	print('YES')
else:
	print('NO')


from sys import stdin, stderr, maxsize
mod = int(1000000000.0) + 7

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
from collections import Counter, defaultdict
from itertools import permutations

def bark():
	s = S()
	x = [S() for i in range(I())]
	for i in x:
		for j in x:
			if s in i + j:
				return 'YES'
	return 'NO'
print(bark())


target = input()
n = int(input())
a = []
ok = 'NO'
for i in range(n):
	a.append(input())
	if target == a[i]:
		ok = 'YES'
		pass
	elif target == a[i][::-1]:
		ok = 'YES'
		pass
	continue
for i in a:
	for j in a:
		if i != j:
			if target in i + j:
				ok = 'YES'
				pass
			elif target in j + i:
				ok = 'YES'
				pass
print(ok)


pw = input()
n = int(input())
w = list()
for _ in range(n):
	w.append(input())
for _ in range(n):
	if pw in w:
		print('YES')
		exit(0)
for i in range(n):
	for j in range(n):
		if w[i][1] == pw[0] and w[j][0] == pw[1]:
			print('YES')
			exit(0)
print('NO')


pswd = input()
n = int(input())
l = []
res = 'NO'
for i in range(n):
	s = input()
	l.append(s)
	if s == pswd:
		res = 'YES'
		break
if res == 'NO':
	for i in range(n):
		if l[i][1] == pswd[0]:
			ind = i
			for k in range(n):
				if l[k][0] == pswd[1]:
					res = 'YES'
					break
print(res)


lock = input()
num = int(input())
lst = []
for i in range(num):
	lst.append(input())
a = False
b = False
for word in lst:
	if word == lock:
		a = True
		b = True
for word in lst:
	if word == lock:
		a = True
		b = True
	if word.endswith(lock[0]):
		a = True
		break
for word in lst:
	if word.startswith(lock[1]):
		b = True
		break
if a and b:
	print('YES')
else:
	print('NO')


p = input()
n = int(input())
l = list()
for i in range(n):
	l.append(input())
yes = False
for i in range(n):
	for j in range(n):
		if l[i] == p or l[j] == p or (l[i][1] == p[0] and l[j][0] == p[1]) or (l[j][1] == p[0] and l[i][0] == p[1]):
			yes = True
			break
print('YES' if yes else 'NO')


s = input()
n = int(input())
x = ''
for i in range(n):
	x += input()
(a, b) = (x[::2], x[1::2])
if s in x:
	print('YES')
elif s[0] in b and s[1] in a:
	print('YES')
else:
	print('NO')


s = input()
t = int(input())
count = 0
count1 = 0
for i in range(t):
	a = input()
	if a[0] == s[1]:
		count1 = count1 + 1
	if a[1] == s[0]:
		count = count + 1
	if a == s:
		count = count + 1
		count1 = count1 + 1
if count1 > 0 and count > 0:
	print('YES')
else:
	print('NO')


n = input()
t = int(input())
b = []
s = 0
d = ''
c = 0
for i in range(t):
	x = input()
	b.append(x)
	d = d + x
if len(b) > 1:
	if n in d:
		print('YES')
	elif d.count(n[0]) >= n.count(n[0]) and d.count(n[1]) >= n.count(n[1]):
		if n[0] == n[1]:
			if b[1][1] == b[0][0]:
				print('YES')
			else:
				print('NO')
		else:
			for i in b:
				if i[::-1] == n:
					c = c + 1
				elif i[1] == n[0] or i[0] == n[1]:
					s = s + 1
			if s >= 2 or c >= 1:
				print('YES')
			else:
				print('NO')
	else:
		print('NO')
elif b[0] == n or b[0][::-1] == n:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
lasts = set()
firsts = set()
attempts = set()
for _ in range(n):
	attempt = input()
	attempts.add(attempt)
	firsts.add(attempt[0])
	lasts.add(attempt[1])
if password in attempts or (password[0] in lasts and password[1] in firsts):
	print('YES')
else:
	print('NO')


paswd = input()
start = paswd[0]
stop = paswd[1]
n = int(input())
(c, d) = (0, 0)
for i in range(n):
	a = input()
	if a[0] == stop:
		c = 1
	if a[1] == start:
		d = 1
	if a[0] == start and a[1] == stop:
		(c, d) = (1, 1)
if c == d == 1:
	print('YES')
if c == 0 or d == 0:
	print('NO')


si = input()
n = int(input())
s = set()
ans = 'NO'
while n:
	w = input()
	if w == si:
		ans = 'YES'
	s.add((w[0], 0))
	s.add((w[1], 1))
	n -= 1
if (si[0], 1) in s and (si[1], 0) in s:
	ans = 'YES'
print(ans)


password = input()
n = int(input())
words = [input() for i in range(n)]
for i in words:
	for j in words:
		if password in i + j:
			print('YES')
			raise SystemExit
print('NO')


password = str(input())
num = int(input())
lista = []
for i in range(num):
	lista.append(list(input().split()))
for j in lista:
	if j[0][1] == password[0]:
		for k in lista:
			if k[0][0] == password[1]:
				print('YES')
				quit()
	if j[0] == password:
		print('YES')
		quit()
else:
	print('NO')


Pass = input()
n = int(input())
words = []
x = []
for i in range(n):
	words.append(input())
for i in range(n):
	for j in range(n):
		if words[i][1] + words[j][0] == Pass:
			x.append('YES')
if Pass in words:
	x.append('YES')
if x.count('YES') == 0:
	print('NO')
else:
	print('YES')


s = str(input())
n = int(input())
e = 0
f = 0
flag = 0
for i in range(n):
	s1 = str(input())
	if n == 1 and s1[::-1] == s:
		print('YES')
		flag = 1
		break
	elif s1[::-1] == s:
		print('YES')
		flag = 1
		break
	elif s1[-1] == s[0]:
		e = e + 1
	elif s1[0] == s[1]:
		f = f + 1
	elif s1 == s:
		print('YES')
		flag = 1
		break
if flag == 0:
	if e > 0 and f > 0:
		print('YES')
	else:
		print('NO')


def canUnlock(pwds, pwd):
	if pwd in pwds:
		return 'YES'
	firstLetter = False
	secondLetter = False
	for p in pwds:
		if p[0] == pwd[1]:
			firstLetter = True
		if p[1] == pwd[0]:
			secondLetter = True
	if firstLetter and secondLetter:
		return 'YES'
	return 'NO'
pwd = input()
N = int(input())
pwds = []
for i in range(N):
	pwds.append(input())
print(canUnlock(pwds, pwd))


pwd = str(input())
num = int(input())
f2 = b1 = False
done = False
for i in range(num):
	temp = str(input())
	if temp == pwd:
		done = True
	if temp[0] == pwd[1]:
		f2 = True
	if temp[1] == pwd[0]:
		b1 = True
if done == True:
	print('YES')
elif f2 == True and b1 == True:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
ans = False
first = ''
second = ''
for _ in range(n):
	s1 = input()
	first += s1[0]
	second += s1[1]
	if s1 == s:
		ans = True
if ans == True:
	print('Yes')
elif first.count(s[1]) and second.count(s[0]):
	print('Yes')
else:
	print('No')


pw = input()
n = int(input())
L = []
for i in range(n):
	L.append(input())
if pw in L:
	print('yes')
else:
	(d, f) = (0, 0)
	for i in range(n):
		if L[i][0] == pw[1]:
			d = 1
		if L[i][1] == pw[0]:
			f = 1
	if d == 1 and f == 1:
		print('yes')
	else:
		print('no')


a = input()
n = int(input())
words = []
i = 0
while i < n:
	b = input()
	words.append(b)
	i += 1
match = False
for word1 in words:
	for word2 in words:
		if a in word1 + word2:
			match = True
			break
if match:
	print('YES')
else:
	print('NO')


p = input()
n = int(input())
c1 = 0
c2 = 0
for i in range(n):
	w = input()
	c1 += 1 if w[0] == p[1] or w == p else 0
	c2 += 1 if w[1] == p[0] or w == p else 0
print('NO' if c1 == 0 or c2 == 0 else 'YES')


def solution():
	password = input()
	num_words = int(input())
	words = []
	for _ in range(num_words):
		words.append(input())
	for x in range(num_words):
		for y in range(num_words):
			possible = words[x] + words[y]
			if password in possible:
				print('YES')
				return
	print('NO')
solution()


p = input()
n = int(input())
arr = []
for i in range(n):
	arr.append(input())
for i in range(n):
	for j in range(n):
		if p in str(arr[i] + arr[j]):
			print('YES')
			exit()
print('NO')


s = input()
n = int(input())
ok = [False, False]
ok1 = False
for _ in range(n):
	t = input()
	if s == t:
		ok1 = True
	if s[0] == t[1]:
		ok[0] = True
	if s[1] == t[0]:
		ok[1] = True
if ok1 or (ok[0] and ok[1]):
	print('YES')
else:
	print('NO')


want = input()
n = int(input())
l = [input() for _ in range(n)]
works = False
if want in l:
	works = True
for i in range(n):
	for j in range(n):
		if want in l[i] + l[j]:
			works = True
print('YES' if works else 'NO')


key = input()
sl = int(input())
word = list()
for i in range(sl):
	nhap = input()
	word.append(nhap)
dau = False
cuoi = False
for (j, i) in enumerate(word):
	if key == i:
		print('YES')
		exit()
	if i[0] == key[1]:
		cuoi = True
	if i[1] == key[0]:
		dau = True
if dau and cuoi:
	print('YES')
else:
	print('NO')


p = input()
n = int(input())
l = []
l1 = []
flag1 = 0
flag2 = 0
for i in range(n):
	l.append(input())
if p in l or p[::-1] in l:
	print('YES')
else:
	for i in l:
		if i[0] == p[0] or i[0] == p[1] or i[1] == p[0] or (i[1] == p[1]):
			l1.append(i)
	for i in l1:
		if i[1] == p[0]:
			flag1 = 1
		if i[0] == p[1]:
			flag2 = 1
	if flag1 == 1 and flag2 == 1:
		print('YES')
	else:
		print('NO')


import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def sumc(t):
	s = 0
	while t > 0:
		s += t % 10
		t //= 10
	return s
p = input()
n = ii()
(p0, p1, w) = ([], [], [])
for i in range(n):
	s = input()
	p0.append(s[0])
	p1.append(s[1])
	w.append(s)
if p[1] in p0 and p[0] in p1 or p in w:
	print('YES')
else:
	print('NO')


def cal(s, a):
	if a in s:
		return 'YES'
	else:
		l = 0
		f = 0
		for p in s:
			if a[0] == p[1]:
				l = 1
			if a[1] == p[0]:
				f = 1
		if f == 1 and l == 1:
			return 'YES'
		else:
			return 'NO'
a = input()
n = int(input())
s = []
for i in range(0, n):
	s.append(input())
print(cal(s, a))


import math
import collections
import bisect
import heapq
import time
import random
password = input()
N = int(input())
words = []
for i in range(N):
	words.append(input())
p1 = False
p2 = False
for w in words:
	if w == password:
		p1 = True
		p2 = True
		break
	if w[1] == password[0]:
		p1 = True
	if w[0] == password[1]:
		p2 = True
if p1 and p2:
	print('YES')
else:
	print('NO')


pwd = input()
s = []
n = int(input())
flag = 0
for i in range(n):
	s.append(input())
for s1 in s:
	for s2 in s:
		if s1[-1] + s2[0] == pwd:
			flag = 1
			break
if flag or pwd in s:
	print('YES')
else:
	print('NO')


def Kashtanka(pw, n):
	lst = []
	for i in range(n):
		lst.append(input())
		if lst[i] == pw:
			return 'YES'
	temp = 1
	for j in range(n):
		if lst[j][1] == pw[0]:
			temp = 0
	if temp:
		return 'NO'
	for l in range(n):
		if pw[1] == lst[l][0]:
			return 'YES'
	return 'NO'
print(Kashtanka(input(), int(input())))


k = input()
sol = []
flag = False
for _ in range(int(input())):
	x = input()
	sol.append(x)
for i in range(len(sol)):
	for j in range(len(sol)):
		if k in (sol[i] + sol[j]) * 3 or k in (sol[j] + sol[i]) * 3:
			flag = True
			break
	if flag == True:
		break
if flag:
	print('YES')
else:
	print('NO')


pw = input()
n = int(input())
a = [input() for _ in range(n)]
if pw in a or (pw[0] in [s[1] for s in a] and pw[1] in [s[0] for s in a]):
	print('YES')
else:
	print('NO')


ch = input()
n = int(input())
ok = [0, 0]
for _ in range(n):
	s = input()
	if s[0] == ch[1]:
		ok[0] = True
	if s[1] == ch[0]:
		ok[1] = True
	if s == ch:
		ok[0] = True
		ok[1] = True
if ok[0] and ok[1]:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
a = [input() for i in range(n)]
for i in a:
	for j in a:
		if s in i + j:
			print('YES')
			return
print('NO')


password = input()
n = int(input())
n1 = n
li = []
i = 1
check = False
while n != 0:
	temp = input()
	li.append(temp)
	n -= 1
for i in range(0, n1):
	for j in range(0, n1):
		a = li[i] + li[j]
		b = li[j] + li[i]
		if password in a or password in b:
			check = True
if check:
	print('YES')
else:
	print('NO')


from sys import stdin, stdout
pwd = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())
words = []
for _ in range(n):
	words.append(stdin.readline().rstrip())
if pwd in words:
	print('YES')
else:
	possible1 = False
	possible2 = False
	for word in words:
		if word[0] == pwd[1]:
			possible1 = True
		if word[1] == pwd[0]:
			possible2 = True
	if possible1 and possible2:
		print('YES')
	else:
		print('NO')


def main():
	pwd = input()
	n = int(input())
	start = set()
	end = set()
	for i in range(n):
		word = input()
		if word == pwd:
			print('YES')
			exit(0)
		start.add(word[0])
		end.add(word[1])
	if pwd[0] in end and pwd[1] in start:
		print('YES')
	else:
		print('NO')
main()


n = input()
v = ''
d = 0
m = 0
n1 = int(input())
for i in range(n1):
	s = input()
	if n[0] == s[1]:
		if m == 0:
			v += s[1]
			m = m + 1
	elif n[1] == s[0]:
		if d == 0:
			v += s[0]
			d = d + 1
	if n == s[::-1] or n == s:
		v = ''
		v += s[::-1]
if v == n or v[::-1] == n or n in v:
	print('YES')
else:
	print('NO')


s = input().strip()
l = []
n = int(input())
for x in range(n):
	l.append(input().strip())
present = False
for i in range(n):
	for j in range(n):
		if s in l[i] + l[j] or s in l[j] + l[i]:
			present = True
if present:
	print('YES')
else:
	print('NO')


word = input()
one_pos = []
two_pos = []
printed = False
for _ in range(int(input())):
	x = input()
	if word == x:
		print('YES')
		printed = True
		break
	if word[0] in x:
		if x[0] == word[0]:
			one_pos.append(0)
		if x[1] == word[0]:
			one_pos.append(1)
	if word[1] in x:
		if x[0] == word[1]:
			two_pos.append(0)
		if x[1] == word[1]:
			two_pos.append(1)
if not printed:
	if 1 in one_pos and 0 in two_pos:
		print('YES')
	else:
		print('NO')


p = input().strip()
a = [input().strip() for i in range(int(input()))]
b = [ai[1] + aj[0] for ai in a for aj in a]
print('YES' if p in a + b else 'NO')


password = str(input())
start = []
end = []
ans = 0
for i in range(int(input())):
	s = str(input())
	if password in s + s:
		print('YES')
		ans = 1
		break
	if password[0] in s:
		start.append(s + s)
	if password[1] in s:
		end.append(s + s)
if ans == 0:
	for i in start:
		if i[-1] == password[0]:
			ans = 2
			break
	if ans != 2:
		print('NO')
	else:
		for i in end:
			if i[0] == password[1]:
				ans = 1
				break
		if ans == 1:
			print('YES')
		else:
			print('NO')


from itertools import permutations

def unlock1(word, substring):
	if len(word) == 1:
		word = word + word
	perms = [''.join(w) for w in permutations(word)]
	for perm in perms:
		end = len(substring) - 1
		start = 0
		check = perm[:end + 1]
		while end != len(perm):
			if check == substring:
				return True
			end += 1
			start += 1
			check = perm[start:end + 1]
	return False
	print(perms)

def unlock(word, substring):
	if False:
		return
	else:
		for w in word:
			if w == substring:
				return True
		for i in range(len(word)):
			for j in range(len(word)):
				if word[i][1] + word[(j + 1) % len(word)][0] == substring:
					return True
	return False
while True:
	try:
		pw = input()
		count = input()
		words = []
		for i in range(int(count)):
			words.append(input())
		if unlock(words, pw):
			print('YES')
		else:
			print('NO')
	except EOFError:
		break


(p, n, w) = (input(), int(input()), [])
w.extend((input() for _ in range(n)))
if any((s == p for s in w)) or (any((s.startswith(p[1]) for s in w)) and any((s.endswith(p[0]) for s in w))):
	print('YES')
else:
	print('NO')


from math import ceil
Input = lambda : map(int, input().split())
Ramz = input()
Flag1 = Flag2 = False
for i in range(int(input())):
	s = input()
	if Ramz == s:
		Flag1 = Flag2 = True
		break
	if Ramz[0] == s[1]:
		Flag1 = True
	if Ramz[1] == s[0]:
		Flag2 = True
print('YES' if Flag1 & Flag2 else 'NO')


a = str(input())
n = int(input())
f = 0
g = 0
for i in range(n):
	s = str(input())
	if s == a:
		print('YES')
		exit()
	elif s[0] == a[1] and s[1] == a[0]:
		print('YES')
		exit()
	elif s[1] == a[0]:
		if f == 1:
			print('YES')
			exit()
		else:
			g = 1
	elif s[0] == a[1]:
		if g == 1:
			print('YES')
			exit()
		else:
			f = 1
print('NO')


y = str(input())
n = int(input())
X = []
for i in range(n):
	x = str(input())
	X.append(x)
cur = -1
for i in range(n):
	if X[i][0] == y[1] and X[i][1] == y[0] or (X[i][0] == y[0] and X[i][1] == y[1]):
		print('YES')
		exit()
for i in range(n):
	if X[i][1] == y[0]:
		cur = i
		break
if cur == -1:
	print('NO')
	exit()
for i in range(n):
	if X[i][0] == y[1] and i != cur:
		print('YES')
		exit()
print('NO')


p = input().strip()
n = int(input())
r = 0
s = [0] * n
for i in range(n):
	s[i] = input().strip()
for i in range(n):
	for j in range(n):
		if p in s[i] + s[j]:
			r = 1
if r:
	print('YES')
else:
	print('NO')


st = input()
arr = []
for _ in range(int(input())):
	arr.append(input())
firf = False
secf = False
flag = True
for sbst in arr:
	if sbst == st:
		print('YES')
		flag = False
		break
	if sbst[1] == st[0]:
		firf = True
	if sbst[0] == st[1]:
		secf = True
	if firf and secf:
		print('YES')
		flag = False
		break
if flag:
	print('NO')


phonePass = input()
numOfWords = int(input())
knownWords = []
flag = False
m1 = False
m2 = False
for i in range(numOfWords):
	s = input()
	knownWords.append(s)
for i in range(numOfWords):
	if knownWords[i] == phonePass:
		flag = True
for i in range(numOfWords):
	if knownWords[i][0] == phonePass[1]:
		m1 = True
	if knownWords[i][1] == phonePass[0]:
		m2 = True
	if m1 and m2:
		flag = True
if not flag:
	print('NO')
else:
	print('YES')


mumu = input().strip()
n = int(input().strip())
inputStrs = []
found = False
for i in range(n):
	inputStr = input().strip()
	if inputStr == mumu or inputStr[::-1] == mumu:
		print('YES')
		found = True
		break
	inputStrs.append(inputStr)
for i in range(n):
	if found == True:
		break
	str1 = inputStrs[i]
	for j in range(n):
		str2 = inputStrs[j]
		if str1 == str2:
			continue
		str3 = str1[1] + str2[0]
		str4 = str2[1] + str1[0]
		if str3 == mumu or str4 == mumu:
			print('YES')
			found = True
			break
if found == False:
	print('NO')


password = input()
n = int(input())
kash_words_list = []
for i in range(n):
	kash_words = input()
	kash_words_list.append(kash_words)
for a in kash_words_list:
	for b in kash_words_list:
		if password in a + b:
			print('YES')
			exit(0)
print('NO')


pw = input()
n = int(input())
d = []
for i in range(n):
	sem = input()
	d.append(sem)
for i in d:
	for j in d:
		if pw in i + j:
			print('YES')
			exit(0)
print('NO')


import sys

def ReadInput():
	return sys.stdin.read().splitlines()

def main():
	input = ReadInput()
	pwd = input[0]
	words = input[2:]
	if pwd in words:
		print('YES')
		return
	if any((pwd[0] == x[1] for x in words)) and any((pwd[1] == x[0] for x in words)):
		print('YES')
		return
	print('NO')
main()


s = input()
n = int(input())
ans = ''
for i in range(n):
	x = input()
	if s == x or s[::-1] == x:
		ans += '0'
	elif s[0] == x[1]:
		ans += '1'
	elif s[1] == x[0]:
		ans += '2'
if ans.count('0') >= 1:
	print('YES')
elif ans.count('1') >= 1 and ans.count('2') >= 1:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
lex = []
for i in range(n):
	lex.append(input())
YES = False
YES1 = False
YES2 = False
for word in lex:
	if word == password:
		YES = True
	if password[0] == word[1]:
		YES1 = True
	if password[1] == word[0]:
		YES2 = True
if YES1 and YES2:
	YES = True
if YES:
	print('YES')
else:
	print('NO')


y = input()
x = int(input())
t = []
ansp = []
u1 = []
u2 = []
for i in range(x):
	s = input()
	t.append(s)
for num in t:
	if num[::-1] == y:
		u1.append(num)
		u2.append(num)
	elif num == y:
		u1.append(num)
		u2.append(num)
	elif num[1] == y[0]:
		u1.append(num)
	elif num[0] == y[1]:
		u2.append(num)
if len(u1) > 0 and len(u2) > 0:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
first = ''
second = ''
ans = 'NO'
for _ in range(n):
	word = input()
	first += word[0]
	second += word[1]
	if word == s:
		ans = 'YES'
if first.count(s[1]) * second.count(s[0]) != 0:
	ans = 'YES'
print(ans)


s = input()
n = int(input())
words = [input() for word in range(n)]
ans = 'NO'
if s in words:
	ans = 'YES'
(start, end) = (False, False)
for word in words:
	if word[0] == s[1]:
		start = True
	if word[1] == s[0]:
		end = True
	if start and end:
		break
if start and end:
	ans = 'YES'
print(ans)


password = str(input())
number_of_barks = int(input())
all_the_barks = []
for i in range(number_of_barks):
	all_the_barks.append(str(input()))
yes = False
for bark in all_the_barks:
	for next_bark in all_the_barks:
		if password in bark + next_bark:
			yes = True
if yes == True:
	print('YES')
else:
	print('NO')


p = input()
p1 = p[0]
p2 = p[-1]
n = int(input())
a = []
f1 = False
f2 = False
for i in range(n):
	a.append(input())
if p in a:
	print('YES')
else:
	for i in range(len(a)):
		if a[i][-1] == p1:
			f1 = True
		if a[i][0] == p2:
			f2 = True
	if f2 and f1:
		print('YES')
	else:
		print('NO')


def main():
	pwd = input()
	n = int(input())
	s = [input() for _ in range(n)]
	print('YES' if pwd in s or (len([i for i in range(n) if s[i][1] == pwd[0]]) > 0 and len([i for i in range(n) if s[i][0] == pwd[1]]) > 0) else 'NO')
main()


s = input()
n = int(input())
a = [input() for i in range(n)]
flag = int(s in a)
for i in range(n):
	for j in range(n):
		if a[i][-1] == s[0] and a[j][0] == s[1]:
			flag = 1
print('YES' if flag else 'NO')


pw = input()
fc = False
lc = False
for _ in range(int(input())):
	s = input()
	if s == pw:
		fc = True
		lc = True
		break
	if s[0] == pw[1]:
		lc = True
	if s[1] == pw[0]:
		fc = True
if fc and lc:
	print('YES')
else:
	print('NO')


pwd = input()
n = int(input())
w = []
for _ in range(n):
	w.append(input())
for i in range(n):
	for j in range(i, n):
		w1 = w[i] + w[j]
		w2 = w[j] + w[i]
		if w1.find(pwd) >= 0 or w2.find(pwd) >= 0:
			print('YES')
			exit()
print('NO')


n = input()
k = int(input())
flag1 = flag2 = flag3 = 0
for i in range(k):
	s = input()
	if s == n:
		flag1 = 1
		flag2 = 1
	elif s[0] == n[1] and s[1] == n[0]:
		flag3 = 1
	elif s[0] == n[1]:
		flag1 = 1
	elif s[1] == n[0]:
		flag2 = 1
if flag1 == 1 and flag2 == 1 or flag3 == 1:
	print('YES')
else:
	print('NO')


p = input()
t = 0
d = 0
e = 0
f = 0
for j in range(int(input())):
	t = input()
	if t[1] == p[0]:
		d = 1
	if t[0] == p[1]:
		e = 1
	elif t == p:
		f = 1
if d == 1 and e == 1 or f == 1:
	print('YES')
else:
	print('NO')


password = input('')
inputs_len = input('')
inputs = []
for i in range(int(inputs_len)):
	inputs.append(input(''))
for i in range(int(inputs_len)):
	if password[0] == inputs[i][0] and password[1] == inputs[i][1]:
		print('YES')
		exit(0)
	elif password[0] == inputs[i][1]:
		for j in range(int(inputs_len)):
			if password[1] == inputs[j][0]:
				print('YES')
				exit(0)
print('NO')


pw = input()
f = 1
n = int(input())
for a in range(0, n):
	bark = input()
	if bark[1] == pw[0]:
		f = f * 2
	if bark[0] == pw[1]:
		f = f * 3
	if bark == pw:
		f = 6
if f % 6 == 0:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
f = 0
arr = []
for i in range(n):
	k = input()
	arr.append(k)
	if k == s:
		f = 1
for i in range(n):
	for j in range(2):
		if arr[i][0] == s[1]:
			for k in range(n):
				if arr[k][1] == s[0]:
					f = 1
		elif arr[i][1] == s[0]:
			for k in range(n):
				if arr[k][0] == s[1]:
					f = 1
if f == 1:
	print('YES')
else:
	print('NO')


p = input()
n = int(input())
a = []
for _ in range(n):
	s = input()
	if s == p or (s[0] == p[1] and p[0] == s[1]):
		print('YES')
		exit()
	a.append(s)
for i in range(n):
	for j in range(i + 1, n):
		if a[i][1] == p[0] and a[j][0] == p[1]:
			print('YES')
			exit()
			break
		if a[j][1] == p[0] and a[i][0] == p[1]:
			print('YES')
			exit()
			break
print('NO')


def find_first(s, strs, n):
	for i in range(n):
		if strs[i][0] == s:
			return True
	return False

def find_last(s, strs, n):
	for i in range(n):
		if str[i][len(strs[i]) - 1] == s:
			return True
	return False

def check(password, strs, n):
	for i in range(n):
		if strs[i] == password:
			return True
	return False
password = input()
n = int(input())
str = [0] * 105
for i in range(n):
	str[i] = input()
if find_first(password[1], str, n) and find_last(password[0], str, n) or check(password, str, n):
	print('YES')
else:
	print('NO')


s1 = input()
n = int(input())
begs = set()
ends = set()
res = False
for i in range(0, n):
	s = input()
	if s == s1:
		res = True
	begs.add(s[0])
	ends.add(s[1])
res = res or (s1[0] in ends and s1[1] in begs)
print('YES' if res else 'NO')


password = input()
n = int(input())
(same, fl, lf) = [False] * 3
for i in range(n):
	k = input()
	if k[0] == password[0]:
		if k[1] == password[1]:
			same = True
	if k[0] == password[1]:
		fl = True
	if k[1] == password[0]:
		lf = True
if fl and lf or same:
	print('YES')
else:
	print('NO')


def bark_password():
	password = input()
	barks = int(input())
	posible = []
	for i in range(barks):
		posible.append(input())
		if posible[i] == password:
			return 'YES'
		elif posible[i][0] in password and posible[i][1] in password:
			if password[0] in posible[i] and password[1] in posible[i]:
				return 'YES'
	left = False
	right = False
	for i in range(len(posible)):
		if posible[i][0] == password[1]:
			left = True
		if posible[i][1] == password[0]:
			right = True
		if left and right:
			return 'YES'
	return 'NO'
A = bark_password()
print(A)


def BarkToUnlock(pswd, barkList):
	flag1 = 0
	flag2 = 0
	if pswd in barkList:
		return 'YES'
	else:
		for b in barkList:
			if b[0] == pswd[1] and flag1 == 0:
				flag1 = 1
			if b[1] == pswd[0] and flag2 == 0:
				flag2 = 1
			if flag1 and flag2:
				return 'YES'
	return 'NO'
pswd = input()
n = int(input())
barkList = []
while n > 0:
	barkList.append(input())
	n -= 1
print(BarkToUnlock(pswd, barkList))


from sys import stdin, stdout
f = stdin.readline().strip()
n = int(stdin.readline())
challengers = []
for i in range(n):
	challengers.append(stdin.readline().strip())
label = 0
for i in range(n):
	for j in range(n):
		s = challengers[i] + challengers[j]
		if f in s:
			label = 1
if label:
	stdout.write('YES')
else:
	stdout.write('NO')


p = input()
n = int(input())
c = False
w = [input() for i in range(n)]
for i in range(n):
	for j in range(n):
		if w[i][0] == p[1] and w[j][1] == p[0]:
			c = True
			break
print({True: 'YES', False: 'NO'}[c or p in w])


def main():
	password = input()
	n = int(input())
	words = [None] * n
	for i in range(n):
		words[i] = input()
	print(solver(password, words))

def solver(password, words):
	if password in words:
		return 'YES'
	firstletters = [word[0] for word in words]
	lastletters = [word[1] for word in words]
	if password[0] in lastletters and password[1] in firstletters:
		return 'YES'
	else:
		return 'NO'
main()


def check(str1, num, k):
	fl = False
	for j in range(len(num)):
		m = str1 + num[j]
		if m[1:3] == password:
			fl = True
	return fl
password = input()
n = int(input())
num = []
flag = False
for i in range(n):
	a = input()
	if a == password:
		print('YES')
		flag = True
	else:
		num.append(a)
if len(num) == 1:
	q = num[0] + num[0]
	if q[1:3] == password:
		print('YES')
		flag = True
if not flag:
	for i in range(len(num)):
		if check(num[i], num, i):
			print('YES')
			flag = True
			break
if not flag:
	print('NO')


key = input()
k = int(input())
words = []
for _ in range(k):
	words.append(input())
for i in range(k):
	for j in range(k):
		if key in words[i] + words[j]:
			print('YES')
			exit(0)
print('NO')


k = input()
n = int(input())
list = []
while n > 0:
	list.insert(n - 1, input())
	n = n - 1
(c, d) = k
r = False
for s in list:
	(a, b) = s
	if a == c:
		if b == d:
			r = True
			break
	if b == c:
		for t in list:
			(e, f) = t
			if e == d:
				r = True
				break
if r:
	print('YES')
else:
	print('NO')


passw = input()
n = int(input())
l = []
for _ in range(n):
	l.append(input())
for i in range(n):
	for j in range(i, n):
		ansGuess = (l[i] + l[j]) * 2
		if passw in ansGuess:
			print('YES')
			exit()
print('NO')


pas = input()
n = int(input())
l = [input() for _ in range(n)]
s = set()
for i in l:
	for j in l:
		s.add(i + j)
		s.add(j + i)
found = False
for a in s:
	if pas in a:
		found = True
print('Yes' if found else 'No')


s = input()
n = int(input())
ss = []
for _ in range(n):
	ss.append(input())
found = False
for i in range(n):
	if ss[i] == s:
		found = True
	if found:
		break
	for j in range(n):
		if s == ss[i][1] + ss[j][0]:
			found = True
			break
if found:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
l = list()
y = 0
for _ in range(n):
	l.append(input())
for i in l:
	if i[::-1] == s or i == s:
		print('YES')
		exit()
for i in l:
	if i[0] == s[1]:
		y += 1
		break
for i in l:
	if i[1] == s[0]:
		y += 1
		break
print('YES' if y == 2 else 'NO')


t = input()
n = int(input())
ok = [0, 0]
for i in range(n):
	w = input()
	if w == t:
		ok = [1, 1]
	if w[1] == t[0]:
		ok[0] = 1
	if w[0] == t[1]:
		ok[1] = 1
if ok == [1, 1]:
	print('YES')
else:
	print('NO')


import sys
read = sys.stdin.readline

def solve(string, n, lst):
	if string in lst:
		return 'YES'
	find_first = False
	find_second = False
	for each in lst:
		if string[0] == each[1]:
			find_first = True
		if string[1] == each[0]:
			find_second = True
	if find_first and find_second:
		return 'YES'
	else:
		return 'NO'

def run():
	string = read().replace('\n', '')
	n = int(read().replace('\n', ''))
	lst = []
	for _ in range(n):
		lst.append(read().replace('\n', ''))
	print(solve(string, n, lst))
run()


w = input()
n = int(input())
b = 0
d = 0
f = 0
for i in range(n):
	s = input()
	if s == w:
		b = 1
	if s[0] == w[1]:
		d = 1
	if s[1] == w[0]:
		f = 1
if b == 1 or (d == 1 and f == 1):
	print('YES')
else:
	print('NO')


a = input()
n = int(input())
(l, r) = (0, 0)
c = 0
for i in range(n):
	s = input()
	if s == a:
		c += 1
	if s[1] == a[0]:
		l += 1
	if s[0] == a[1]:
		r += 1
if r and l or c:
	print('YES')
else:
	print('NO')


pssd = input()
s = []
for i in range(int(input())):
	s.append(input().strip())
flg = flg1 = flg2 = 'NO'
for data in s:
	if data == pssd:
		flg = 'YES'
	if pssd[0] == data[1]:
		flg1 = 'YES'
	if pssd[1] == data[0]:
		flg2 = 'YES'
if flg == 'YES':
	print('YES')
elif flg1 == 'YES' and flg2 == 'YES':
	print('YES')
else:
	print('NO')


s = input()
temp = []
for _ in range(int(input())):
	temp.append(input())
one = False
two = False
for x in temp:
	if x[0] == s[1]:
		one = True
	if x[1] == s[0]:
		two = True
print('YES' if one and two or s in temp else 'NO')


word = input()
char1 = word[0]
char2 = word[1]
n = int(input())
list1 = []
flag1 = False
flag2 = False
for x in range(n):
	new = input()
	list1.append(new)
	if new.endswith(char1):
		flag1 = True
	if new.startswith(char2):
		flag2 = True
	if new == word:
		flag1 = True
		flag2 = True
if flag1 and flag2:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
flag1 = False
flag2 = False
flag3 = False
for i in range(n):
	word = input()
	if password == word:
		flag1 = True
	if word[1] == password[0]:
		flag2 = True
	if word[0] == password[1]:
		flag3 = True
if flag1:
	print('YES')
elif flag2 and flag3:
	print('YES')
else:
	print('NO')


s = input()
a1 = s[0]
b1 = s[1]
check = 0
x = []
y = []
n = int(input())
for i in range(0, n):
	s1 = input()
	if (s == s1 or s == s1[::-1]) and check == 0:
		print('YES')
		check = 1
	else:
		x.append(s1[0])
		y.append(s1[1])
if check == 0:
	if a1 in y and b1 in x:
		print('YES')
	else:
		print('NO')


s = input()
words = []
n = int(input())
for _ in range(n):
	words.append(input())
f = 0
for i in range(n):
	st = words[i][1]
	for j in range(n):
		end = words[j][0]
		if st + end == s or s == words[j]:
			f = 1
if f:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
first = list()
second = list()
test3 = False
for i in range(n):
	a = input()
	if a == password:
		test3 = True
	first.append(a[0])
	second.append(a[1])
test1 = False
test2 = False
for i in first:
	if password[1] == i:
		test1 = True
for i in second:
	if password[0] == i:
		test2 = True
if test1 and test2 or test3:
	print('YES')
else:
	print('NO')


pw = input()
n = int(input())
a = 0
b = 0
for i in range(n):
	x = input()
	if x == pw:
		a += 1
		b += 1
		break
	else:
		if x[0] == pw[1]:
			a += 1
		if x[1] == pw[0]:
			b += 1
print('YES') if a >= 1 and b >= 1 else print('NO')


p = input()
n = int(input())
a = [0] * n
for i in range(n):
	a[i] = input()
fl = False
for i in range(n):
	for j in range(n):
		if a[i] == p or a[j] == p or a[i][1] + a[j][0] == p:
			fl = True
			break
if fl:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
words = []
for i in range(n):
	words.append(input())
for word_i in words:
	for word_k in words:
		if password in word_i + word_k:
			print('YES')
			exit()
print('NO')


x = input()
p = 0
q = 0
for _ in range(int(input())):
	n = input()
	if n == x or n == x[::-1]:
		exit(print('YES'))
	if n[1] == x[0]:
		p = 1
	if n[0] == x[1]:
		q = 1
	if p and q:
		exit(print('YES'))
if p and q:
	print('YES')
else:
	print('NO')


pas = input()
n = int(input())
words = []
for i in range(n):
	words.append(input())
for i in words:
	if i == pas or i == pas[::-1]:
		print('YES')
		quit()
count1 = 0
count2 = 0
for i in words:
	if i[0] == pas[1]:
		count1 = count1 + 1
	if i[1] == pas[0]:
		count2 += 1
if count1 >= 1 and count2 >= 1:
	print('YES')
else:
	print('NO')


pas = input()
n = int(input())
s = []
flag1 = False
flag2 = False
flag3 = False
for i in range(n):
	s.append(input())
for i in s:
	if i[1] == pas[0]:
		flag1 = True
	if i[0] == pas[1]:
		flag2 = True
	if i == pas:
		flag3 = True
if flag1 and flag2 or flag3:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
tries = [input() for _ in range(n)]

def get_result():
	for t in tries:
		if t == password:
			return True
		for i in range(n):
			if t[1] + tries[i][0] == password or tries[i][1] + t[0] == password:
				return True
	return False
print('YES' if get_result() else 'NO')


r = list(input())
a = []
b = []
t = 0
for i in range(int(input())):
	p = list(input())
	if p == r:
		t += 1
	a.append(p[0])
	b.append(p[1])
if r[0] in b and r[1] in a or t == 1:
	print('YES')
else:
	print('NO')


def main():
	pw = input()
	n = int(input())
	flag = [False] * 3
	for i in range(n):
		bk = input()
		if bk == pw:
			flag[0] = True
		if bk[1] == pw[0]:
			flag[1] = True
		if bk[0] == pw[1]:
			flag[2] = True
	if flag[0] or (flag[1] and flag[2]):
		print('YES')
	else:
		print('NO')
main()


st = input()
dct = [input() for i in range(int(input()))]
first = [i[0] for i in dct]
last = [i[1] for i in dct]
print('YES') if st in dct or (st[0] in last and st[1] in first) else print('NO')


s = input()
n = int(input())
ss = []
for i in range(n):
	a = input()
	ss.append(a)
for i in range(n):
	for j in range(0, n):
		str = ss[i] + ss[j]
		str2 = ss[j] + ss[i]
		if s in str or s in str2:
			print('yes')
			exit(0)
print('no')


from sys import stdin, stderr, maxsize
mod = int(1000000000.0) + 7

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
from collections import Counter, defaultdict
from itertools import permutations

def bark():
	s = S()
	f = ''
	l = ''
	arr = []
	for _ in range(I()):
		x = S()
		if x == s or x[::-1] == s:
			return 'YES'
		arr.append(x)
	p = permutations(arr, 2)
	for i in p:
		if s in ''.join(i):
			return 'YES'
	return 'NO'
print(bark())


c = input()
n = int(input())
l = []
f = 0
for i in range(n):
	l.append(input())
for i in l:
	for j in l:
		if c in i + j:
			f = 1
			break
if f == 1:
	print('YES')
else:
	print('NO')


password = input()
n = int(input())
lst = []
(a, b, c) = (True, True, True)
for i in range(n):
	lst.append(input())
if password in lst or password[1] + password[0] in lst:
	print('YES')
else:
	for i in range(n):
		if lst[i][0] == password[1] and a:
			for j in range(n):
				if lst[j][1] == password[0]:
					print('YES')
					b = False
					c = False
					a = False
					break
				elif j == n - 1 and i == n - 1:
					b = True
					a = False
					break
	for i in range(n):
		if lst[i][1] == password[0] and b:
			for j in range(n):
				if lst[j][0] == password[1]:
					print('YES')
					c = False
					b = False
					break
				elif j == n - 1 and i == n - 1:
					c = False
					print('No')
					break
	if c:
		print('NO')


string1 = input()
n = int(input())
(flag1, flag2) = (False, False)
for i in range(n):
	string2 = input()
	if string2 == string1:
		(flag1, flag2) = (True, True)
	if string2[1] == string1[0]:
		flag1 = True
	if string2[0] == string1[1]:
		flag2 = True
if flag1 & flag2:
	print('YES')
else:
	print('NO')


password_m = str(input())
n = int(input())
list = []
list_1 = []
list_2 = []
for x in range(n):
	a = input()
	list.append(a)
if password_m in list:
	print('YES')
elif password_m[::-1] in list:
	print('YES')
else:
	if len(list) == 1:
		list.append(list[0])
	for x in range(len(list)):
		b = list[x]
		if b[1] == password_m[0]:
			list_1.append(1)
		elif b[0] == password_m[1]:
			list_2.append(1)
	if len(list_1) > 0 and len(list_2) > 0:
		print('YES')
	else:
		print('NO')


s = input()
n = int(input())
wi = input()
f = wi[0]
sec = wi[1]
for i in range(n - 1):
	temp = input()
	wi += temp
	f += temp[0]
	sec += temp[1]
if wi.find(s) != -1 or (f.find(s[1]) != -1 and sec.find(s[0]) != -1):
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
Flag1 = Flag2 = False
for i in range(n):
	tmp = input()
	if tmp == s:
		print('YES')
		exit()
	if tmp[1] == s[0]:
		Flag1 = True
	if tmp[0] == s[1]:
		Flag2 = True
if Flag1 and Flag2:
	print('YES')
else:
	print('NO')


s = input()
n = int(input())
a = []
for i in range(n):
	a.append(input())
t = 0
if s in a:
	print('YES')
else:
	for i in range(n):
		if a[i][1] == s[0]:
			for j in range(0, n):
				if a[j][0] == s[1]:
					t = 1
			break
	if t == 1:
		print('YES')
	else:
		print('NO')


c = input()
a = int(input())
s = 0
t = 0
for i in range(a):
	x = input()
	if x == c:
		s = 1
		t = 1
	else:
		if x[0] == c[1]:
			s = 1
		if x[1] == c[0]:
			t = 1
if s == t == 1:
	print('YES')
else:
	print('NO')


pas = input()
n = int(input())
a = []
found = False
for i in range(n):
	temp = input()
	a.append(temp)
	if temp == pas:
		found = True
for i in a:
	for j in a:
		if pas in i + j:
			found = True
			break
if found:
	print('YES')
else:
	print('NO')


a = input()
s = []
n = int(input())
for i in range(n):
	b = input()
	s.append(b)
c1 = 0
c2 = 0
for i in range(n):
	if s[i][0] == a[-1]:
		c1 += 1
	if s[i][-1] == a[0]:
		c2 += 1
if a in s:
	print('YES')
elif c1 > 0 and c2 > 0:
	print('YES')
else:
	print('NO')


k = str(input())
n = int(input())
s = []
for i in range(n):
	s.append(str(input()))
pri = ''
if k in s:
	pri = 'YES'
else:
	for i in range(len(s)):
		for j in range(len(s)):
			if k in str(s[i]) + str(s[j]) or k in str(s[j]) + str(s[i]):
				pri = 'YES'
				break
if pri == '':
	pri = 'NO'
print(pri)


password = input()
n = int(input())
words = list()
for i in range(n):
	words.append(input())
first_word = [words[i][0] for i in range(n)]
second_word = [words[i][1] for i in range(n)]
if password in words:
	print('YES')
elif password[1] in first_word and password[0] in second_word:
	print('YES')
else:
	print('NO')


def sweet_home(array, key):
	for i in array:
		for x in array:
			new_1 = i + x
			new_2 = x + i
			if new_1.find(key) != -1 or new_2.find(key) != -1:
				return 'YES'
	return 'NO'
password = str(input())
n = int(input())
words = []
for i in range(0, n):
	words.append(str(input()))
print(sweet_home(words, password))


s = input()
n = int(input())
a = []
for i in range(n):
	a.append(input())
for i in a:
	for j in a:
		if s in i + j:
			print('YES')
			exit()
print('NO')


s = input()
n = int(input())
mas1 = []
mas2 = []
for i in range(n):
	x = input()
	if x == s:
		print('YES')
		exit()
	mas1.append(x[0])
	mas2.append(x[1])
for i2 in range(len(mas2)):
	if mas2[i2] == s[0]:
		for i1 in range(len(mas1)):
			if mas1[i1] == s[1]:
				print('YES')
				exit()
print('NO')


s = input()
n = int(input())
t = k = 0
visited = [False, False]
for i in range(n):
	s1 = input()
	if s1 != s:
		if s1[0] == s[1] and s1[1] == s[0]:
			print('YES')
			k = 0
			break
		if s1[0] == s[1] and visited[1] == False:
			t += 1
			visited[1] = True
		elif s1[1] == s[0] and visited[0] == False:
			t += 1
			visited[0] = True
		if t == 2:
			print('YES')
			k = 0
			break
		else:
			k = 1
	else:
		print('YES')
		k = 0
		break
if k == 1:
	print('NO')


def test():
	string = input()
	n = input()
	n = int(n)
	arr = []
	for i in range(n):
		temp = input()
		if temp == string:
			print('YES')
			return
		arr.append(temp)
	for i in range(len(arr)):
		for j in range(len(arr)):
			if arr[i][1] + arr[j][0] == string:
				print('YES')
				return
	print('NO')
test()


pas = list(input())
n = int(input())
act = []
for i in range(n):
	mat = list(input())
	act.append(mat)
flag = 0
for i in range(n):
	if pas[0] == act[i][1]:
		for j in range(n):
			if pas[1] == act[j][0]:
				flag = 1
				break
for i in range(n):
	if pas == act[i]:
		flag = 1
		break
if flag == 0:
	print('NO')
elif flag == 1:
	print('YES')


s = input()
n = int(input())
a = [input() for i in range(n)]
for x in a:
	for y in a:
		if s in x + y:
			print('YES')
			exit(0)
print('NO')


import sys
import math
s = input()
n = int(input())
Flag1 = Flag2 = False
for i in range(n):
	tmps = input()
	if tmps == s:
		print('YES')
		exit()
	if tmps[1] == s[0]:
		Flag1 = True
	if tmps[0] == s[1]:
		Flag2 = True
if Flag1 and Flag2:
	print('YES')
else:
	print('NO')


one = input()
num = int(input())
twos = []
for i in range(num):
	twos.append(input())
if one in twos or one[::-1] in twos:
	print('YES')
else:
	(flag1, flag2) = (False, False)
	for i in range(num):
		if twos[i][0] == one[1]:
			flag1 = True
		if twos[i][1] == one[0]:
			flag2 = True
	if flag1 and flag2:
		print('YES')
	else:
		print('NO')


S = input()
a = S[0]
b = S[1]
T = int(input())
k = 0
j = 0
m = 0
while T > 0:
	R = input()
	N = len(R)
	if R[0] == S[1]:
		j = j + 1
	if R[N - 1] == S[0]:
		k = k + 1
	if S == R:
		m = 1
	T = T - 1
if m == 1:
	print('YES')
elif j != 0 and k != 0:
	print('YES')
else:
	print('NO')