HackerLand is a country with $n$ beautiful cities and $m$ undirected roads. Like every other beautiful country, HackerLand has traffic jams.

Each road has a crowd value. The crowd value of a path is defined as the maximum crowd value for all roads in the path. For example, if the crowd values for all roads are $[1,4,5,6,3]$, then the crowd value for the path will be $max(1,4,5,6,3)=6$.

Each city $\boldsymbol{i}$ has a type value, $T_i$, denoting the type of buildings in the city.

David just started his vacation in HackerLand. He wants to travel from city $\boldsymbol{x}$ to city $y$. He also wants to see at least $\boldsymbol{\mbox{k}}$ different types of buildings along the path from $\boldsymbol{x}$ to $y$. When choosing a path from city $\boldsymbol{x}$ to city $y$ that has at least $\boldsymbol{\mbox{k}}$ different types of buildings along the path, David always selects the one with the minimum crowd value.

You will be given $\textit{q}$ queries. Each query takes the form of $3$ space-separated integers, $x_i$, $y_i$, and $k_i$, denoting the respective values for starting city, destination city, and minimum number of unique buildings that David wants to see along the way. For each query, you must print the minimum crowd value for a path between $x_i$ and $y_i$ that has at least $k_i$ different buildings along the route. If there is no such path, print -1.

Note: A path may contain cycles (i.e., the same roads or cities may be traveled more than once).

Input Format

The first line contains $3$ space-separated integers denoting the respective values for $n$ (the number of cities), $m$ (the number of roads), and $\textit{q}$ (the number of queries).

The second line contains $n$ space-separated integers describing the respective building type for each city in array $\mathbf{T}$ (where the $\boldsymbol{i}$-th value is $T_i$ and $1\leq i\leq n$).   
 
Each of the $m$ subsequent lines defines a road in the form of $3$ space-separated integers, $x_i$, $y_i$, and $u_i$, defining an undirected road with crowd value $u_i$ that connects cities $x_i$ and $y_i$. 

Each of the $\textit{q}$ subsequent lines defines a query in the form of $3$ space-separated integers, $x_j$, $y_j$, and $k_j$ (where $0\leq j<q$), respectively. 

Constraints

$1\leq n,m,q\leq10^5$
$1\leq T_i,u_i\leq10^9$
$1\leq x_j,y_j,k_j\leq n$
Each road connect $2$ distinct cities, meaning no road starts and ends in the same city.

Output Format

For each query, print its answer on a new line.

Sample Input
7 6 1
1 1 4 5 1 3 2
1 2 3
2 6 2
2 3 4
3 4 3
2 4 9
5 7 9
1 2 4

Sample Output
4

Explanation

The diagram below depicts the country given as Sample Input. Different values of $T_i$ are shown in different colors.

The path for the last query (1 2 4) will be $1\to2\to3\to4\to3\to2\to6\to2$. David sees his first type of building in cities $1$ and $2$, his second type of building in city $3$, his third type of building in city $\begin{array}{c}4\end{array}$, and his fourth type of building in city $\boldsymbol{6}$. The crowd values for each road traveled on this path are $[3,4,3,3,4,2,2]$; the maximum of these values is $\begin{array}{c}4\end{array}$. Thus, we print $\begin{array}{c}4\end{array}$ on a new line.

Source: hackerrank | Difficulty: MEDIUM_HARD
Tags: Graph algorithms


Solutions:
from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


import math
import os
import random
import re
import sys
from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, N):
	while ids[N] != N:
		ids[N] = ids[ids[N]]
		N = ids[N]
	return N

def union(queries, blds, ids, p, q):
	N = root(ids, p)
	X = root(ids, q)
	if N == X:
		return (N, X)
	if len(blds[N]) > len(blds[X]):
		(bigb, smb) = (blds[N], blds[X])
	else:
		(bigb, smb) = (blds[X], blds[N])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[N][0]) + len(queries[N][1]) > len(queries[X][0]) + len(queries[X][1]):
		ids[X] = N
		blds[N] = bigb
		blds[X] = None
		return (X, N)
	else:
		ids[N] = X
		blds[X] = bigb
		blds[N] = None
		return (N, X)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for N in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [N for N in range(n)]
