#include <bits/stdc++.h>
using namespace std;
long long n, m1, m2, len, bo, ans, fa[1000005], f[1000000], dep[1000000],
    nex[1000000], wen[1000000], hea[1000000], val[1000000], v[1000000];
struct arr {
  long long x, y, z, bo;
} a[1000000];
bool cmp(arr x, arr y) { return x.z < y.z; }
void add(long long x, long long y, long long z) {
  ++len;
  nex[len] = hea[x];
  wen[len] = y;
  val[len] = z;
  hea[x] = len;
}
long long find(long long x) {
  if (fa[x] == x) return x;
  fa[x] = find(fa[x]);
  return fa[x];
}
void dfs(long long x, long long y) {
  f[x] = y;
  dep[x] = dep[y] + 1;
  for (long long i = hea[x]; i; i = nex[i])
    if (wen[i] != y) {
      v[wen[i]] = val[i];
      dfs(wen[i], x);
    }
}
signed main() {
  scanf("%lld%lld%lld", &n, &m1, &m2);
  for (long long i = 1; i <= m1; i++) scanf("%lld%lld", &a[i].x, &a[i].y);
  for (long long i = m1 + 1; i <= m1 + m2; i++)
    scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].z);
  sort(a + 1, a + m1 + m2 + 1, cmp);
  for (long long i = 1; i <= n; i++) fa[i] = i;
  for (long long i = 1, j = 0; i <= m1 + m2 && j < n - 1; i++) {
    long long x = find(a[i].x), y = find(a[i].y);
    if (x != y) {
      ++j;
      a[i].bo = 1;
      fa[find(x)] = find(y);
      add(a[i].x, a[i].y, a[i].z);
      add(a[i].y, a[i].x, a[i].z);
    }
  }
  dfs(1, 0);
  for (long long i = 1; i <= n; i++) fa[i] = i;
  for (long long i = m1 + 1; i <= m1 + m2; i++)
    if (!a[i].bo) {
      long long x = find(a[i].x), y = find(a[i].y), z = a[i].z;
      while (x != y) {
        if (dep[x] < dep[y]) swap(x, y);
        if (v[x] == 0) ans += z, ++bo;
        fa[x] = find(f[x]);
        x = find(x);
      }
    }
  if (bo < m1) ans = -1;
  printf("%lld", ans);
}
