unique-ranking

The end sem exams at are over. Sneh is the class teacher of class X/C. She now has the hectic task of preparing the report card of her class.

Since Sneh is a computer teacher she tried to infuse some fun into his task. Sneh knew that there are n students in class and they had k subjects. Subjects are numbered from 1 to k and students have unique id from 1 to n.
She now wanted to know which student performed the worst in a group of subjects. She ordered those group priority wise that is if the group contained subjects 1 ,3 and 5 then while determining the worst performer initially subject 1 will be taken in consideration and only those students who have performed worst in subject 1(there might be a tie) will be considered for the next subject that is subject 3 and the rest will be eliminated and ultimately after all the subjects are considered the 
worst performer will be determined. If after considering all the subjects there is a tie among few students then the student with the smallest id will be the worst performer.
Help Sneh in determining the worst performer of his class.

Input Format:
First line contains n that is the number of students and k that is the toal number of subjects.
The next n line contains k integers denoting the marks obtained by the students in k subjects.
The next line contains q denoting the number of query you need to answer.
Each query contains a value x which indicates the number of subjects to be considered and then contains x distinct integers indicating the subjects to be considered arranged priority wise.

Output Format:
For each query output the unique id of the worst performer of the class.

Constraints:
1 ≤ n ≤ 1000
1 ≤ k ≤ 20
0 ≤ marks ≤ 100
1 ≤ q ≤ 1000  

SAMPLE INPUT
3 3
50 60 70
40 60 80
50 60 61
3
3 1 2 3
1 2
2 3 1

SAMPLE OUTPUT
2
1
3

Explanation

In query 1 we have to consider 3 subjects .First subject to consider priority wise is 1 and the worst performer in this subject is 2 and since there is no tie this is the final answer.

In query 2 we have to consider only 1 subject that is 2.Since there is a tie among all the students in this so student with the smallest id is selected.

In query 3 we have to consider 2 subjects .First one is 3 and the worst performer is 3.

Source: hackerearth | Difficulty: MEDIUM
Tags: Sorting

Implementation

Complete search


Solutions: