一、内容

You play a computer game. In this game, you lead a party of m heroes, and you have to clear a dungeon with n monsters. Each monster is characterized by its power ai. Each hero is characterized by his power pi and endurance siThe 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 kmonsters, the hero fights with the monster k+1). 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 i-th hero cannot defeat more than simonsters 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.

Input

 The first line contains one integer t
(1≤t≤105) — the number of test cases. Then the test cases follow.

The first line of each test case contains one integer n
(1≤n≤2⋅105) — the number of monsters in the dungeon.
The second line contains n
integers a1, a2, ..., an (1≤ai≤109), where ai is the power of the i

-th monster.
The third line contains one integer m
(1≤m≤2⋅105) — the number of heroes in your party.

Then m
lines follow, each describing a hero. Each line contains two integers pi and si (1≤pi≤109, 1≤si≤n) — the power and the endurance of the i

-th hero.
It is guaranteed that the sum of n+mover all test cases does not exceed 2⋅105
.

Output

For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or −1

if it is impossible).

Input

2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1

Output

5
-1

二、思路


  • 首先你必须从1开始一个一个得打怪兽,由于每个英雄可以使用无限次。那么我们可以用一个H【s】记录耐力值大于等于s的英雄得最大p是多少。
  • 所以当我们选择了len个怪物时,只需要判断len个怪物中的最大值Max是否大于H【len】,如果大于代表不合法,小于等于的话,代表这个英雄能够打败len个怪物。

三、代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#define max(a, b) (a > b ? a : b)
using namespace std;
const int N = 2e5 + 5;
int n, t, m, a[N], p, s, h[N];
int main() {
scanf("%d", &t);
while (t--) {
int maxa = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), maxa = max(maxa, a[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d%d", &p, &s), h[s] = max(h[s], p);
for (int i = n; i >= 1; i--) h[i] = max(h[i], h[i + 1]);//当前值等于右边的最大值【i,n】
if (h[1] < maxa) {
printf("-1\n");
} else {
int len = 0, Max = 0, ans = 1;
for (int i = 1; i <= n; i++) {
Max = max(a[i], Max), len++;
if (h[len] >= Max) continue; //可以继续添加数
len = 1; ans++; Max = a[i];
}
printf("%d\n", ans);
}
for (int i = 1; i <= n; i++) h[i] = 0;
}
return 0;
}