#include <bits/stdc++.h>
using namespace std;
template <class _Tp>
_Tp gcd(_Tp a, _Tp b) {
  return (b == 0) ? (a) : (gcd(b, a % b));
}
const long long Inf = 1000000000000000000ll;
const int inf = 1000000000;
char buf[1 << 25], *p1 = buf, *p2 = buf;
inline int getc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 25, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int x = 0, f = 0;
  char c = getc();
  while (!isdigit(c)) (c == '-') && (f = 1), c = getc();
  while (isdigit(c)) x = x * 10 + (c & 15), c = getc();
  return f ? -x : x;
}
const int N = 500005;
struct Edge {
  int to, nxt, w;
} e[N * 2];
int head[N], Top, cnt, res[N];
int f[N], dep[N], fa[N], n, m, k, U[N], V[N], W[N];
int getfa(int x) {
  if (fa[x] == x) return x;
  return fa[x] = getfa(fa[x]);
}
void add(int u, int v, int w) {
  e[++Top].to = v;
  e[Top].nxt = head[u];
  head[u] = Top;
  e[Top].w = w;
}
void dfs(int u) {
  for (int i = head[u]; i; i = e[i].nxt) {
    int to = e[i].to;
    if (to == f[u]) continue;
    f[to] = u;
    dep[to] = dep[u] + 1;
    dfs(to);
  }
}
int main() {
  n = read();
  k = read();
  m = read();
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= k; ++i) {
    int x = read(), y = read();
    fa[getfa(x)] = getfa(y);
    add(x, y, 0);
    add(y, x, 0);
  }
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read(), w = read();
    if (getfa(x) == getfa(y)) {
      ++cnt;
      U[cnt] = x;
      V[cnt] = y;
      W[cnt] = w;
    } else {
      fa[getfa(x)] = getfa(y);
      add(x, y, 1);
      add(y, x, 1);
    }
  }
  dfs(1);
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= cnt; ++i) {
    int x = getfa(U[i]), y = getfa(V[i]);
    while (x != y) {
      if (dep[x] > dep[y]) {
        res[x] = W[i];
        fa[x] = getfa(f[x]);
        x = fa[x];
      } else {
        res[y] = W[i];
        fa[y] = getfa(f[y]);
        y = fa[y];
      }
    }
  }
  long long ans = 0;
  for (int u = 1; u <= n; ++u)
    for (int i = head[u]; i; i = e[i].nxt)
      if (e[i].to == f[u] && !e[i].w) {
        if (fa[u] == u) {
          puts("-1");
          return 0;
        }
        ans += res[u];
      }
  printf("%lld\n", ans);
  return 0;
}
