#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
struct SegmentTree {
  struct Node {
    bool hasCarry = 0;
    int b = 0, e = 0;
    U carry = U();
    T val = T();
    Node() {}
    void join(const Node &l, const Node &r) {
      val = l.val + r.val;
      b = l.b;
      e = r.e;
    }
    void update(const U &u) {
      carry += u;
      u(val);
      hasCarry = 1;
    }
    void pushDown(Node &l, Node &r) {
      if (!hasCarry) return;
      l.update(carry.trim(0, r.e - r.b + 1));
      r.update(carry.trim(l.e - l.b + 1, 0));
      carry = U();
      hasCarry = 0;
    }
  };
  vector<Node> V;
  int N;
  SegmentTree(int N) : V(2 * N), N(N) {}
  template <class I>
  void create(const vector<I> &VEC, int b = 0, int e = -1) {
    if (e == -1) e = N - 1;
    int n = (((b) + (e)) | ((b) != (e)));
    if (b == e) {
      V[n].val = T(VEC[b]), V[n].b = b, V[n].e = e;
    } else {
      int m = (b + e) / 2;
      create(VEC, b, m);
      create(VEC, m + 1, e);
      V[n].join(V[(((b) + (m)) | ((b) != (m)))],
                V[(((m + 1) + (e)) | ((m + 1) != (e)))]);
    }
  }
  T query(int i, int j, int b = 0, int e = -1) {
    if (e == -1) e = N - 1;
    int n = (((b) + (e)) | ((b) != (e)));
    if (i <= b && e <= j) {
      return V[n].val;
    } else {
      int m = (b + e) / 2;
      V[n].pushDown(V[(((b) + (m)) | ((b) != (m)))],
                    V[(((m + 1) + (e)) | ((m + 1) != (e)))]);
      if (i > m) return query(i, j, m + 1, e);
      if (j <= m) return query(i, j, b, m);
      return query(i, j, b, m) + query(i, j, m + 1, e);
    }
  }
  void update(int i, int j, const U &v, int b = 0, int e = -1) {
    if (e == -1) e = N - 1;
    int n = (((b) + (e)) | ((b) != (e)));
    if (i <= b && e <= j) {
      V[n].update(v.trim(max(b - i, 0), max(j - e, 0)));
    } else if (i > e || j < b)
      return;
    else {
      int m = (b + e) / 2;
      int l = (((b) + (m)) | ((b) != (m))),
          r = (((m + 1) + (e)) | ((m + 1) != (e)));
      V[n].pushDown(V[l], V[r]);
      update(i, j, v, b, m);
      update(i, j, v, m + 1, e);
      V[n].join(V[l], V[r]);
    }
  }
};
struct Min {
  int mi;
  Min(int v = 2000000000) { mi = v; }
  Min operator+(const Min &a) const {
    Min r;
    r.mi = min(mi, a.mi);
    return r;
  }
};
struct Replace {
  int v;
  Replace(int v = 0) : v(v) {}
  Replace trim(int l, int r) const { return *this; }
  void operator+=(const Replace &a) { v = a.v; }
  void operator()(Min &a) const { a.mi = v; }
};
struct HLD {
  vector<int> P, LVL, SZ, PC;
  vector<vector<int>> adj;
  vector<vector<int>> groups;
  vector<int> groupId;
  vector<int> groupPos;
  vector<SegmentTree<Min, Replace>> ST;
  int N;
  HLD(vector<vector<int>> adj) {
    N = adj.size();
    P = PC = vector<int>(N, -1);
    LVL = SZ = groupId = groupPos = vector<int>(N, -1);
    this->adj = adj;
    for (int i = 0; i < N; ++i) {
      if (groupId[i] == -1) {
        if (SZ[i] == -1) initDfs(i);
        groups.push_back(vector<int>());
        for (int j = i; j != -1; j = PC[j]) {
          groups.back().push_back(j);
          groupId[j] = groups.size() - 1;
          groupPos[j] = groups.back().size() - 1;
        }
        ST.emplace_back(groups.back().size());
        ST.back().create(vector<int>(groups.back().size(), 2000000000));
      }
    }
  }
  void initDfs(int u, int level = 0) {
    LVL[u] = level;
    SZ[u] = 1;
    int maid = -1;
    for (int i = 0; i < adj[u].size(); ++i) {
      int v = adj[u][i];
      if (SZ[v] != -1) continue;
      P[v] = u;
      initDfs(v, level + 1);
      SZ[u] += SZ[v];
      if (maid == -1 || SZ[v] > SZ[maid]) {
        maid = v;
      }
    }
    PC[u] = maid;
  }
  int LCA(int u, int v) {
    while (u != v && u != -1 && v != -1) {
      if (LVL[v] < LVL[u]) swap(u, v);
      if (groupId[u] == groupId[v]) return u;
      bool vGoesUp = LVL[groups[groupId[u]][0]] < LVL[groups[groupId[v]][0]];
      (vGoesUp ? v : u) = P[groups[groupId[(vGoesUp ? v : u)]][0]];
    }
    return u == v ? v : -1;
  }
  void update(int u, int v, int val, int debug = 0) {
    while (u != v && u != -1 && v != -1) {
      if (groupId[u] == groupId[v]) {
        int pu = groupPos[u], pv = groupPos[v];
        if (debug) cout << "same: " << pu << " " << pv << endl;
        int g = groupId[u];
        if (LVL[u] < LVL[v]) {
          ST[g].update(pu + 1, pv, val);
        } else {
          ST[g].update(pv + 1, pu, val);
        }
        break;
      }
      if (LVL[groups[groupId[u]][0]] < LVL[groups[groupId[v]][0]]) {
        int g = groupId[v], p = groupPos[v];
        ST[g].update(0, p, val);
        if (debug) cout << v << "-->" << P[groups[g][0]] << endl;
        v = P[groups[g][0]];
      } else {
        int g = groupId[u], p = groupPos[u];
        ST[g].update(0, p, val);
        if (debug) cout << u << "-->" << P[groups[g][0]] << endl;
        u = P[groups[g][0]];
      }
    }
  }
  Min path(int u, int v, int debug = 0) {
    Min A, B;
    while (u != v && u != -1 && v != -1) {
      if (groupId[u] == groupId[v]) {
        int pu = groupPos[u], pv = groupPos[v];
        if (debug) cout << "same: " << pu << " " << pv << endl;
        int g = groupId[u];
        if (LVL[u] < LVL[v]) {
          B = ST[g].query(pu + 1, pv) + B;
        } else {
          A = ST[g].query(pv + 1, pu) + A;
        }
        break;
      }
      if (LVL[groups[groupId[u]][0]] < LVL[groups[groupId[v]][0]]) {
        int g = groupId[v], p = groupPos[v];
        B = ST[g].query(0, p) + B;
        if (debug) cout << v << "-->" << P[groups[g][0]] << endl;
        v = P[groups[g][0]];
      } else {
        int g = groupId[u], p = groupPos[u];
        A = ST[g].query(0, p) + A;
        if (debug) cout << u << "-->" << P[groups[g][0]] << endl;
        u = P[groups[g][0]];
      }
    }
    return A + B;
  }
};
vector<long long> A;
bool bf(int p, int u, int fv, vector<vector<int>> &adj,
        vector<vector<int>> &dis) {
  if (u == fv) return true;
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i];
    if (v == p) continue;
    if (bf(u, v, fv, adj, dis)) {
      A.push_back(dis[u][i]);
      return true;
    }
  }
  return false;
}
struct DisjointSet {
  vector<int> P;
  DisjointSet(int N) : P(N, -1) {}
  int find(int x) { return P[x] < 0 ? x : (P[x] = find(P[x])); }
  bool join(int x, int y) {
    if ((x = find(x)) == (y = find(y))) return false;
    if (P[y] < P[x]) swap(x, y);
    P[x] += P[y];
    P[y] = x;
    return true;
  }
};
struct Edge {
  int i, j, id, c;
  bool operator<(const Edge e) const { return c < e.c; }
};
int main() {
  int N, K, M;
  cin >> N >> K >> M;
  vector<Edge> O1(M), O2(K);
  for (int i = 0; i < K; ++i) {
    Edge &e = O2[i];
    scanf("%d%d", &e.i, &e.j);
    e.c = 0;
    e.i--;
    e.j--;
    e.id = i;
  }
  for (int i = 0; i < M; ++i) {
    Edge &e = O1[i];
    scanf("%d%d%d", &e.i, &e.j, &e.c);
    e.i--;
    e.j--;
    e.id = i;
  }
  sort(O1.begin(), O1.end());
  sort(O2.begin(), O2.end());
  DisjointSet DS(N);
  vector<int> U(M);
  for (Edge e : O2) {
    DS.join(e.i, e.j);
  }
  long long ans = 0;
  for (Edge e : O1) {
    if (DS.join(e.i, e.j)) {
      U[e.id] = 1;
    }
  }
  reverse(O1.begin(), O1.end());
  vector<vector<int>> adj(N);
  for (Edge e : O2) {
    DS.join(e.i, e.j);
    adj[e.i].push_back(e.j);
    adj[e.j].push_back(e.i);
  }
  set<pair<int, int>> S;
  for (Edge e : O1) {
    if (!U[e.id]) continue;
    adj[e.i].push_back(e.j);
    adj[e.j].push_back(e.i);
    S.insert(make_pair(min(e.i, e.j), max(e.i, e.j)));
  }
  HLD H(adj);
  for (Edge e : O1) {
    if (U[e.id]) continue;
    int lc = H.LCA(e.i, e.j);
    if (lc != -1) {
      H.update(e.i, e.j, e.c);
    }
  }
  for (int i = 0; i < N; ++i) {
    if (H.P[i] != -1) {
      if (S.count(make_pair(min(i, H.P[i]), max(i, H.P[i])))) continue;
      int t = H.path(i, H.P[i]).mi;
      if (t >= 2000000000) {
        cout << -1 << endl;
        return 0;
      }
      ans += t;
    }
  }
  cout << ans << endl;
}
