cswork / OAOpen practice workspace

Observer.AIOA #1

Choose the Best Flask

Estimated MediumJudge-ready

Topics (AI-labelled)Binary SearchPrefix & Ranges

Choose one flask type that can serve all orders and minimizes the total waste, i.e. marked capacity minus order volume; break ties by the smallest type index.

The standard input/output format and supplementary bounds on this site are as defined in this problem statement.

Input

n (1≤n≤1000); the next line contains n order volumes (1..10^9); then f (1≤f≤1000), followed by one line per flask type: first the number of markings, then the markings in ascending order. The total number of markings is ≤2×10^5.

Output

Output the 0-based flask type index; if no type can serve all orders, output -1.

Example 1

Input:

4
4 6 6 7
3
3
3 5 7
3
6 8 9
3
3 5 6

Output:

0

Example 2

Input:

2
10 11
1
2
1 5

Output:

-1

Example 3

Input:

3
12 10 9
2
1
3
5
2 2 3 11 17

Output:

1

Editorial

Loading solution…