#include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define TT template TT using vec = vector; template bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; } template bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; #include const ll INF=1LL<<60; void solve(){ ll N; cin>>N; atcoder::mf_graphG(500); ll S=0,T=N+1; ll cost=0; rep(i,0,N){ ll p; cin>>p; if(p<0)G.add_edge(S,i+1,-p); else{ cost+=p; G.add_edge(i+1,T,p); } } ll M; cin>>M; rep(i,0,M){ ll u,v; cin>>u>>v; G.add_edge(u,v,INF); } ll K; cin>>K; ll now=N+2; rep(i,0,K){ ll a,b,s; cin>>a>>b>>s; cost+=s; G.add_edge(now,T,s); G.add_edge(a,now,INF); G.add_edge(b,now,INF); now++; } cost-=G.flow(S,T); cout<