Whiteboard Markers


As a TA, Lauren uses a lot of whiteboard markers. She has a collection of nn markers in kk different colours. Each marker 1≤i≤n1 \le i \le n has a particular amount of ink left, xix_i, and has a particular colour, cic_i.

Since whiteboard markers get worse as they run out of ink, when choosing a whiteboard marker of a particular colour to use next, Lauren always wants to choose the one with the most amount of ink first. If there are multiple markers of that colour with the greatest amount of ink, she will choose the marker with the lower index. After using a marker, it will consume some amount of ink.

More precisely, after Lauren uses yy units of ink from marker ii, the new value of xix_i will be xi−yx_i - y. If there are no markers of the desired colour with enough ink left, she will not use any marker (and will be sad). Note that Lauren can only use one marker at a time.

Help Lauren determine which marker to use, given the colour and amount of ink she needs to use.

Input

Your first line will contain nn and mm as space-separated integers, representing the number of whiteboard markers Lauren has the number of queries she will make.

Your next nn lines will contain an integer xix_i and a string cic_i, separated by a space, representing the amount of ink left in the iith marker and the colour of the iith marker respectively.

Your next mm lines will contain an integer yjy_j and a string djd_j, separated by a space, representing that Lauren needs to use yjy_j units of ink of colour djd_j.

Output

For each query, output the index (ii) of the marker that Lauren should use. If there are no markers of the desired colour with enough ink left, output 00.

Constraints

  • 1≤k≤101 \le k \le 10
  • 1≤xi,yj≤10001 \le x_i, y_j \le 1000 for all 1≤i≤n1 \le i \le n and for all 1≤j≤m1 \le j \le m
Subtask 1
  • 1≤n≤5001 \le n \le 500
  • 1≤m≤5001 \le m \le 500
Subtask 2
  • 1≤n≤1051 \le n \le 10^5
  • 1≤m≤1051 \le m \le 10^5

Example 1

Input 1
7 9
10 black
8 blue
5 black
10 black
9 blue
4 red
3 red
2 blue
10 black
3 blue
10 black
6 black
3 red
2 red
1 red
2 red
Output 1
5
1
2
4
0
6
7
6
0
Explanation

The initial whiteboard markers are:

Code 1
10 black
8 blue
5 black
10 black
9 blue
4 red
3 red

The queries are:

Code 2
2 blue
10 black
3 blue
10 black
6 black
3 red
2 red
1 red
2 red
  • The output of query 1, 2 blue, is 5. There are 2 blue markers, marker 2 with 8 ink and marker 5 with 9 ink, so we choose marker 5. Marker 5 now has 7 ink remaining.
  • The output of query 2, 10 black, is 1. There are 3 black markers, marker 1 with 10 ink, marker 3 with 5 ink, and marker 4 with 10 ink. Markers 1 and 4 both have 10 ink, so we choose the one with the lower index, marker 1. Marker 1 now has 0 ink remaining.
  • The output of query 3, 3 blue, is 2. There are 2 blue markers, marker 2 with 8 ink and marker 5 with 7 ink, so we choose marker 2. Marker 2 now has 5 ink remaining.

Example 2

Input 2
1 3
10 black
1 black
10 black
1 black
Output 2
1
0
1
Explanation
  • The output of query 1, 1 black, is 1. The only black marker has 10 ink. Marker 1 now has 9 ink remaining.
  • The output of query 2, 10 black, is 0. The only black marker only has 9 ink, which is not enough, and no markers are used. Marker 1 still has 9 ink remaining.
  • The output of query 3, 1 black, is 1. The only black marker has 9 ink. Marker 1 now has 8 ink remaining.

Example 3

Input 3
1 1
10 black
1 blue
Output 3
0
Explanation
  • The output of query 1, 1 blue, is 0. There are no blue markers in the collection, so we cannot use any markers.

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.