This time you should help a team of researchers on an island in the Pacific Ocean. They research the culture of the ancient tribes that used to inhabit the island many years ago.

Overall they've dug out n villages. Some pairs of villages were connected by roads. People could go on the roads in both directions. Overall there were exactly n - 1 roads, and from any village one could get to any other one.

The tribes were not peaceful and they had many wars. As a result of the wars, some villages were destroyed completely. During more peaceful years some of the villages were restored.

At each moment of time people used only those roads that belonged to some shortest way between two villages that existed at the given moment. In other words, people used the minimum subset of roads in such a way, that it was possible to get from any existing village to any other existing one. Note that throughout the island's whole history, there existed exactly n - 1 roads that have been found by the researchers. There never were any other roads.

The researchers think that observing the total sum of used roads’ lengths at different moments of time can help to better understand the tribes' culture and answer several historical questions.

You will be given the full history of the tribes' existence. Your task is to determine the total length of used roads at some moments of time.

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of villages. The next n - 1 lines describe the roads. The i-th of these lines contains three integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 109, 1 ≤ i < n) — the numbers of villages that are connected by the i-th road and the road's length. The numbers in the lines are separated by a space.

The next line contains an integer q (1 ≤ q ≤ 105) — the number of queries. Then follow q queries, one per line, ordered by time. Each query belongs to one of three types:

  * "+ x" — village number x is restored (1 ≤ x ≤ n). 
  * "- x" — village number x is destroyed (1 ≤ x ≤ n). 
  * "?" — the archaeologists want to know the total length of the roads which were used for that time period.



It is guaranteed that the queries do not contradict each other, that is, there won't be queries to destroy non-existing villages or restore the already existing ones. It is guaranteed that we have at least one query of type "?". It is also guaranteed that one can get from any village to any other one by the given roads.

At the initial moment of time no village is considered to exist.

Output

For each query of type "?" print the total length of used roads on a single line. You should print the answers to the queries in the order, in which they are given in the input.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Examples

Input

6
1 2 1
1 3 5
4 1 7
4 5 3
6 4 2
10
+ 3
+ 1
?
+ 6
?
+ 5
?
- 6
- 3
?


Output

