#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, tot, cnt;
int fir[N], fa[N], dep[N], top[N], son[N], size[N], id[N];
struct edge {
  int to, nxt;
} e[N << 1];
inline void add(int x, int y) {
  e[++tot].to = y;
  e[tot].nxt = fir[x];
  fir[x] = tot;
  e[++tot].to = x;
  e[tot].nxt = fir[y];
  fir[y] = tot;
}
inline void dfs1(int p) {
  size[p] = 1;
  for (int i = fir[p]; i; i = e[i].nxt) {
    int q = e[i].to;
    if (q == fa[p]) continue;
    fa[q] = p, dep[q] = dep[p] + 1;
    dfs1(q), size[p] += size[q];
    if (size[q] > size[son[p]]) son[p] = q;
  }
}
inline void dfs2(int p) {
  id[p] = ++cnt;
  if (son[p]) top[son[p]] = top[p], dfs2(son[p]);
  for (int i = fir[p]; i; i = e[i].nxt) {
    int q = e[i].to;
    if (q == fa[p] || q == son[p]) continue;
    top[q] = q, dfs2(q);
  }
}
struct tree {
  int val[N << 2];
  inline void pushdown(int p) {
    if (val[p]) val[p * 2] = val[p * 2 + 1] = val[p], val[p] = 0;
  }
  inline void change(int p, int l, int r, int ql, int qr, int c) {
    if (ql <= l && qr >= r) {
      val[p] = c;
      return;
    }
    int mid = (l + r) >> 1;
    pushdown(p);
    if (ql <= mid) change(p * 2, l, mid, ql, qr, c);
    if (qr > mid) change(p * 2 + 1, mid + 1, r, ql, qr, c);
  }
  inline int query(int p, int l, int r, int pos) {
    if (l == r) return val[p];
    int mid = (l + r) >> 1;
    pushdown(p);
    if (pos <= mid)
      return query(p * 2, l, mid, pos);
    else
      return query(p * 2 + 1, mid + 1, r, pos);
  }
} t;
int f[N], vis[N], rea[N];
inline int find(int x) {
  if (f[x] != x)
    return f[x] = find(f[x]);
  else
    return x;
}
struct Edge {
  int x, y, dis;
} que[N];
inline bool cmp1(Edge a, Edge b) { return a.dis < b.dis; }
inline bool cmp2(Edge a, Edge b) { return a.dis > b.dis; }
inline void t_change(int x, int y, int z) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    t.change(1, 1, n, id[top[x]], id[x], z);
    x = fa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  if (x ^ y) t.change(1, 1, n, id[y] + 1, id[x], z);
}
int main() {
  int k, m, a, b;
  long long ans = 0;
  scanf("%d%d%d", &n, &k, &m);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &a, &b);
    f[find(a)] = find(b), add(a, b);
  }
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", &que[i].x, &que[i].y, &que[i].dis);
  sort(que + 1, que + m + 1, cmp1);
  for (int i = 1; i <= m; i++) {
    int f1 = find(que[i].x), f2 = find(que[i].y);
    if (f1 == f2) continue;
    f[f1] = f2, vis[i] = 1, add(que[i].x, que[i].y);
  }
  dep[1] = 1, dfs1(1);
  top[1] = 1, dfs2(1);
  for (int i = m; i >= 1; i--) {
    if (vis[i]) {
      rea[dep[que[i].x] > dep[que[i].y] ? que[i].x : que[i].y] = 1;
      continue;
    } else
      t_change(que[i].x, que[i].y, que[i].dis);
  }
  for (int i = 2; i <= n; i++) {
    if (rea[i]) continue;
    int cur = t.query(1, 1, n, id[i]);
    if (cur == 0) {
      printf("-1\n");
      return 0;
    }
    ans += cur;
  }
  printf("%lld\n", ans);
  return 0;
}
