#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int N = 5e5 + 100;
const long long mod = 998244353;
struct E {
  int x, y, val;
  E(){};
  E(int x, int y, int val) : x(x), y(y), val(val){};
} edge[N];
int tot_e = 0;
int n, m;
multiset<long long> tup;
set<pair<int, int> > hav[N];
set<pair<int, int> > may;
int deg[N];
map<int, int> idx[N];
void upd_tup(int x, bool op) {
  set<pair<int, int> >::iterator ii;
  long long res = 0;
  int num = 1;
  for (ii = hav[x].begin(); ii != hav[x].end() && num <= 3; ii++, num++) {
    int val = (*ii).first;
    int id = (*ii).second;
    res += (*ii).first;
    if (op == 0) {
      deg[id]--;
      if (deg[id] == 1) {
        may.erase(pair<int, int>(val, id));
      }
    } else {
      deg[id]++;
      if (deg[id] == 2) {
        may.insert(pair<int, int>(val, id));
      }
    }
  }
  if (num == 4) {
    if (op == 0)
      tup.erase(tup.find(res));
    else
      tup.insert(res);
  }
}
void add_edge(int x, int y, int val) {
  int id = ++tot_e;
  deg[tot_e] = 0;
  edge[tot_e] = E(x, y, val);
  idx[x][y] = idx[y][x] = id;
  upd_tup(x, 0);
  upd_tup(y, 0);
  hav[x].insert(pair<int, int>(val, id));
  hav[y].insert(pair<int, int>(val, id));
  upd_tup(x, 1);
  upd_tup(y, 1);
}
void del_edge(int x, int y) {
  int id = idx[x][y];
  int val = edge[id].val;
  idx[x][y] = idx[y][x] = 0;
  upd_tup(x, 0);
  upd_tup(y, 0);
  hav[x].erase(pair<int, int>(val, id));
  hav[y].erase(pair<int, int>(val, id));
  upd_tup(x, 1);
  upd_tup(y, 1);
}
void ask() {
  vector<int> buk;
  buk.clear();
  long long ans = 1e14;
  if (!tup.empty()) ans = min(ans, *tup.begin());
  set<pair<int, int> >::iterator ii;
  int num = 1;
  for (ii = may.begin(); ii != may.end() && num <= 6; ii++, num++) {
    buk.push_back((*ii).second);
  }
  for (int i = 0; i < buk.size(); i++) {
    for (int j = i + 1; j < buk.size(); j++) {
      int x = buk[i], y = buk[j];
      if (edge[x].x != edge[y].x && edge[x].y != edge[y].y &&
          edge[x].x != edge[y].y && edge[x].y != edge[y].x)
        ans = min(ans, (long long)edge[x].val + edge[y].val);
    }
  }
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    if (x > y) swap(x, y);
    add_edge(x, y, val);
  }
  ask();
  int qq;
  cin >> qq;
  while (qq--) {
    int op;
    cin >> op;
    if (op == 0) {
      int x, y;
      cin >> x >> y;
      if (x > y) swap(x, y);
      del_edge(x, y);
      ask();
    } else {
      int x, y, val;
      cin >> x >> y >> val;
      if (x > y) swap(x, y);
      add_edge(x, y, val);
      ask();
    }
  }
}
