import re def get_str(i,num): str_list = re.findall(r'.{{{str_length}}}'.format(str_length=i), num) return str_list def add_str(res,i,str_list): if res.get(i): res[i] += str_list else: res.update({i: str_list}) return res data = map(int, raw_input().strip().split()) n, q = data[0], data[1] books={} reader={} for i in range(0,n): book = {} num=str(raw_input().strip()) length=len(num) book = add_str(book, length, [num]) for item_len in range(1,length): length_temp=item_len for j in range(0,length_temp): str_list = get_str(item_len, num[j:]) book=add_str(book,item_len, str_list) books.update({i:book}) # print books result=[] for i in range(0,q): num=raw_input().strip() length,name=num.split() length=int(length) book_list = books[i].get(length, []) #print name,book_list if name in book_list: result.append(name) else: result.append('-1') for i in result: print i