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

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'class TrieNode():\n def __init__(self):\n  self.children=[None]*27 \n  self.ispresent=0 \n  self.priority="aa"\n  self.index=-1 \nclass Trie():\n def __init__(self):\n  self.root=self.getnode()\n def getnode(self):\n  return TrieNode()\n def getind(self,ch):\n  if ch==\'-\': return 26 \n  return ord(ch)-97 \n def insert(self,key,pos,pri):\n  curr=self.root \n  l=len(key)\n  for i in range(l):\n   place=self.getind(key[i])\n   if curr.children[place]==None:\n    curr.children[place]=self.getnode()\n    curr=curr.children[place]\n    curr.index=pos\n    curr.priority=pri \n   else:\n    curr=curr.children[place]\n    if pri>curr.priority:\n     curr.priority=pri \n     curr.index=pos \n  curr.ispresent=1 \n def search(self,key):\n  curr=self.root \n  l=len(key)\n  for i in range(l):\n   place=self.getind(key[i])\n   if curr.children[place]==None:\n    return -1 \n   curr=curr.children[place]\n  return curr.index \ndata=Trie()\nn=int(input())\nstrings=[]\nfor i in range(n):\n string,pri=input().split()\n pri=int(pri)\n data.insert(string,i,pri)\n strings.append(string)\nq=int(input())\nfor i in range(q):\n start=input()\n ans=data.search(start)\n if ans==-1:\n  print(\'NO\')\n else:\n  #print(ans,start)\n  print(strings[ans])\n  \n \n    \n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 34827