#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m1, m2, E;
long long ans;
int x[N], y[N], c[N], t[N], fa[N], vis[N], dep[N], f[N][20], res[N];
int fir[N], nex[N << 1], arr[N << 1], cost[N << 1];
inline int Find(int x) { return (fa[x] == x) ? fa[x] : fa[x] = Find(fa[x]); }
inline void Add_Edge(int x, int y, int len) {
  nex[++E] = fir[x];
  fir[x] = E;
  arr[E] = y;
  cost[E] = len;
}
void dfs(int x) {
  vis[x] = 1;
  for (int i = 1; i < 20; ++i) f[x][i] = f[f[x][i - 1]][i - 1];
  for (int i = fir[x]; i; i = nex[i]) {
    if (arr[i] == f[x][0]) continue;
    f[arr[i]][0] = x;
    dep[arr[i]] = dep[x] + 1;
    if (cost[i]) res[arr[i]] = -1;
    dfs(arr[i]);
  }
}
inline int LCA(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 19; i >= 0; --i) {
    if (dep[f[x][i]] >= dep[y]) x = f[x][i];
  }
  if (x == y) return x;
  for (int i = 19; i >= 0; --i) {
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  }
  return f[x][0];
}
void doit(int x, int lca, int c) {
  x = Find(x);
  while (dep[x] > dep[lca]) {
    if (res[x] != -1) ans += c, res[x] = c;
    fa[x] = f[x][0];
    x = Find(x);
  }
}
int main() {
  scanf("%d%d%d", &n, &m1, &m2);
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m1; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (Find(x) == Find(y)) continue;
    Add_Edge(x, y, 0);
    Add_Edge(y, x, 0);
    fa[Find(x)] = Find(y);
  }
  for (int i = 1; i <= m2; ++i) {
    scanf("%d%d%d", &x[i], &y[i], &c[i]);
    if (Find(x[i]) != Find(y[i])) {
      Add_Edge(x[i], y[i], -1);
      Add_Edge(y[i], x[i], -1);
      fa[Find(x[i])] = Find(y[i]);
    } else
      t[i] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    if (Find(i) == i) dep[i] = 1, dfs(i);
  }
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m2; ++i) {
    if (!t[i]) continue;
    int lca = LCA(x[i], y[i]);
    doit(x[i], lca, c[i]);
    doit(y[i], lca, c[i]);
  }
  for (int i = 1; i <= n; ++i) {
    if (f[i][0] == 0)
      continue;
    else if (!res[i])
      return 0 * puts("-1");
  }
  cout << ans << endl;
  return 0;
}
