#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, mod = 1e9 + 7;
const long long inf = 1e18;
int n, m, q;
struct Node {
  int p, size;
  pair<int, pair<int, int>> v;
  Node *L, *R;
  Node(pair<int, pair<int, int>> v, int p)
      : p(p), size(1), v(v), L(nullptr), R(nullptr) {}
  Node(Node* h) { *this = *h; }
  Node* update() {
    size = 1;
    if (L) size += L->size;
    if (R) size += R->size;
    return this;
  }
  int Lsize() { return L ? L->size : 0; }
};
pair<int, pair<int, int>> mus(pair<int, pair<int, int>> v) {
  return make_pair(v.first, make_pair(v.second.first, v.second.second - 1));
}
mt19937 rnd;
struct Treap {
  Node *root, *L, *M, *R;
  Treap(int seed = 0) : root(nullptr) { rnd.seed(seed); }
  virtual void split(Node* p, pair<int, pair<int, int>> v, Node*& L, Node*& R) {
    if (not p) return L = R = nullptr, void();
    if (p->v <= v)
      L = p, split(p->R, v, p->R, R);
    else
      R = p, split(p->L, v, L, p->L);
    p->update();
  }
  virtual Node* merge(Node* L, Node* R) {
    if (not L) return R;
    if (not R) return L;
    if (L->p < R->p) return L->R = merge(L->R, R), L->update();
    return R->L = merge(L, R->L), R->update();
  }
  Node* insert(Node* p, pair<int, pair<int, int>> v) {
    split(p, v, L, R);
    return merge(merge(L, new Node(v, rand())), R);
  }
  void insert(pair<int, pair<int, int>> v) { root = insert(root, v); }
  Node* remove(Node* p, pair<int, pair<int, int>> v) {
    split(p, v, L, R);
    split(L, mus(v), L, M);
    return merge(merge(L, M ? merge(M->L, M->R) : M), R);
  }
  void remove(pair<int, pair<int, int>> v) { root = remove(root, v); }
  int rank(Node* p, pair<int, pair<int, int>> v) {
    if (not p) return 1;
    if (p->v >= v) return rank(p->L, v);
    return 1 + p->Lsize() + rank(p->R, v);
  }
  int rank(pair<int, pair<int, int>> v) { return rank(root, v); }
  Node* kth(Node* p, int k) {
    if (not p or k > p->size) return nullptr;
    if (k <= p->Lsize()) return kth(p->L, k);
    if (k == p->Lsize() + 1) return p;
    return kth(p->R, k - p->Lsize() - 1);
  }
  Node* kth(int k) { return kth(root, k); }
  Node* pedecessor(Node* p, pair<int, pair<int, int>> v) {
    if (not p) return p;
    if (p->v >= v) return pedecessor(p->L, v);
    R = pedecessor(p->R, v);
    return R ? R : p;
  }
  Node* pedecessor(pair<int, pair<int, int>> v) { return pedecessor(root, v); }
  Node* successor(Node* p, pair<int, pair<int, int>> v) {
    if (not p) return p;
    if (p->v <= v) return successor(p->R, v);
    L = successor(p->L, v);
    return L ? L : p;
  }
  Node* successor(pair<int, pair<int, int>> v) { return successor(root, v); }
  int size() { return root->size; }
} T[N], ALL;
map<pair<int, int>, int> dic;
long long sol() {
  long long ans = inf;
  pair<int, pair<int, int>> mi = ALL.kth(1)->v;
  int u = mi.second.first, v = mi.second.second;
  if (T[u].size() >= 3) {
    ans = min(ans, (long long)T[u].kth(1)->v.first + T[u].kth(2)->v.first +
                       T[u].kth(3)->v.first);
  }
  if (T[v].size() >= 3) {
    ans = min(ans, (long long)T[v].kth(1)->v.first + T[v].kth(2)->v.first +
                       T[v].kth(3)->v.first);
  }
  if (T[u].size() >= 2 && T[v].size() >= 2) {
    pair<int, pair<int, int>> fw = T[u].kth(2)->v, fx = T[v].kth(2)->v;
    if (fw.second.second == fx.second.second) {
      if (T[u].size() >= 3) {
        ans = min(ans, (long long)T[u].kth(3)->v.first + fx.first);
      }
      if (T[v].size() >= 3) {
        ans = min(ans, (long long)fw.first + T[v].kth(3)->v.first);
      }
    } else
      ans = min(ans, (long long)fw.first + fx.first);
  }
  {
    int l = 0, r = ALL.size() + 1;
    while (l + 1 < r) {
      int mid = (l + r) / 2;
      pair<int, pair<int, int>> ky =
          make_pair(ALL.kth(mid)->v.first, make_pair(mod, mod));
      int cnt =
          (ALL.rank(ky) - 1) - (T[u].rank(ky) - 1) - (T[v].rank(ky) - 1) + 1;
      if (cnt > 0)
        r = mid;
      else
        l = mid;
    }
    if (r <= ALL.size())
      ans = min(ans, (long long)mi.first + ALL.kth(r)->v.first);
  }
  return ans;
}
void add(pair<int, pair<int, int>> v) {
  ALL.insert(v);
  dic[v.second] = v.first;
  T[v.second.first].insert(v);
  swap(v.second.first, v.second.second);
  T[v.second.first].insert(v);
}
void del(pair<int, pair<int, int>> v) {
  ALL.remove(v);
  dic.erase(v.second);
  T[v.second.first].remove(v);
  swap(v.second.first, v.second.second);
  T[v.second.first].remove(v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    pair<int, pair<int, int>> v;
    scanf("%d%d%d", &v.second.first, &v.second.second, &v.first);
    if (v.second.first > v.second.second) swap(v.second.first, v.second.second);
    add(v);
  }
  scanf("%d", &q);
  printf("%lld\n", sol());
  for (int i = 1; i <= q; ++i) {
    int opt;
    pair<int, pair<int, int>> v;
    scanf("%d", &opt);
    if (opt == 0) {
      scanf("%d%d", &v.second.first, &v.second.second);
      if (v.second.first > v.second.second)
        swap(v.second.first, v.second.second);
      v.first = dic[v.second];
      del(v);
    } else {
      scanf("%d%d%d", &v.second.first, &v.second.second, &v.first);
      if (v.second.first > v.second.second)
        swap(v.second.first, v.second.second);
      add(v);
    }
    printf("%lld\n", sol());
  }
  return 0;
}
