P14118 [HackerEarth] Problem Statement
普及
题目描述

Problem Statement

Mark’s company Facebook employed n coders .Now Mark needs to build a tree hierarchy of supervisor - subordinate relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form:

Programmer p(i) is ready to become a supervisor of another programmer q(i) at extra cost r(i). The qualification aj of each employee is known, and for each of the application following is true: a(pi)>a(qi).(Here pi and qi are in subscript)

But since Mark is busy ,he needs your help to calculate the minimum cost of such a hierarchy, or find out whether it is impossible to build it.

Input

The first input line contains T – No of Test Case.(1 ≤ T ≤ 20)
For each test case input line contains an integer n (1 ≤ n ≤ 1000) – no of programmers in the company. The following line contains n space-separated integers a(j) (0 ≤ a(j) ≤ 10^6) — the programmer qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: p(i) , q(i) and r(i) (1 ≤ p(i),q(i) ≤ n, 0 ≤ r(i) ≤ 10^6). Different applications can be similar, i.e. they can come from one and the same programmer who offered to become a supervisor of the same person but at a different cost!!! . For each application a(pi)>a(qi).

Output

For each test case output a single line — the minimum cost of building if there exist a hierarchy, or “-1” if it is impossible to build one..

It is guaranteed that all the answers will fit into long long

Explanation:

In the sample test case 1 :
One of the best possible ways to build a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the total minimum cost.

In the sample test case 2:
it is impossible to build the required hierarchy, so the answer is -1.

SAMPLE INPUT
2
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
3
1 2 3
2
3 1 2
3 1 3

SAMPLE OUTPUT
11
-1

输入格式

见题面

输出格式

见题面

样例输入
1 100 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 99 1 2 100000 2 3 99999 3 4 99998 4 5 99997 5 6 99996 6 7 99995 7 8 99994 8 9 99993 9 10 99992 10 11 99991 11 12 99990 12 13 99989 13 14 99988 14 15 99987 15 16 99986 16 17 99985 17 18 99984 18 19 99983 19 20 99982 20 21 99981 21 22 99980 22 23 99979 23 24 99978 24 25 99977 25 26 99976 26 27 99975 27 28 99974 28 29 99973 29 30 99972 30 31 99971 31 32 99970 32 33 99969 33 34 99968 34 35 99967 35 36 99966 36 37 99965 37 38 99964 38 39 99963 39 40 99962 40 41 99961 41 42 99960 42 43 99959 43 44 99958 44 45 99957 45 46 99956 46 47 99955 47 48 99954 48 49 99953 49 50 99952 50 51 99951 51 52 99950 52 53 99949 53 54 99948 54 55 99947 55 56 99946 56 57 99945 57 58 99944 58 59 99943 59 60 99942 60 61 99941 61 62 99940 62 63 99939 63 64 99938 64 65 99937 65 66 99936 66 67 99935 67 68 99934 68 69 99933 69 70 99932 70 71 99931 71 72 99930 72 73 99929 73 74 99928 74 75 99927 75 76 99926 76 77 99925 77 78 99924 78 79 99923 79 80 99922 80 81 99921 81 82 99920 82 83 99919 83 84 99918 84 85 99917 85 86 99916 86 87 99915 87 88 99914 88 89 99913 89 90 99912 90 91 99911 91 92 99910 92 93 99909 93 94 99908 94 95 99907 95 96 99906 96 97 99905 97 98 99904 98 99 99903 99 100 99902
样例输出
11 -1 0 0 9 -1 6 4 -1 -1 4 22 8 23 17 0 -1 1000000 3