import io import sys from collections import defaultdict, deque, Counter from itertools import permutations, combinations, accumulate from heapq import heappush, heappop from bisect import bisect_right, bisect_left from math import gcd import math _INPUT = """\ 6 4 5 4 1 1 1 4 1 1 2 0 0 1 2 4 1 3 5 4 5 1 5 3 3 4 5 4 2 3 4 5 4 0 0 0 0 1 2 1 1 3 1 1 4 1 1 5 1 20 10 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 4 0 0 3 3 0 1 5 0 4 1 2 4 2 3 2 3 4 3 4 5 1 5 6 2 6 7 4 7 8 5 2 9 3 6 9 1 9 10 8 """ # 最小費用流(minimum cost flow) class MinCostFlow: def __init__(self, n): self.n = n self.G = [[] for i in range(n)] def addEdge(self, f, t, cap, cost): # [to, cap, cost, rev] self.G[f].append([t, cap, cost, len(self.G[t])]) self.G[t].append([f, 0, -cost, len(self.G[f])-1]) def minCostFlow(self, s, t, f): n = self.n G = self.G prevv = [0]*n; preve = [0]*n INF = 1<<63 res = 0 while f: dist = [INF]*n dist[s] = 0 update = 1 while update: update = 0 for v in range(n): if dist[v] == INF: continue gv = G[v] for i in range(len(gv)): to, cap, cost, rev = gv[i] if cap > 0 and dist[v] + cost < dist[to]: dist[to] = dist[v] + cost prevv[to] = v; preve[to] = i update = 1 if dist[t] == INF: return -1 d = f; v = t while v != s: d = min(d, G[prevv[v]][preve[v]][1]) v = prevv[v] f -= d res += d * dist[t] v = t while v != s: e = G[prevv[v]][preve[v]] e[1] -= d G[v][e[3]][1] += d v = prevv[v] return res def input(): return sys.stdin.readline()[:-1] def solve(test): K, N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C=[0]*N min_cost_flow = MinCostFlow(N+2) for _ in range(M): a,b,c=map(int,input().split()) min_cost_flow.addEdge(a, b, 10**5, c) min_cost_flow.addEdge(b, a, 10**5, c) for i in range(K): C[A[i]-1]+=1 for i in range(N): if C[i]: min_cost_flow.addEdge(0, i+1, C[i], 0) if B[i]: min_cost_flow.addEdge(i+1, N+1, B[i], 0) print(min_cost_flow.minCostFlow(0, N+1, K)) def random_input(): from random import randint,shuffle N=randint(1,10) M=randint(1,N) A=list(range(1,M+1))+[randint(1,M) for _ in range(N-M)] shuffle(A) return (" ".join(map(str, [N,M]))+"\n"+" ".join(map(str, A))+"\n")*3 def simple_solve(): return [] def main(test): if test==0: solve(0) elif test==1: sys.stdin = io.StringIO(_INPUT) case_no=int(input()) for _ in range(case_no): solve(0) else: for i in range(1000): sys.stdin = io.StringIO(random_input()) x=solve(1) y=simple_solve() if x!=y: print(i,x,y) print(*[line for line in sys.stdin],sep='') break #0:提出用、1:与えられたテスト用、2:ストレステスト用 main(0)