// Arup Guha
// 12/17/2019
// Solution to 2014 February USACO Silver Problem: Auto-Complete

import java.util.*;
import java.io.*;

public class auto {
	
	public static void main(String[] args) throws Exception {
		
		// Read in the points, padding on the left and right ends.
		BufferedReader stdin = new BufferedReader(new FileReader("auto.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int n = Integer.parseInt(tok.nextToken());
		int numQ = Integer.parseInt(tok.nextToken());
		
		// Read and sort, leaving access to old indexes.
		word[] words = new word[n];
		for (int i=0; i<n; i++)
			words[i] = new word(stdin.readLine().trim(), i+1);
		Arrays.sort(words);
		
		StringBuffer sb = new StringBuffer();
		
		// Process queries.
		for (int i=0; i<numQ; i++) {
		
			// Get the query.
			tok = new StringTokenizer(stdin.readLine());
			int rank = Integer.parseInt(tok.nextToken());
			String pre = tok.nextToken();
		
			// Find the first index where this prefix is found -1 if not found at all.
			int idx = binsearch(words, pre);
			
			// Here is when the query is valid.
			if (idx != -1 && idx+rank-1 < n && isPre(pre, words[idx+rank-1].w))
				sb.append(words[idx+rank-1].idx+"\n");
			
			// Otherwise it's not.
			else
				sb.append("-1\n");
		}

		// Output to file.
		PrintWriter out = new PrintWriter(new FileWriter("auto.out"));
		out.print(sb);
		out.close();		
		stdin.close();
	}
	
	// Not sure why I wrote this...
	public static boolean isPre(String s, String t) {
		return t.indexOf(s) == 0;
	}
	
	// Runs the binary search to return the first index in words (sorted) that has pre as a prefix.
	public static int binsearch(word[] words, String pre) {
	
		int low = 0, high = words.length-1;
		
		// I stop one short, just in case.
		while (low < high) {
			
			// Do the comparison.
			int mid = (low+high)/2;
			int cmp = words[mid].w.compareTo(pre);
			
			// This is safe.
			if (cmp < 0) low = mid+1;
			
			// So is this.
			else if (cmp == 0) return mid;
			
			// Tricky, but so is this.
			else if (!isPre(pre, words[mid].w)) high = mid-1;
			
			// Otherwise, this is the best we can do.
			else high = mid;
		}
		
		// Since I stopped short, I have to do this.
		if (isPre(pre, words[low].w)) return low;
		if (isPre(pre, words[high].w)) return high;
		return -1;
	}
	
}

class word implements Comparable<word> {
	public String w;
	public int idx;
	
	public word(String s, int i) {
		w = s;
		idx = i;
	}
	
	public int compareTo(word other) {
		return w.compareTo(other.w);
	}
}
	
	