#include <bits/stdc++.h>
inline int read() {
  int data = 0, w = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
  return data * w;
}
const int N(5e5 + 10), INF(1e9 + 7);
struct node {
  int a, b, v, op;
} a[N << 1];
struct edge {
  int next, to, v;
} e[N << 1];
inline int operator<(const node &a, const node &b) { return a.v < b.v; }
long long ans;
int n, m, K, cnt, head[N], e_num, f[N], fa[N], size[N], heavy[N];
int val[N], dfn[N], pos[N], bel[N], max[N << 2], lazy[N << 2];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
inline void add_edge(int from, int to, int v) {
  e[++e_num] = (edge){head[from], to, v};
  head[from] = e_num;
}
void dfs(int x) {
  size[x] = 1;
  for (int i = head[x]; i; i = e[i].next) {
    int to = e[i].to;
    if (to == fa[x]) continue;
    fa[to] = x;
    if (e[i].v) val[to] = INF;
    dfs(to), size[x] += size[to];
    if (size[heavy[x]] < size[to]) heavy[x] = to;
  }
}
void dfs(int x, int chain) {
  pos[dfn[x] = ++cnt] = x, bel[x] = chain;
  if (heavy[x]) dfs(heavy[x], chain);
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].to != fa[x] && e[i].to != heavy[x]) dfs(e[i].to, e[i].to);
}
void build(int x = 1, int l = 1, int r = n) {
  if (l == r) return (void)(max[x] = val[pos[l]]);
  int mid = (l + r) >> 1, ls = x << 1, rs = ls | 1;
  build(ls, l, mid), build(rs, mid + 1, r), lazy[x] = INF;
  max[x] = std::max(max[ls], max[rs]);
}
void pushtag(int x, int v) {
  max[x] = std::min(max[x], v), lazy[x] = std::min(lazy[x], v);
}
void pushdown(int x, int ls, int rs) {
  if (lazy[x] == INF) return;
  pushtag(ls, lazy[x]), pushtag(rs, lazy[x]), lazy[x] = INF;
}
void Update(int ql, int qr, int v, int x = 1, int l = 1, int r = n) {
  if (ql <= l && r <= qr) return pushtag(x, v);
  int mid = (l + r) >> 1, ls = x << 1, rs = ls | 1;
  pushdown(x, ls, rs);
  if (ql <= mid) Update(ql, qr, v, ls, l, mid);
  if (mid < qr) Update(ql, qr, v, rs, mid + 1, r);
  max[x] = std::max(max[ls], max[rs]);
}
int Query(int p, int x = 1, int l = 1, int r = n) {
  if (l == r) return max[x];
  int mid = (l + r) >> 1, ls = x << 1, rs = ls | 1;
  pushdown(x, ls, rs);
  if (p <= mid)
    return Query(p, ls, l, mid);
  else
    return Query(p, rs, mid + 1, r);
}
void Modify(int x, int y, int v) {
  for (; bel[x] != bel[y]; Update(dfn[bel[x]], dfn[x], v), x = fa[bel[x]])
    if (dfn[bel[x]] < dfn[bel[y]]) std::swap(x, y);
  if (dfn[x] > dfn[y]) std::swap(x, y);
  if (dfn[x] != dfn[y]) Update(dfn[x] + 1, dfn[y], v);
}
int main() {
  n = read(), K = read(), m = read();
  for (int i = 1; i <= K; i++) a[++cnt] = (node){read(), read(), 0, 1};
  for (int i = 1; i <= m; i++) a[++cnt] = (node){read(), read(), read(), 0};
  std::sort(a + 1, a + cnt + 1);
  int tot = cnt;
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= cnt; i++)
    if (find(a[i].a) != find(a[i].b)) {
      f[find(a[i].a)] = a[i].b;
      add_edge(a[i].a, a[i].b, a[i].op), add_edge(a[i].b, a[i].a, a[i].op);
      a[i].op = 0;
    } else
      a[i].op = 1;
  cnt = 0, dfs(1), dfs(1, 1), build();
  for (int i = 1; i <= tot; i++)
    if (a[i].op) Modify(a[i].a, a[i].b, a[i].v);
  for (int i = 1; i <= n; i++) {
    int t = Query(i);
    if (t == INF) {
      ans = -1;
      break;
    }
    ans += t;
  }
  printf("%lld\n", ans);
  return 0;
}
