#include <bits/stdc++.h>
using namespace std;
const int MX = 500005;
int dsu_par[MX];
int dsu_find(int v) {
  if (dsu_par[v] == v) return v;
  return dsu_par[v] = dsu_find(dsu_par[v]);
}
bool dsu_union(int u, int v) {
  u = dsu_find(u);
  v = dsu_find(v);
  if (u == v) return false;
  dsu_par[u] = v;
  return true;
}
vector<pair<int, int>> neigh[MX];
int depth[MX];
int par[MX];
void dfs(int v, int p, int d) {
  par[v] = p;
  depth[v] = d;
  for (auto e : neigh[v]) {
    int u = e.first;
    if (u == p) continue;
    if (!e.second) {
      dsu_union(u, v);
    }
    dfs(u, v, d + 1);
  }
}
struct E {
  int u, v, w;
  bool taken;
} edges[MX];
int main() {
  int n, k, m;
  ignore = scanf("%d %d %d", &n, &k, &m);
  for (int i = 1; i <= n; i++) dsu_par[i] = i;
  for (int i = 0; i < k; i++) {
    int u, v;
    ignore = scanf("%d %d", &u, &v);
    neigh[u].emplace_back(v, 1);
    neigh[v].emplace_back(u, 1);
    dsu_union(u, v);
  }
  for (int i = 0; i < m; i++) {
    ignore = scanf("%d %d %d", &edges[i].u, &edges[i].v, &edges[i].w);
    if (dsu_union(edges[i].u, edges[i].v)) {
      neigh[edges[i].u].emplace_back(edges[i].v, 0);
      neigh[edges[i].v].emplace_back(edges[i].u, 0);
      edges[i].taken = true;
    }
  }
  for (int i = 1; i <= n; i++) {
    dsu_par[i] = i;
  }
  dfs(1, 0, 1);
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    if (edges[i].taken) continue;
    int cnt = 0;
    int u = edges[i].u;
    int v = edges[i].v;
    while (u != v) {
      if (depth[u] < depth[v]) swap(u, v);
      if (dsu_par[u] == u) cnt++;
      dsu_union(u, par[u]);
      u = dsu_find(u);
    }
    ans += cnt * 1LL * edges[i].w;
  }
  for (int i = 2; i <= n; i++) {
    if (dsu_par[i] == i) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << ans << endl;
}
