#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 500007;
const int MAXL = 20;
struct eT {
  void setd(int _u, int _v, int _w, int _l) {
    u = _u, v = _v, w = _w, last = _l;
  }
  int u, v, w, last;
} edge[MAXN * 2];
int n, m, q;
long long ans;
int ke, la[MAXN];
int lca[MAXN][MAXL], lev[MAXN];
int eu[MAXN], ev[MAXN];
int fu[MAXN], fv[MAXN], fw[MAXN];
int dsu[MAXN], rnk[MAXN];
bool in[MAXN];
vector<int> ad[MAXN], dl[MAXN];
void init();
void input();
void work();
void predfs(int now, int fa, int nl);
int getlca(int u, int v);
void dfs(int now, int fa, multiset<int> &s);
void uni(multiset<int> &a, multiset<int> &b);
void addedge(int u, int v, int w) {
  edge[ke].setd(u, v, w, la[u]);
  la[u] = ke++;
  edge[ke].setd(v, u, w, la[v]);
  la[v] = ke++;
}
int getfa(int now) { return dsu[now] ? (dsu[now] = getfa(dsu[now])) : now; }
void uni(int u, int v) {
  u = getfa(u), v = getfa(v);
  if (u ^ v) {
    if (rnk[u] > rnk[v]) swap(u, v);
    dsu[u] = v;
    if (rnk[u] == rnk[v]) ++rnk[v];
  }
}
int main() {
  init();
  input();
  work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
  scanf("%d%d%d", &n, &q, &m);
  for (int i = 1; i <= q; ++i) scanf("%d%d", &eu[i], &ev[i]);
  for (int i = 1; i <= m; ++i) scanf("%d%d%d", &fu[i], &fv[i], &fw[i]);
}
void work() {
  memset(la, -1, sizeof(la));
  for (int i = 1; i <= q; ++i) uni(eu[i], ev[i]), addedge(eu[i], ev[i], 1);
  int u, v;
  for (int i = 1; i <= m; ++i) {
    u = getfa(fu[i]), v = getfa(fv[i]);
    if (u ^ v) {
      in[i] = true;
      addedge(fu[i], fv[i], 0);
      uni(u, v);
    }
  }
  memset(lca, -1, sizeof(lca));
  predfs(1, -1, 1);
  for (int j = 1; j < MAXL; ++j)
    for (int i = 1; i <= n; ++i)
      if (~lca[i][j - 1]) lca[i][j] = lca[lca[i][j - 1]][j - 1];
  int nl;
  for (int i = 1; i <= m; ++i) {
    if (!in[i]) {
      u = fu[i], v = fv[i];
      nl = getlca(u, v);
      ad[u].push_back(i), ad[v].push_back(i);
      dl[nl].push_back(i);
    }
  }
  ans = 0;
  multiset<int> s;
  dfs(1, -1, s);
  cout << ans << endl;
}
void predfs(int now, int fa, int nl) {
  lca[now][0] = fa;
  lev[now] = nl;
  for (int i = la[now]; ~i; i = edge[i].last)
    if (edge[i].v ^ fa) predfs(edge[i].v, now, nl + 1);
}
int getlca(int u, int v) {
  if (lev[u] < lev[v]) swap(u, v);
  for (int d = lev[u] - lev[v], b = 0; d; d >>= 1, ++b)
    if (d & 1) u = lca[u][b];
  if (u == v) return u;
  for (int i = MAXL - 1; i >= 0; --i)
    if (lca[u][i] ^ lca[v][i]) u = lca[u][i], v = lca[v][i];
  return lca[u][0];
}
void dfs(int now, int fa, multiset<int> &s) {
  multiset<int> ts;
  for (int i = la[now]; ~i; i = edge[i].last) {
    if (edge[i].v ^ fa) {
      ts.clear();
      dfs(edge[i].v, now, ts);
      if (edge[i].w) {
        if (ts.empty()) {
          printf("-1\n");
          exit(0);
        } else
          ans += *ts.begin();
      }
      uni(s, ts);
    }
  }
  for (auto x : ad[now]) s.insert(fw[x]);
  for (auto x : dl[now]) s.erase(s.find(fw[x])), s.erase(s.find(fw[x]));
}
void uni(multiset<int> &a, multiset<int> &b) {
  if (a.size() < b.size()) {
    uni(b, a);
    a.swap(b);
    return;
  }
  for (auto &x : b) a.insert(x);
}
