#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
namespace ywy {
inline int get() {
  int n = 0;
  char c;
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9') break;
    if (c == '-') goto s;
  }
  n = c - '0';
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 + c - '0';
    else
      return (n);
  }
s:
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 - c + '0';
    else
      return (n);
  }
}
typedef struct _bb {
  int s;
  int t;
  int l;
  unsigned char istree;
  friend bool operator<(const _bb &a, const _bb &b) { return (a.l < b.l); }
} xiabb;
vector<xiabb> vec;
int ints[500001], size[500001], zhongson[500001], deep[500001], gdfn = 1;
int find(int n) {
  if (ints[n] == n) return (n);
  return (ints[n] = find(ints[n]));
}
typedef struct _b {
  int dest;
  int nxt;
} bian;
bian memchi[2000001];
int heads[500001], dfn[500001], top[500001], fa[500001];
int gn = 1;
inline void add(int s, int t) {
  memchi[gn].dest = t;
  memchi[gn].nxt = heads[s];
  heads[s] = gn;
  gn++;
}
int tag[2000001];
long long ans = 0;
void tui(int l, int r, int tree) {
  if (l == r) {
    if (l != 1) {
      if (tag[tree] == 0x7f7f7f7f) {
        cout << -1 << endl;
        exit(0);
      } else {
        ans += tag[tree];
      }
    }
    return;
  }
  int mid = (l + r) >> 1;
  tag[(tree << 1)] = min(tag[(tree << 1)], tag[tree]);
  tag[((tree << 1) | 1)] = min(tag[((tree << 1) | 1)], tag[tree]);
  tui(l, mid, (tree << 1));
  tui(mid + 1, r, ((tree << 1) | 1));
}
void set(int rl, int rr, int l, int r, int tree, int x) {
  if (rl > rr) return;
  if (rl == l && rr == r) {
    tag[tree] = min(tag[tree], x);
    return;
  }
  int mid = (l + r) >> 1;
  if (rl > mid)
    set(rl, rr, mid + 1, r, ((tree << 1) | 1), x);
  else {
    if (rr <= mid)
      set(rl, rr, l, mid, (tree << 1), x);
    else {
      set(rl, mid, l, mid, (tree << 1), x);
      set(mid + 1, rr, mid + 1, r, ((tree << 1) | 1), x);
    }
  }
}
void dfs(int pt, int baba) {
  size[pt] = 1;
  int mx = 0, best = 0;
  for (register int i = heads[pt]; i; i = memchi[i].nxt) {
    if (memchi[i].dest == baba) continue;
    deep[memchi[i].dest] = deep[pt] + 1;
    fa[memchi[i].dest] = pt;
    dfs(memchi[i].dest, pt);
    size[pt] += size[memchi[i].dest];
    if (size[memchi[i].dest] > mx) {
      mx = size[memchi[i].dest];
      best = memchi[i].dest;
    }
  }
  top[pt] = pt;
  zhongson[pt] = best;
}
void efs(int pt, int baba) {
  dfn[pt] = gdfn;
  gdfn++;
  if (zhongson[pt]) top[zhongson[pt]] = top[pt], efs(zhongson[pt], pt);
  for (register int i = heads[pt]; i; i = memchi[i].nxt) {
    if (memchi[i].dest == baba || memchi[i].dest == zhongson[pt]) continue;
    efs(memchi[i].dest, pt);
  }
}
void ywymain() {
  memset(tag, 0x7f, sizeof(tag));
  int n = get(), k = get(), m = get();
  for (register int i = 1; i <= k; i++) {
    xiabb cjr;
    cjr.s = get();
    cjr.t = get();
    cjr.l = -1234567890;
    cjr.istree = 0;
    vec.push_back(cjr);
  }
  for (register int i = 1; i <= n; i++) ints[i] = i;
  for (register int i = 1; i <= m; i++) {
    xiabb cjr;
    cjr.s = get();
    cjr.t = get();
    cjr.l = get();
    cjr.istree = 0;
    vec.push_back(cjr);
    int aa = find(cjr.s), ab = find(cjr.t);
    ints[aa] = ab;
  }
  sort(vec.begin(), vec.end());
  for (register int i = 1; i <= n; i++) ints[i] = i;
  for (register int i = 0; i < vec.size(); i++) {
    int aa = find(vec[i].s), ab = find(vec[i].t);
    if (aa == ab) continue;
    vec[i].istree = 1;
    ints[aa] = ab;
  }
  for (register int i = 1; i <= n; i++) ints[i] = i;
  for (register int i = 0; i < vec.size(); i++) {
    if (vec[i].l != -1234567890 && vec[i].istree) {
      int aa = find(vec[i].s), ab = find(vec[i].t);
      ints[aa] = ab;
    }
  }
  for (register int i = 0; i < vec.size(); i++) {
    if (vec[i].l == -1234567890)
      add(find(vec[i].s), find(vec[i].t)), add(find(vec[i].t), find(vec[i].s));
  }
  dfs(find(1), 0);
  efs(find(1), 0);
  for (register int i = 0; i < vec.size(); i++) {
    if (vec[i].l == -1234567890) continue;
    int s = find(vec[i].s), t = find(vec[i].t);
    if (s == t) continue;
    while (top[s] != top[t]) {
      if (deep[top[s]] > deep[top[t]]) swap(s, t);
      set(dfn[top[t]], dfn[t], 1, k + 1, 1, vec[i].l);
      t = fa[top[t]];
    }
    if (deep[s] > deep[t]) swap(s, t);
    set(dfn[s] + 1, dfn[t], 1, k + 1, 1, vec[i].l);
  }
  tui(1, k + 1, 1);
  cout << ans << endl;
}
}  // namespace ywy
int main() {
  ywy::ywymain();
  return (0);
}
