#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxl = 20;
int n;
vector<pair<int, int>> g[maxn];
bool marked[maxn];
int h[maxn];
int par[maxn][maxl];
int stv[maxn];
int vts[maxn];
long long int dis[maxn][maxl];
int t = 0;
void dfs(int v) {
  marked[v] = true;
  for (int i = 1; i < maxl; i++) {
    if (par[v][i - 1] != -1) {
      par[v][i] = par[par[v][i - 1]][i - 1];
      dis[v][i] = dis[v][i - 1] + dis[par[v][i - 1]][i - 1];
    } else {
      par[v][i] = -1;
    }
  }
  t++;
  vts[v] = t;
  stv[t] = v;
  for (pair<int, int> u : g[v]) {
    if (!marked[u.first]) {
      h[u.first] = h[v] + 1;
      par[u.first][0] = v;
      dis[u.first][0] = u.second;
      dfs(u.first);
    }
  }
}
int findPar(int v, int k) {
  int hp = h[v] - k;
  for (int i = maxl - 1; i >= 0; i--) {
    if (par[v][i] != -1 && h[par[v][i]] >= hp) v = par[v][i];
  }
  return v;
}
int lca(int a, int b) {
  if (h[a] < h[b]) swap(a, b);
  a = findPar(a, h[a] - h[b]);
  if (a == b) return a;
  for (int i = maxl - 1; i >= 0; i--) {
    if (par[a][i] != par[b][i]) {
      a = par[a][i];
      b = par[b][i];
    }
  }
  return par[a][0];
}
long long int distance(int a, int b) {
  int parent = lca(a, b);
  long long int disa = 0, disb = 0;
  for (int i = maxl - 1; i >= 0; i--) {
    if (par[a][i] != -1 && h[par[a][i]] >= h[parent]) {
      disa += dis[a][i];
      a = par[a][i];
    }
    if (par[b][i] != -1 && h[par[b][i]] >= h[parent]) {
      disb += dis[b][i];
      b = par[b][i];
    }
  }
  return disa + disb;
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int v, u, c;
    cin >> v >> u >> c;
    v--;
    u--;
    g[v].push_back(make_pair(u, c));
    g[u].push_back(make_pair(v, c));
  }
  h[0] = 0;
  par[0][0] = -1;
  dfs(0);
  int q;
  cin >> q;
  long long int totalLength = 0;
  set<int> vill;
  while (q--) {
    char a;
    cin >> a;
    if (a == '+') {
      int x;
      cin >> x;
      x--;
      vill.insert(vts[x]);
      if (vill.size() == 1) {
        continue;
      }
      auto pos = vill.find(vts[x]);
      int v1, v2;
      if (pos == vill.begin()) {
        v1 = stv[*++pos];
        v2 = stv[*--vill.end()];
      } else if (pos == --vill.end()) {
        v1 = stv[*--pos];
        v2 = stv[*vill.begin()];
      } else {
        v1 = stv[*++pos];
        pos--;
        v2 = stv[*--pos];
      }
      totalLength += distance(x, v1) + distance(x, v2) - distance(v1, v2);
    } else if (a == '-') {
      int x;
      cin >> x;
      x--;
      auto pos = vill.find(vts[x]);
      int v1, v2;
      if (vill.size() == 1) {
        vill.erase(vts[x]);
        continue;
      }
      if (pos == vill.begin()) {
        v1 = stv[*++pos];
        v2 = stv[*--vill.end()];
      } else if (pos == --vill.end()) {
        v1 = stv[*--pos];
        v2 = stv[*vill.begin()];
      } else {
        v1 = stv[*++pos];
        pos--;
        v2 = stv[*--pos];
      }
      totalLength += distance(v1, v2) - distance(x, v1) - distance(x, v2);
      vill.erase(vts[x]);
    } else {
      cout << totalLength / 2 << endl;
    }
  }
  return 0;
}
