#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const double eps = 1e-6;
struct inout {
  static const long long ibufl = 1 << 25;
  char in_buf[ibufl + 5], out_buf[ibufl + 5], *inf, *ouf;
  void init() {
    fread(in_buf, 1, ibufl, stdin);
    inf = in_buf;
    ouf = out_buf;
  }
  inout& operator>>(long long& a) {
    long long fh = 1;
    while (!(isdigit(*inf) || *inf == '-')) ++inf;
    if (*inf == '-') fh = -1, ++inf;
    a = 0;
    while (isdigit(*inf)) {
      a = a * 10 + *inf - '0';
      ++inf;
    }
    a *= fh;
    return *this;
  }
  inout& operator>>(char& a) {
    while (*inf == ' ' || *inf == '\n') ++inf;
    a = *inf;
    ++inf;
    return *this;
  }
  inout& operator>>(char* a) {
    while (*inf == ' ' || *inf == '\n') ++inf;
    while (!(*inf == ' ' || *inf == '\n')) {
      *a = *inf;
      ++inf;
      ++a;
    }
    *a = '\0';
    return *this;
  }
  inout& operator>>(double& a) {
    long long fh = 1;
    double s;
    while (!(isdigit(*inf) || *inf == '-')) ++inf;
    if (*inf == '-') fh = -1, ++inf;
    a = 0;
    while (isdigit(*inf)) {
      a = a * 10 + *inf - '0';
      ++inf;
    }
    if (*inf == '.') {
      s = 0.1;
      ++inf;
      while (isdigit(*inf)) {
        a += s * (*inf - '0');
        ++inf;
        s *= 0.1;
      }
    }
    a *= fh;
    return *this;
  }
  void writeint(long long x) {
    if (x / 10) writeint(x / 10);
    *ouf = x % 10 + '0';
    ++ouf;
  }
  inout& operator<<(long long a) {
    if (a < 0) {
      *ouf = '-';
      ++ouf;
      a = -a;
    }
    writeint(a);
    return *this;
  }
  static const long long sz = 2;
  inout& operator<<(char a) {
    *ouf = a;
    ++ouf;
    return *this;
  }
  inout& operator<<(char* a) {
    while (*a) {
      *ouf = *a;
      ++ouf;
      ++a;
    }
    return *this;
  }
  inout& operator<<(double a) {
    if (a < -eps) {
      *ouf = '-';
      ++ouf;
      a = -a;
    }
    writeint((long long)a);
    a -= (long long)a;
    *ouf = '.';
    ++ouf;
    for (long long i = 1; i <= sz; i++) {
      a = a * 10;
      *ouf = (long long)a + '0';
      ++ouf;
      a -= (long long)a;
    }
    return *this;
  }
  void out() { fwrite(out_buf, 1, ouf - out_buf, stdout); }
};
inout io;
multiset<pair<long long, long long> > e[100005], p[100005];
long long n;
map<pair<long long, long long>, long long> mp;
set<pair<long long, long long> > s;
long long get(long long x) {
  long long re = 0;
  for (auto u : p[x]) re += u.first;
  return re;
}
pair<long long, long long> C(pair<long long, long long> x) {
  if (x.first > x.second) {
    swap(x.first, x.second);
  }
  return x;
}
set<pair<long long, pair<long long, long long> > > edge;
void solve(long long x) {
  if (e[x].size() >= 3) {
    auto u = e[x].begin();
    p[x].clear();
    for (long long _ = 1; _ <= 3; _++) {
      p[x].emplace(*u);
      u++;
    }
    s.emplace(make_pair(get(x), x));
  } else
    p[x] = e[x];
}
void add(long long x, long long y, long long w) {
  if (x > y) swap(x, y);
  for (auto u : p[x]) edge.erase(make_pair(u.first, C(make_pair(x, u.second))));
  for (auto u : p[y]) edge.erase(make_pair(u.first, C(make_pair(u.second, y))));
  mp[make_pair(x, y)] = w;
  if (p[x].size() == 3) s.erase(make_pair(get(x), x));
  if (p[y].size() == 3) s.erase(make_pair(get(y), y));
  e[x].emplace(make_pair(w, y));
  e[y].emplace(make_pair(w, x));
  solve(x);
  solve(y);
  for (auto u : p[x])
    if (p[u.second].count(make_pair(u.first, x)))
      edge.emplace(make_pair(u.first, C(make_pair(x, u.second))));
  for (auto u : p[y])
    if (p[u.second].count(make_pair(u.first, y)))
      edge.emplace(make_pair(u.first, C(make_pair(y, u.second))));
}
void erase(long long x, long long y) {
  if (x > y) swap(x, y);
  long long w = mp[make_pair(x, y)];
  for (auto u : p[x]) edge.erase(make_pair(u.first, C(make_pair(x, u.second))));
  for (auto u : p[y]) edge.erase(make_pair(u.first, C(make_pair(u.second, y))));
  if (p[x].size() == 3) s.erase(make_pair(get(x), x));
  if (p[y].size() == 3) s.erase(make_pair(get(y), y));
  e[x].erase(e[x].lower_bound(make_pair(w, y)));
  e[y].erase(e[y].lower_bound(make_pair(w, x)));
  solve(x);
  solve(y);
  for (auto u : p[x])
    if (p[u.second].count(make_pair(u.first, x)))
      edge.emplace(make_pair(u.first, C(make_pair(x, u.second))));
  for (auto u : p[y])
    if (p[u.second].count(make_pair(u.first, y)))
      edge.emplace(make_pair(u.first, C(make_pair(y, u.second))));
}
signed main() {
  io.init();
  long long n, m;
  io >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long x, y, w;
    io >> x >> y >> w;
    add(x, y, w);
  }
  long long q;
  io >> q;
  for (long long i = 0; i <= q; i++) {
    if (i) {
      long long tap;
      io >> tap;
      if (tap) {
        long long x, y, w;
        io >> x >> y >> w;
        add(x, y, w);
      } else {
        long long x, y;
        io >> x >> y;
        erase(x, y);
      }
    }
    long long ans = 1e18;
    if (!s.empty()) ans = (*s.begin()).first;
    auto u = edge.begin();
    long long n1 = (*u).second.first, n2 = (*u).second.second, val = (*u).first;
    for (auto u1 : p[n1]) {
      for (auto u2 : p[n2]) {
        if (u1.second != n2 && u2.second != n1 && u1.second != u2.second) {
          ans = min(ans, u1.first + u2.first);
        }
      }
    }
    u++;
    long long siz = 2;
    while (siz <= edge.size()) {
      if (siz > 10) break;
      if ((*u).second.first != n1 && (*u).second.second != n2 &&
          (*u).second.first != n2 && (*u).second.second != n1) {
        ans = min(ans, val + (*u).first);
        break;
      }
      u++;
      siz++;
    }
    io << ans << '\n';
  }
  io.out();
  return 0;
}
