description: You are given an undirected weighted graph, consisting of n vertices and m edges.
  
  Some queries happen with this graph:
  
    * Delete an existing edge from the graph. 
    * Add a non-existing edge to the graph. 
  
  
  
  At the beginning and after each query, you should find four different vertices a, b, c, d such that there exists a path between a and b, there exists a path between c and d, and the sum of lengths of two shortest paths from a to b and from c to d is minimal. The answer to the query is the sum of the lengths of these two shortest paths. The length of the path is equal to the sum of weights of edges in this path.
  
  Input
  
  The first line contains two integers n and m (4 ≤ n, m ≤ 10^5) — the number of vertices and edges in the graph respectively.
  
  Each of the next m lines contain three integers v, u, w (1 ≤ v, u ≤ n, v ≠ u, 1 ≤ w ≤ 10^9) — this triple means that there is an edge between vertices v and u with weight w.
  
  The next line contains a single integer q (0 ≤ q ≤ 10^5) — the number of queries.
  
  The next q lines contain the queries of two types:
  
    * 0 v u — this query means deleting an edge between v and u (1 ≤ v, u ≤ n, v ≠ u). It is guaranteed that such edge exists in the graph. 
    * 1 v u w — this query means adding an edge between vertices v and u with weight w (1 ≤ v, u ≤ n, v ≠ u, 1 ≤ w ≤ 10^9). It is guaranteed that there was no such edge in the graph. 
  
  
  
  It is guaranteed that the initial graph does not contain multiple edges.
  
  At the beginning and after each query, the graph doesn't need to be connected.
  
  It is guaranteed that at each moment the number of edges will be at least 4. It can be proven, that at each moment there exist some four vertices a, b, c, d such that there exists a path between vertices a and b, and there exists a path between vertices c and d.
  
  Output
  
  Print q + 1 integers — the minimal sum of lengths of shortest paths between chosen pairs of vertices before the queries and after each of them.
  
  Example
  
  Input
  
  
  6 6
  1 3 6
  4 3 1
  1 4 1
  2 6 4
  2 4 2
  5 4 3
  4
  1 2 5 2
  0 1 4
  0 3 4
  1 6 1 3
  
  
  Output
  
  
  4
  3
  3
  7
  5
  
  Note
  
  Before the queries you can choose vertices (a, b) = (3, 2) and (c, d) = (1, 4). The sum of lengths of two shortest paths is 3 + 1 = 4.
  
  After the first query you can choose vertices (a, b) = (2, 5) and (c, d) = (1, 4). The sum of lengths of two shortest paths is 2 + 1 = 3.
  
  After the second query you can choose vertices (a, b) = (3, 4) and (c, d) = (2, 5). The sum of lengths of two shortest paths is 1 + 2 = 3.
  
  After the third query, you can choose vertices (a, b) = (2, 6) and (c, d) = (4, 5). The sum of lengths of two shortest paths is 4 + 3 = 7.
  
  After the last query you can choose vertices (a, b) = (1, 6) and (c, d) = (2, 5). The sum of lengths of two shortest paths is 3 + 2 = 5.
solution: #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<edge> {
      vector<edge> tmp;
      auto it = adj[u].begin();
      for (int i = 0; i < 3 && it != adj[u].end(); ++i) {
        tmp.push_back(vec[*it]);
        ++it;
      }
      return tmp;
    };
    auto get_sum = [&](int u) {
      auto tmp = get_adj(u);
      long long sum = 0;
      for (auto& c : tmp) sum += c.w;
      return sum;
    };
    auto add = [&](int u, int e) {
      auto it = adj[u].begin();
      for (int i = 0; i < 3 && it != adj[u].end(); ++i) {
        if (cnt[*it]-- == 1) alls.erase(*it);
        ++it;
      }
      if (adj[u].size() >= 3) s.erase(s.find(get_sum(u)));
      adj[u].insert(e);
      if (adj[u].size() >= 3) s.insert(get_sum(u));
      it = adj[u].begin();
      for (int i = 0; i < 3 && it != adj[u].end(); ++i) {
        if (cnt[*it]++ == 1) alls.insert(*it);
        ++it;
      }
    };
    auto del = [&](int u, int e) {
      auto it = adj[u].begin();
      for (int i = 0; i < 3 && it != adj[u].end(); ++i) {
        if (cnt[*it]-- == 1) alls.erase(*it);
        ++it;
      }
      if (adj[u].size() >= 3) s.erase(s.find(get_sum(u)));
      adj[u].erase(e);
      if (adj[u].size() >= 3) s.insert(get_sum(u));
      it = adj[u].begin();
      for (int i = 0; i < 3 && it != adj[u].end(); ++i) {
        if (cnt[*it]++ == 1) alls.insert(*it);
        ++it;
      }
    };
    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;
      add(vec[i].u, i);
      add(vec[i].v, i);
      mmap[make_pair(vec[i].u, vec[i].v)] = mmap[make_pair(vec[i].v, vec[i].u)] =
          i;
    }
    auto calres = [&]() {
      long long res = (s.size() ? *s.begin() : 3e9);
      int id = *alls.begin();
      {
        auto it = alls.begin();
        for (int i = 0;; ++i) {
          ++it;
          if (it == alls.end()) break;
          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& [u, v, w] : a) {
        for (auto& [u1, v1, w1] : b) {
          if (u != u1 && v != v1 && u != v1 && v != u1)
            res = min(res, 0ll + w + w1);
        }
      }
      return res;
    };
    cout << calres() << '\n';
    int q;
    cin >> q;
    while (q--) {
      int type;
      cin >> type;
      if (type == 1) {
        int u, v, c;
        cin >> u >> v >> c;
        --u;
        --v;
        if (u > v) swap(u, v);
        int i = vec.size();
        vec.push_back({u, v, c});
        add(u, i);
        add(v, i);
        mmap[make_pair(u, v)] = mmap[make_pair(v, u)] = i;
      } else {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        del(u, mmap[make_pair(u, v)]);
        del(v, mmap[make_pair(u, v)]);
        assert(alls.find(mmap[make_pair(u, v)]) == alls.end());
      }
      cout << calres() << '\n';
    }
  }
  