// Arup Guha
// 3/24/2015
// Solution to February 14 Bronze USACO Problem: Auto Completion

import java.util.*;
import java.io.*;

public class auto {

	final public static int MAXLEN = 1000;

	public static HashMap<String,Integer> map;
	public static String[] words;

	public static void main(String[] args) throws Exception {

		// Read in the grid.
		BufferedReader stdin = new BufferedReader(new FileReader("auto.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int numWords = Integer.parseInt(tok.nextToken());
		int numQueries = Integer.parseInt(tok.nextToken());
		words = new String[numWords];
		map = new HashMap<String,Integer>();
		for (int i=0; i<numWords; i++) {
			words[i] = stdin.readLine();
			map.put(words[i], i+1);
		}
		Arrays.sort(words);

		BufferedWriter fout = new BufferedWriter(new FileWriter("auto.out"));

		// Go through each query.
		for (int i=0; i<numQueries; i++) {

			// Get query and solve.
			tok = new StringTokenizer(stdin.readLine());
			int rank = Integer.parseInt(tok.nextToken());
			String partial = tok.nextToken();
			fout.write(solve(partial, rank)+"\n");
		}

		stdin.close();
		fout.close();
	}

	public static int solve(String partial, int rank) {

		// Our boundary string at the end...
		char[] last = new char[MAXLEN+1];
		Arrays.fill(last, 'z');
		for (int i=0; i<partial.length(); i++)
			last[i] = partial.charAt(i);

		// Find first string equal to or AFTER each of these in the list.
		int start = binSearch(partial, 0, words.length);
		int end = binSearch(new String(last), 0, words.length);

		// Not enough...
		if (rank > end-start) return -1;

		// This is the ID of the word we want.
		return map.get(words[start+rank-1]);
	}

	public static int binSearch(String partial, int low, int high) {

		// Do most of the search here.
		while (low < high-1) {

			int mid = (low+high)/2;

			// Our word comes before words[mid].
			if (partial.compareTo(words[mid]) < 0)
				high = mid;

			// Equal or later...
			else
				low = mid;
		}

		// Walk the last little bit.
		while (low < words.length && words[low].compareTo(partial) < 0) low++;
		return low;
	}
}