#P1257D. Yet Another Monster Killing Problem
Yet Another Monster Killing Problem
Description
You play a computer game. In this game, you lead a party of heroes, and you have to clear a dungeon with monsters. Each monster is characterized by its power . Each hero is characterized by his power and endurance .
The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.
When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated monsters, the hero fights with the monster ). When the hero fights the monster, there are two possible outcomes:
- if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends;
- otherwise, the monster is defeated.
After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the -th hero cannot defeat more than monsters during each day), or if all monsters are defeated — otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.
Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times.
The first line contains one integer () — the number of test cases. Then the test cases follow.
The first line of each test case contains one integer () — the number of monsters in the dungeon.
The second line contains integers , , ..., (), where is the power of the -th monster.
The third line contains one integer () — the number of heroes in your party.
Then lines follow, each describing a hero. Each line contains two integers and (, ) — the power and the endurance of the -th hero.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or if it is impossible).
Input
The first line contains one integer () — the number of test cases. Then the test cases follow.
The first line of each test case contains one integer () — the number of monsters in the dungeon.
The second line contains integers , , ..., (), where is the power of the -th monster.
The third line contains one integer () — the number of heroes in your party.
Then lines follow, each describing a hero. Each line contains two integers and (, ) — the power and the endurance of the -th hero.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or if it is impossible).