blds = [set([T[N]]) for N in range(n)]
for (u, x, y) in edges:
	(N, X) = union(queries, blds, ids, x, y)
	if N == X:
		continue
	tot_blds = len(blds[X])
	for qr in queries[N][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[X][0].add(qr)
		else:
			queries[X][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[X][1], qr)
	while queries[N][1] and queries[N][1][0][0] <= tot_blds:
		res[queries[N][1][0][-1]] = u
		heapq.heappop(queries[N][1])
	for qr in queries[N][1]:
		heapq.heappush(queries[X][1], qr)
	queries[N][0] = None
	queries[N][1] = None
	while queries[X][1] and queries[X][1][0][0] <= tot_blds:
		res[queries[X][1][0][-1]] = u
		heapq.heappop(queries[X][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def wheat(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def ion(queries, blds, ids, p, q):
	i = wheat(ids, p)
	j = wheat(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = ion(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if wheat(ids, qr[1]) != wheat(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, bld, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(bld[i]) > len(bld[j]):
		(bigb, smb) = (bld[i], bld[j])
	else:
		(bigb, smb) = (bld[j], bld[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		bld[i] = bigb
		bld[j] = None
		return (j, i)
	else:
		ids[i] = j
		bld[j] = bigb
		bld[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)


from collections import defaultdict
import heapq

def root(ids, i):
	while ids[i] != i:
		ids[i] = ids[ids[i]]
		i = ids[i]
	return i

def union(queries, blds, ids, p, q):
	i = root(ids, p)
	j = root(ids, q)
	if i == j:
		return (i, j)
	if len(blds[i]) > len(blds[j]):
		(bigb, smb) = (blds[i], blds[j])
	else:
		(bigb, smb) = (blds[j], blds[i])
	for b in smb:
		bigb.add(b)
	del smb
	if len(queries[i][0]) + len(queries[i][1]) > len(queries[j][0]) + len(queries[j][1]):
		ids[j] = i
		blds[i] = bigb
		blds[j] = None
		return (j, i)
	else:
		ids[i] = j
		blds[j] = bigb
		blds[i] = None
		return (i, j)
(n, m, q) = map(int, input().split())
T = list(map(int, input().split()))
edges = []
for i in range(m):
	(x, y, u) = map(int, input().split())
	edges.append((u, x - 1, y - 1))
edges.sort()
queries = [[set([]), []] for _ in range(n)]
res = [-1 for i in range(q)]
for qi in range(q):
	(x, y, k) = map(int, input().split())
	(x, y) = sorted([x - 1, y - 1])
	if x == y and k <= 1:
		res[qi] = 0
	else:
		qr = (k, x, y, qi)
		if x == y:
			heapq.heappush(queries[x][1], qr)
		else:
			queries[x][0].add(qr)
			queries[y][0].add(qr)
ids = [i for i in range(n)]
blds = [set([T[i]]) for i in range(n)]
for (u, x, y) in edges:
	(i, j) = union(queries, blds, ids, x, y)
	if i == j:
		continue
	tot_blds = len(blds[j])
	for qr in queries[i][0]:
		if root(ids, qr[1]) != root(ids, qr[2]):
			queries[j][0].add(qr)
		else:
			queries[j][0].discard(qr)
			if tot_blds >= qr[0]:
				res[qr[-1]] = u
			else:
				heapq.heappush(queries[j][1], qr)
	while queries[i][1] and queries[i][1][0][0] <= tot_blds:
		res[queries[i][1][0][-1]] = u
		heapq.heappop(queries[i][1])
	for qr in queries[i][1]:
		heapq.heappush(queries[j][1], qr)
	queries[i][0] = None
	queries[i][1] = None
	while queries[j][1] and queries[j][1][0][0] <= tot_blds:
		res[queries[j][1][0][-1]] = u
		heapq.heappop(queries[j][1])
for r in res:
	print(r)