Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef has a collection of N postage stamps. Each stamp belongs to some type, which are enumerated as positive integers. More valuable stamps have a higher enumerated type. So, type 1 is the least valuable type of stamps, type 2 is a bit more valuable, and so on.
Chef often communicates with other philatelists via a stamp exchange website called P-bay. On any particular day, P-bay lists several offers, each of which is represented as an unordered pair {A, B}, allowing its users to exchange stamps of type A with an equal number of stamps of type B. Chef can use such an offer to put up any number of stamps of enumerated type A on the website and get the same number of stamps of type B in return, or vice-versa (of course, he must have enough stamps of the corresponding type). Assume that any number of stamps Chef wants are always available on the site's exchange market. Each offer is open during only one day: Chef can't use it after this day, but he can use it several times during this day. If there are some offers which are active during a given day, Chef can use them in any order.
Chef is interested in making his collection as valuable as possible. Help him to find maximum possible value of his collection after going through (accepting or declining) all the offers. Value of Chef's collection is equal to the sum of type enumerations of all stamps in the collection.

------ Input ------ 

The first line of the input contains an integer T denoting the number of test cases. The  description of T test cases follows.
The first line of each test case contains two integers N and M denoting number of stamps in Chef's collection and number of offers, respectively.
Next line contains N integers denoting the enumerated types of stamps in Chef's collection.
The i^{th} of the following M lines contains three integers — T_{i}, A_{i} and B_{i} — denoting that on day T_{i}, there is an offer about exchanging stamps of types A_{i} and B_{i}.

------ Output ------ 

For each test case, output a single line containing maximum possible value of Chef's collection after all offers.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ N ≤ 2*10^{5}$
$1 ≤ M ≤ 5*10^{4}$
$Types are enumerated with integers from 1 to 5*10^{4}$
$1 ≤ T_{i} ≤ 5*10^{4}$

------ Subtasks ------ 

Subtask 1: (33 points)

$1 ≤ M ≤ 1000$
$Types are enumerated with integers from 1 to 1000$
$1 ≤ T_{i} ≤ 1000$

Subtask 2: (67 points)

$Original constraints$

----- Sample Input 1 ------ 
1

3 4

1 3 5

1 4 3

1 1 3

2 6 22

3 5 8
----- Sample Output 1 ------ 
16
----- explanation 1 ------ 
Example case 1. Chef can use 2nd offer to exchange the stamp of type 1 with an stamp of type 3. After that he can use 1st offer to exchange his 2 stamps of type 3 with 2 stamps of type 4. Finally, on 3rd day Chef uses the offer to exchange the stamp of type 5 with an stamp of type 8. After this transaction, value of his collection becomes 4 + 4 + 8 = 16.

Source: codechef | Difficulty: VERY_HARD
Tags: Data structures

Graph algorithms

Graph traversal


Solutions:
import collections

def solve(u):
	if u not in vis:
		vis.add(u)
		compo.append(u)
		for v in g[u]:
			solve(v)
t = int(input())
for _ in range(t):
	max_t = {}
	(n, m) = map(int, input().split())
	elems = list(map(int, input().split()))
	day_wise = {}
	for i in range(m):
		(d, a, b) = list(map(int, input().split()))
		if d in day_wise:
			day_wise[d].append([a, b])
		else:
			day_wise[d] = [[a, b]]
	for day in sorted(day_wise.keys(), reverse=1):
		vis = set()
		g = {}
		for (u, v) in day_wise[day]:
			if u in g:
				g[u].append(v)
			else:
				g[u] = [v]
			if v in g:
				g[v].append(u)
			else:
				g[v] = [u]
		for u in g:
			compo = []
			solve(u)
			mx = -float('inf')
			for i in compo:
				mx = max(mx, max_t.get(i, i))
			for i in compo:
				max_t[i] = mx
	ans = 0
	for e in elems:
		ans += max_t.get(e, e)
	print(ans)


T = int(input())
for t in range(T):
	(N, M) = map(int, input().split())
	cc = list(map(int, input().split()))
	all = dict()
	for i in range(M):
		(T, A, B) = map(int, input().split())
		if T in all:
			if A in all[T]:
				all[T][A].append(B)
			else:
				all[T][A] = [B]
			if B in all[T]:
				all[T][B].append(A)
			else:
				all[T][B] = [A]
		else:
			all[T] = {A: [B], B: [A]}
	days = list(all.keys())
	days.sort(reverse=True)
	fmax = dict()
	for day in days:
		G = all[day]
		vis = set()
		for vertex in G:
			if vertex in vis:
				continue
			viscomp = {vertex}
			maxcomp = 0
			stack = [vertex]
			while stack:
				u = stack.pop()
				maxcomp = max(fmax.get(u, u), maxcomp)
				for v in G[u]:
					if v not in viscomp:
						viscomp.add(v)
						stack.append(v)
			for v in viscomp:
				fmax[v] = maxcomp
			vis = vis.union(viscomp)
	sum = 0
	for stamp in cc:
		sum = sum + fmax.get(stamp, stamp)
	print(sum)