#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long llread() {
  char ch = getchar();
  long long x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct Edge {
  int u, v, w, next;
} G[400010];
int head[400010], tot;
int h[400010], ft[400010][20], dfs_clock;
long long dis[400010][20];
struct Node {
  int x, y;
  inline bool operator<(const Node& rhs) const { return y < rhs.y; }
} a[400010];
set<Node> now;
set<Node>::iterator it;
bool vis[400010];
inline void add(int u, int v, int w) {
  G[++tot] = (Edge){u, v, w, head[u]};
  head[u] = tot;
}
inline void dfs(int x, int fa, int height) {
  h[x] = height;
  ft[x][0] = fa;
  a[x].x = x;
  a[x].y = ++dfs_clock;
  for (int i = head[x]; i != -1; i = G[i].next)
    if (G[i].v != fa) {
      dis[G[i].v][0] = G[i].w;
      dfs(G[i].v, x, height + 1);
    }
}
inline void init(int n) {
  for (int i = (1); i <= (18); i++)
    for (int j = (1); j <= (n); j++) {
      ft[j][i] = ft[ft[j][i - 1]][i - 1];
      dis[j][i] = dis[j][i - 1] + dis[ft[j][i - 1]][i - 1];
    }
}
inline long long getdis(int x, int y) {
  long long ret = 0;
  if (h[x] < h[y]) swap(x, y);
  int t = h[x] - h[y];
  for (int i = (18); i >= (0); i--) {
    if (t & (1 << i)) ret += dis[x][i], x = ft[x][i];
  }
  if (x == y) return ret;
  for (int i = (18); i >= (0); i--) {
    if (ft[x][i] != ft[y][i])
      ret += (dis[x][i] + dis[y][i]), x = ft[x][i], y = ft[y][i];
  }
  return ret + dis[x][0] + dis[y][0];
}
int main() {
  memset(head, -1, sizeof(head));
  int n = read();
  for (int i = (1); i < (n); i++) {
    int u = read(), v = read(), w = read();
    add(u, v, w);
    add(v, u, w);
  }
  dfs(1, 0, 1);
  init(n);
  int m = read();
  string st;
  long long res = 0;
  for (int i = (1); i <= (m); i++) {
    cin >> st;
    if (st[0] == '+') {
      int x = read();
      if (vis[x]) continue;
      now.insert(a[x]);
      it = now.find(a[x]);
      int qthi = 0, thi = it->x, hthi = 0;
      if (it != now.begin())
        it--, qthi = it->x, it++;
      else
        qthi = now.rbegin()->x;
      if (++it != now.end())
        hthi = it->x, it--;
      else
        it--, hthi = now.begin()->x;
      res -= getdis(qthi, hthi);
      res += getdis(qthi, thi);
      res += getdis(thi, hthi);
      vis[x] = 1;
    } else if (st[0] == '-') {
      int x = read();
      if (!vis[x]) continue;
      it = now.find(a[x]);
      int qthi = 0, thi = it->x, hthi = 0;
      if (it != now.begin())
        it--, qthi = it->x, it++;
      else
        qthi = now.rbegin()->x;
      if (++it != now.end())
        hthi = it->x, it--;
      else
        it--, hthi = now.begin()->x;
      now.erase(now.find(a[x]));
      res += getdis(qthi, hthi);
      res -= getdis(qthi, thi);
      res -= getdis(thi, hthi);
      vis[x] = 0;
    } else {
      printf("%I64d\n", res / 2ll);
    }
  }
  return 0;
}
