Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries.
Jessie’s queries are as follows:

She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority.
Note:

Every recipe has a unique priority

-----Input-----

First line contains an integer N - the number of recipes.

Followed by N strings Si along with an integer each Vi.
Si stands for the recipe and Vi for the priority.

It is followed by an integer Q - the number of queries.

Followed by Q strings Qi.

Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'.

-----Output-----
Q – lines, each contain the answer for each of the query.

If for a query no recipe matches print "NO". (Without quotes)
Constraints:
0 <= N <= 1000

0 <= Q <= 1000

-10^9 <= Vi <= 10^9

1 <= |Si| <= 1000 (length of Si)

1 <= |Qi| <= 1000 (length of Qi)

-----Example-----
Input:
4
flour-with-eggs 100
chicken-ham -10
flour-without-eggs 200
fish-with-pepper 1100
6
f
flour-with
flour-with-
c
fl
chik

Output:
fish-with-pepper
flour-without-eggs
flour-with-eggs
chicken-ham
flour-without-eggs
NO

Difficulty: interview

Starter code:

Solutions:
nr=int(input())
d={}
for r in range(nr):
 s,v=list(map(str,input().split()))
 d[int(v)]=s
q=int(input())
lis=[]
for i in range(q):
 lis.append(input())
l=list(d.keys())
l.sort(reverse=True)
ans='NO'


for j in lis:
 ans='NO'
 for k in l:
  if len(j)<=len(d[k]):
   a=d[k]
   if j==a[0:len(j)]:
    ans=a
    break
 print(ans) 


# cook your dish here
n=int(input())
d=dict()
for _ in range(n):
 aa=input().split()
 s=aa[0]
 v=int(aa[1])
 d[s]=v
q=int(input())
for _ in range(q):
 qi=input()
 value=None
 recep=None
 for i in d:
  if i.startswith(qi):
   if value is None or value<d[i]:
    value=d[i]
    recep=i
 if recep==None:
  print("NO")
 else:
  print(recep)

# cook your dish here
n=int(input())
d=dict()
for _ in range(n):
 aa=input().split()
 s=aa[0]
 v=int(aa[1])
 d[s]=v
q=int(input())
for _ in range(q):
 qi=input()
 value=None
 recep=None
 for i in d:
  if i.startswith(qi):
   if value is None or value<d[i]:
    value=d[i]
    recep=i
 if recep==None:
  print("NO")
 else:
  print(recep)

# cook your dish here
import re
n=int(input())
l=[]
for _ in range(n):
 l.append(input().split())
 l[-1][1]=int(l[-1][1])
 
l.sort(key=lambda x: x[1], reverse=True)
q=int(input())
for _ in range(q):
 
 s="^"+input()
 
 for k in l:
  if re.findall(s, k[0]):
   print(k[0])
   break
 else:
  print('NO')
 


# cook your dish here
import re
n=int(input())
l=[]
for _ in range(n):
 l.append(input().split())
 l[-1][1]=int(l[-1][1])
 
l.sort(key=lambda x: x[1], reverse=True)
q=int(input())
for _ in range(q):
 
 s="^"+input()
 
 for k in l:
  if re.findall(s, k[0]):
   print(k[0])
   break
 else:
  print('NO')
 


n=int(input())
s=[]
a=[]
for i in range(n):
 si,vi=input().split()
 vi=int(vi)
 a.append(vi)
 s.append(si)
#print(s)
#print(a)
q=int(input())
for j in range(0,q):
 qi=input()
 ma=-20000000009
 pos=-1
 for k in range(0,n):
  if s[k].startswith(qi):
   #print(s[k])
   if a[k]>ma:
    #print(a[k])
    pos=k
    ma=a[k]
 if pos==-1:
  print("NO")
 else:
  print(s[pos])

# cook your dish here
n=int(input())
s=[]
a=[]
for i in range(n):
 si,vi=input().split()
 vi=int(vi)
 a.append(vi)
 s.append(si)
#print(s)
#print(a)
q=int(input())
for j in range(0,q):
 qi=input()
 ma=-20000000009
 pos=-1
 for k in range(0,n):
  if s[k].startswith(qi):
   #print(s[k])
   if a[k]>ma:
    #print(a[k])
    pos=k
    ma=a[k]
 if pos==-1:
  print("NO")
 else:
  print(s[pos])

 
  
  
  
  
  
  


res=[]
for _ in range(int(input())):
 a,b=list(map(str,input().split()))
 res.append([a,int(b)])
res.sort(key=lambda x:x[-1])
#print(res)
for _ in range(int(input())):
 check=input().strip()
 f=1
 for i in range(len(res)-1,-1,-1):
  if res[i][0][:len(check)]==check:
   f=0
   ans=res[i][0]
   break
 if f==1:
  print("NO")
 elif f==0:
  print(ans)


class TrieNode():
 def __init__(self):
  self.children=[None]*27 
  self.ispresent=0 
  self.priority="aa"
  self.index=-1 
class Trie():
 def __init__(self):
  self.root=self.getnode()
 def getnode(self):
  return TrieNode()
 def getind(self,ch):
  if ch=='-': return 26 
  return ord(ch)-97 
 def insert(self,key,pos,pri):
  curr=self.root 
  l=len(key)
  for i in range(l):
   place=self.getind(key[i])
   if curr.children[place]==None:
    curr.children[place]=self.getnode()
    curr=curr.children[place]
    curr.index=pos
    curr.priority=pri 
   else:
    curr=curr.children[place]
    if pri>curr.priority:
     curr.priority=pri 
     curr.index=pos 
  curr.ispresent=1 
 def search(self,key):
  curr=self.root 
  l=len(key)
  for i in range(l):
   place=self.getind(key[i])
   if curr.children[place]==None:
    return -1 
   curr=curr.children[place]
  return curr.index 
data=Trie()
n=int(input())
strings=[]
for i in range(n):
 string,pri=input().split()
 pri=int(pri)
 data.insert(string,i,pri)
 strings.append(string)
q=int(input())
for i in range(q):
 start=input()
 ans=data.search(start)
 if ans==-1:
  print('NO')
 else:
  #print(ans,start)
  print(strings[ans])
  
 
    


n=int(input())
li=[]
for i in range(n):
 string,pri=input().split()
 pri=int(pri)
 li.append([string,pri])
li.sort(key=lambda x:x[1],reverse=True)
q=int(input())
for i in range(q):
 start=input()
 f=0
 for i in range(n):
  if li[i][0].startswith(start):
   f=1 
   print(li[i][0])
   break 
 if not f:
  print('NO')