#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int logn = 20;
const int mod = 998244353;
struct edge {
  int u, v, w;
};
vector<edge> vec;
struct cmp {
  bool operator()(int a, int b) const {
    return vec[a].w < vec[b].w || (vec[a].w == vec[b].w && a < b);
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("A"
            ".INP",
            "r")) {
    freopen(
        "A"
        ".INP",
        "r", stdin);
    freopen(
        "A"
        ".OUT",
        "w", stdout);
  }
  int n, m;
  cin >> n >> m;
  vec.resize(m);
  multiset<long long> s;
  set<int, cmp> alls;
  vector<int> cnt(maxn * 2, 0);
  vector<set<int, cmp>> adj(n);
  auto get_adj = [&](int u) -> vector<int> {
    vector<int> tmp;
    auto it = adj[u].begin();
    for (int i = 0; i < 3 && it != adj[u].end(); ++i) {
      tmp.push_back(*it);
      ++it;
    }
    return tmp;
  };
  auto get_sum = [&](vector<int>& tmp) {
    long long sum = 0;
    for (auto& c : tmp) sum += vec[c].w;
    return sum;
  };
  auto change = [&](int u, int e, int mode = 0) {
    auto prev = get_adj(u);
    if (mode)
      adj[u].erase(e);
    else
      adj[u].insert(e);
    auto cur = get_adj(u);
    if (cur == prev) return;
    if (prev.size() == 3) s.erase(s.find(get_sum(prev)));
    if (cur.size() == 3) s.insert(get_sum(cur));
    for (auto& c : cur) {
      if (find(prev.begin(), prev.end(), c) == prev.end() && cnt[c]++ == 1) {
        alls.insert(c);
      }
    }
    for (auto& c : prev) {
      if (find(cur.begin(), cur.end(), c) == cur.end() && --cnt[c] == 1) {
        alls.erase(c);
      }
    }
  };
  map<pair<int, int>, int> mmap;
  for (int i = 0; i < m; ++i) {
    cin >> vec[i].u >> vec[i].v >> vec[i].w;
    if (vec[i].u > vec[i].v) swap(vec[i].u, vec[i].v);
    --vec[i].u;
    --vec[i].v;
    change(vec[i].u, i);
    change(vec[i].v, i);
    mmap[make_pair(vec[i].u, vec[i].v)] = i;
  }
  auto calres = [&]() {
    long long res = (s.size() ? *s.begin() : 3e9);
    int id = *alls.begin();
    {
      auto it = alls.begin();
      while (++it != alls.end()) {
        int j = *it;
        if (vec[id].u != vec[j].u && vec[id].u != vec[j].v &&
            vec[id].v != vec[j].u && vec[id].v != vec[j].v) {
          res = min(res, 0ll + vec[id].w + vec[j].w);
          break;
        }
      }
    }
    auto a = get_adj(vec[id].u), b = get_adj(vec[id].v);
    for (auto& i : a) {
      for (auto& j : b) {
        if (vec[i].u != vec[j].u && vec[i].u != vec[j].v &&
            vec[i].v != vec[j].u && vec[i].v != vec[j].v)
          res = min(res, 0ll + vec[i].w + vec[j].w);
      }
    }
    return res;
  };
  cout << calres() << '\n';
  int q;
  cin >> q;
  while (q--) {
    int type, u, v;
    cin >> type >> u >> v;
    --u;
    --v;
    if (u > v) swap(u, v);
    if (type == 1) {
      int c;
      cin >> c;
      int i = vec.size();
      vec.push_back({u, v, c});
      change(u, i);
      change(v, i);
      mmap[make_pair(u, v)] = i;
    } else {
      int e = mmap[make_pair(u, v)];
      change(u, e, 1);
      change(v, e, 1);
    }
    cout << calres() << '\n';
  }
}
