#include <bits/stdc++.h>
using namespace std;
const int N = 2000005;
int f[N], h[N], nxt[N], adj[N], w[N], fa[N], d[N], u, v, x, cna, n, m, k, t,
    cnum;
bool p[N];
long long ans;
struct ky {
  int u, v, x;
} a[N];
template <class I>
inline void sp(I& p, I& q) {
  I x = p;
  p = q, q = x;
}
inline int fd(const int x) { return f[x] == x ? x : f[x] = fd(f[x]); }
inline void add() {
  nxt[++t] = h[u], h[u] = t, adj[t] = v, w[t] = x, nxt[++t] = h[v], h[v] = t,
  adj[t] = u, w[t] = x;
}
inline void Add() { a[++cna] = (ky){u, v, x}; }
inline void dfs(const int x) {
  d[x] = d[fa[x]] + 1;
  for (register int i = h[x], j; i; i = nxt[i])
    if (!d[j = adj[i]]) fa[j] = x, p[j] = w[i] ? 1 : 0, dfs(j);
}
int main() {
  scanf("%d%d%d", &n, &k, &m);
  register int i;
  for (i = 1; i <= n; ++i) f[i] = i;
  for (i = 1; i <= k; ++i)
    scanf("%d%d", &u, &v), x = 1, add(), f[fd(u)] = fd(v);
  for (i = 1; i <= m; ++i)
    scanf("%d%d%d", &u, &v, &x), fd(u) ^ fd(v) ? f[fd(u)] = fd(v), x = 0,
                                                 add() : Add();
  dfs(1);
  for (i = 1; i <= n; ++i) f[i] = i;
  for (i = 1; i <= cna; ++i) {
    u = a[i].u, v = a[i].v, x = a[i].x;
    while ((u = fd(u)) ^ (v = fd(v)))
      d[u] < d[v] ? sp(u, v), 0 : 0, p[u] ? ++cnum, ans += x : 0, f[u] = fa[u];
  }
  cnum < k ? puts("-1") : printf("%lld\n", ans);
  return 0;
}
