#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, k, m, cnt, fa[N];
struct _ {
  int to, id;
} pre[N];
vector<_> g[N];
struct {
  int u, v, w;
} e[N];
int Find(int first) { return fa[first] ? fa[first] = Find(fa[first]) : first; }
int dep[N], ans[N];
void dfs(int first, int d, int f) {
  dep[first] = d;
  for (_ e : g[first])
    if (e.to != f) {
      pre[e.to] = {first, e.id};
      dfs(e.to, d + 1, first);
    }
}
int main() {
  scanf("%d%d%d", &n, &k, &m);
  int tot = 0;
  while (k--) {
    int u, v;
    scanf("%d%d", &u, &v);
    fa[Find(u)] = Find(v);
    g[u].push_back({v, 1}), g[v].push_back({u, 1});
  }
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    int uu = Find(u), vv = Find(v);
    if (uu == vv)
      e[++cnt] = {u, v, w};
    else {
      fa[uu] = vv;
      g[u].push_back({v, 0}), g[v].push_back({u, 0});
    }
  }
  dfs(1, 0, 0);
  memset(fa, 0, sizeof fa);
  for (int i = 1; i <= cnt; ++i) {
    int u = e[i].u, v = e[i].v, w = e[i].w;
    while (Find(u) != Find(v)) {
      if (dep[u] < dep[v]) swap(u, v);
      if (Find(u) != Find(pre[u].to)) {
        fa[u] = Find(pre[u].to);
        ans[u] = w;
      }
      u = Find(u);
    }
  }
  long long sum = 0;
  for (int i = 1; i <= n; ++i)
    if (pre[i].to && pre[i].id) {
      if (!ans[i])
        return puts("-1"), 0;
      else
        sum += ans[i];
    }
  printf("%lld\n", sum);
}