5
14
17
10
#include <bits/stdc++.h>
using namespace std;
int N, dad[110000][20], num[110000], cnt, F[110000];
int d[110000], Q, ccnt, B[110000], E[110000];
pair<int, int> seg[110000 * 5];
long long D[110000], len[110000][20], res;
vector<pair<int, int> > v[110000];
void dfs(int x, int parent, long long l, int depth) {
  dad[x][0] = parent;
  D[x] = l;
  d[x] = depth;
  len[x][0] = D[x] - D[parent];
  B[x] = ++ccnt;
  for (int i = 1; i <= 18; i++) dad[x][i] = dad[dad[x][i - 1]][i - 1];
  for (int i = 1; i <= 18; i++)
    len[x][i] = len[dad[x][i - 1]][i - 1] + D[x] - D[dad[x][i - 1]];
  for (int i = 0; i < (int)v[x].size(); i++)
    if (v[x][i].first != parent)
      dfs(v[x][i].first, x, l + v[x][i].second, depth + 1);
  num[x] = ++cnt;
  E[x] = ccnt;
}
pair<int, long long> get_lca(int a, int b) {
  if (d[b] > d[a]) swap(a, b);
  if (!b) return pair<int, long long>(a, 0);
  long long l = 0;
  for (int i = 18; i >= 0; i--)
    if (d[b] <= d[dad[a][i]]) {
      l += len[a][i];
      a = dad[a][i];
    }
  if (a == b) return pair<int, long long>(a, l);
  for (int i = 18; i >= 0; i--)
    if (dad[a][i] != dad[b][i]) {
      l += len[a][i] + len[b][i];
      a = dad[a][i];
      b = dad[b][i];
    }
  l += len[a][0] + len[b][0];
  return pair<int, long long>(dad[a][0], l);
}
int find_mn() {
  int k = 1, b = 1, e = N;
  while (b != e) {
    if (seg[2 * k].first) {
      k = k + k;
      e = ((b + e) / 2);
    } else {
      k = k + k + 1;
      b = ((b + e) / 2) + 1;
    }
  }
  if (!seg[k].first) return 0;
  return seg[k].second;
}
int find_mx() {
  int k = 1, b = 1, e = N;
  while (b != e) {
    if (seg[2 * k + 1].first) {
      k = k + k + 1;
      b = ((b + e) / 2) + 1;
    } else {
      k = k + k;
      e = ((b + e) / 2);
    }
  }
  if (!seg[k].first) return 0;
  return seg[k].second;
}
void upd(int k, int b, int e, int a, int t, int l) {
  seg[k].first += t;
  if (b == e) {
    seg[k].second = l;
    return;
  }
  if (a <= ((b + e) / 2))
    upd(2 * k, b, ((b + e) / 2), a, t, l);
  else
    upd(2 * k + 1, ((b + e) / 2) + 1, e, a, t, l);
}
void remove(int x) {
  for (int i = B[x]; i <= N; i += (i & -i)) F[i]--;
  upd(1, 1, N, num[x], -1, 0);
  int mn = find_mn();
  int mx = find_mx();
  int old = get_lca(mn, mx).first;
  int sm = 0;
  for (int i = E[x]; i; i -= (i & -i)) sm += F[i];
  for (int i = B[x] - 1; i; i -= (i & -i)) sm -= F[i];
  pair<int, long long> lca = get_lca(old, x);
  if (lca.first != old) {
    res -= lca.second;
    return;
  }
  if (sm) return;
  for (int i = 18; i >= 0; i--) {
    int y = dad[x][i];
    sm = 0;
    for (int i = E[y]; i; i -= (i & -i)) sm += F[i];
    if (B[y] - 1 >= 0)
      for (int i = B[y] - 1; i; i -= (i & -i)) sm -= F[i];
    if (y && !sm) {
      res -= len[x][i], x = dad[x][i];
    }
  }
  res -= len[x][0];
}
void add(int x) {
  int mn = find_mn();
  int mx = find_mx();
  int old = get_lca(mn, mx).first;
  int sm = 0;
  for (int i = E[x]; i; i -= (i & -i)) sm += F[i];
  for (int i = B[x] - 1; i; i -= (i & -i)) sm -= F[i];
  for (int i = B[x]; i <= N; i += (i & -i)) F[i]++;
  upd(1, 1, N, num[x], 1, x);
  pair<int, long long> lca = get_lca(old, x);
  if (lca.first != old) {
    res += lca.second;
    return;
  }
  if (sm) return;
  for (int i = B[x]; i <= N; i += (i & -i)) F[i]--;
  int tt = x;
  for (int i = 18; i >= 0; i--) {
    int y = dad[x][i];
    sm = 0;
    for (int i = E[y]; i; i -= (i & -i)) sm += F[i];
    if (B[y] - 1 >= 0)
      for (int i = B[y] - 1; i; i -= (i & -i)) sm -= F[i];
    if (y && !sm) {
      res += len[x][i], x = dad[x][i];
    }
  }
  for (int i = B[tt]; i <= N; i += (i & -i)) F[i]++;
  res += len[x][0];
}
int main() {
  cin >> N;
  for (int i = 1, a, b, c; i < N; i++) {
    scanf(" %d %d %d", &a, &b, &c);
    v[a].push_back(pair<int, int>(b, c));
    v[b].push_back(pair<int, int>(a, c));
  }
  dfs(1, 0, 0, 1);
  cin >> Q;
  while (Q--) {
    char c;
    int a, b;
    scanf(" %c", &c);
    if (c == '-') {
      scanf(" %d", &a);
      remove(a);
    } else if (c == '+') {
      scanf(" %d", &a);
      add(a);
    } else
      printf("%I64d\n", res);
  }
  return 0;
}
