#include <bits/stdc++.h>
using namespace std;
class UFDS {
 public:
  vector<int> p, rank, setSizes;
  int numSets;
  UFDS(int n) {
    numSets = n;
    rank.assign(n, 0);
    p.assign(n, 0);
    for (int i = 0; i < n; i++) p[i] = i;
    setSizes.assign(n, 1);
  }
  int findSet(int i) { return (p[i] == i) ? i : p[i] = findSet(p[i]); }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!isSameSet(i, j)) {
      int x = findSet(i), y = findSet(j);
      if (rank[x] < rank[y]) {
        setSizes[x] += setSizes[y];
        p[y] = x;
      } else {
        setSizes[y] += setSizes[x];
        p[x] = y;
        if (rank[x] == rank[y]) rank[y]++;
      }
      numSets--;
    }
  }
};
const int MX = 1e5 * 5 + 10;
vector<pair<int, int> > graph[MX];
int ism[MX], par[MX], dep[MX];
vector<pair<int, int> > enEd;
vector<long long> W;
void dfs(int i, int p) {
  dep[i] = p == -1 ? 0 : dep[p] + 1;
  for (auto &e : graph[i])
    if (p != e.first)
      par[e.first] = i, ism[e.first] = e.second, dfs(e.first, i);
}
int main() {
  ios::sync_with_stdio(0);
  long long n, k, m, used = 0, ans = 0;
  cin >> n >> k >> m;
  UFDS ufds(n + 1), pos(n + 1);
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b, a--, b--;
    graph[a].push_back(pair<int, int>(b, 1));
    graph[b].push_back(pair<int, int>(a, 1));
    ufds.unionSet(a, b);
  }
  for (int i = 0; i < m; i++) {
    int a, b, w;
    cin >> a >> b >> w, a--, b--;
    if (!ufds.isSameSet(a, b)) {
      graph[a].push_back(pair<int, int>(b, 0));
      graph[b].push_back(pair<int, int>(a, 0));
      ufds.unionSet(a, b);
    } else
      enEd.push_back(pair<int, int>(a, b)), W.push_back(w);
  }
  par[0] = -1;
  dfs(0, -1);
  for (int i = 0; i < n; i++) pos.rank[i] = dep[i];
  for (int i = 0; i < enEd.size(); i++) {
    int a = pos.findSet(enEd[i].first), b = pos.findSet(enEd[i].second);
    while (a != b) {
      if (dep[a] < dep[b]) swap(a, b);
      if (ism[a]) ans += W[i], used++;
      pos.unionSet(a, par[a]);
      a = pos.findSet(par[a]);
    }
  }
  if (used == k)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
