#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
const int LOG = 18;
const int INF = 1e6;
template<int MAXN, int MAXLG> struct centroidDecomp{
vector<int> adj[MAXN];
int sz[MAXN]; //size of subtree
int cpar[MAXN];//parent in centroid tree
multiset<pair<int, int>> cdist[MAXN];
int color [MAX];
bool vis[MAXN]; //visited array
int L[MAXLG][MAXN]; //lca
int depth[MAXN]; //depth
void addEdge(int u, int v){
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
int getDist(int v){
if(cdist[v].empty() == true){
return INF;
}
return (*(cdist[v].rbegin())).first;
}
void dfs(int n, int p) {
depth[n] = depth[p] + 1;
L[0][n] = p;
for (int i = 0; i < MAXLG - 1; i++)
L[i + 1][n] = L[i][L[i][n]];
for (int v : adj[n]) if (v != p) {
dfs(v, n);
}
}
int lca(int u, int v) {
if (depth[u] < depth[v])
swap(u, v);
for (int i = MAXLG - 1; i >= 0; i--) {
if (depth[u] - (1<<i) >= depth[v])
u = L[i][u];
}
for (int i = MAXLG - 1; i >= 0; i--) {
if (L[i][u] != L[i][v])
u = L[i][u], v = L[i][v];
}
return u == v ? u : L[0][u];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
void dfs_sz(int n, int p=-1) {
sz[n] = 1;
for (int v : adj[n]) if (v != p && !vis[v]) {
dfs_sz(v, n);
sz[n] += sz[v];
}
}
int centroid(int n) {
dfs_sz(n);
int num = sz[n];
int p = -1;
do {
int nxt = -1;
for (int v : adj[n]) if (!vis[v] && v != p) {
if (2 * sz[v] > num)
nxt = v;
}
p = n, n = nxt;
} while (~n);
return p;
}
int closest(int v){
int ans = getDist(v);
// cout << ans << " asdf "<< v << endl;
for (int n = v; n>=0; n = cpar[n]){
ans = min(ans, getDist(n) + dist(n, v));
//cout << getDist(n) << "asdf" << endl;
}
return ans;
}
void toggle(int v){
color[v] = 1-color[v];
if(color[v] == 1){
for (int n = v; n>=0; n = cpar[n]){
cdist[n].insert(make_pair(dist(n, v), v));
//cout << dist(n, v) << " inserted " << v << " " << n << endl;
}
}
else{
for (int n = v; n>=0; n = cpar[n]){
cdist[n].erase(make_pair(dist(n, v), v));
}
}
}
void centroid_decomp(int n, int p=-1) {
int c = centroid(n);
vis[c] = true;
cpar[c] = p;
for (int v : adj[c]) if (!vis[v]) {
centroid_decomp(v, c);
}
}
};
int n, q;
centroidDecomp<MAX, LOG> cd;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n-1; i++) {
int u, v;
cin >> u >> v;
u--; v--;
cd.addEdge(u, v);
}
cd.dfs(0, 0);
cd.centroid_decomp(0);
cin >> q;
for(int i = 0; i<q; i++) {
int t, v;
cin >> t >> v;
v--;
if (t == 0) {
cd.toggle(v);
}
else {
int ans = cd.closest(v);
if(ans >= n || ans <0){
cout << -1 << endl;
continue;
}
cout << ans << endl;;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKY29uc3QgaW50IE1BWCA9IDFlNSArIDU7CmNvbnN0IGludCBMT0cgPSAxODsKY29uc3QgaW50IElORiA9IDFlNjsKdGVtcGxhdGU8aW50IE1BWE4sIGludCBNQVhMRz4gc3RydWN0IGNlbnRyb2lkRGVjb21wewogICAgdmVjdG9yPGludD4gYWRqW01BWE5dOwogICAgaW50IHN6W01BWE5dOyAvL3NpemUgb2Ygc3VidHJlZQogICAgaW50IGNwYXJbTUFYTl07Ly9wYXJlbnQgaW4gY2VudHJvaWQgdHJlZQogICAgbXVsdGlzZXQ8cGFpcjxpbnQsIGludD4+IGNkaXN0W01BWE5dOwogICAgaW50IGNvbG9yIFtNQVhdOwogICAgYm9vbCB2aXNbTUFYTl07IC8vdmlzaXRlZCBhcnJheQoKICAgIGludCBMW01BWExHXVtNQVhOXTsgLy9sY2EKICAgIGludCBkZXB0aFtNQVhOXTsgLy9kZXB0aAogICAgdm9pZCBhZGRFZGdlKGludCB1LCBpbnQgdil7CiAgICAgICAgYWRqW3VdLmVtcGxhY2VfYmFjayh2KTsKICAgICAgICBhZGpbdl0uZW1wbGFjZV9iYWNrKHUpOwogICAgfQogICAgaW50IGdldERpc3QoaW50IHYpewogICAgICAgIGlmKGNkaXN0W3ZdLmVtcHR5KCkgPT0gdHJ1ZSl7CiAgICAgICAgICAgIHJldHVybiBJTkY7CiAgICAgICAgfQogICAgICAgIHJldHVybiAoKihjZGlzdFt2XS5yYmVnaW4oKSkpLmZpcnN0OwogICAgfQogICAgdm9pZCBkZnMoaW50IG4sIGludCBwKSB7CiAgICAgICAgZGVwdGhbbl0gPSBkZXB0aFtwXSArIDE7CiAgICAgICAgTFswXVtuXSA9IHA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBNQVhMRyAtIDE7IGkrKykKICAgICAgICAgICAgTFtpICsgMV1bbl0gPSBMW2ldW0xbaV1bbl1dOwogICAgICAgIGZvciAoaW50IHYgOiBhZGpbbl0pIGlmICh2ICE9IHApIHsKICAgICAgICAgICAgZGZzKHYsIG4pOwogICAgICAgIH0KICAgIH0KCiAgICBpbnQgbGNhKGludCB1LCBpbnQgdikgewogICAgICAgIGlmIChkZXB0aFt1XSA8IGRlcHRoW3ZdKQogICAgICAgICAgICBzd2FwKHUsIHYpOwogICAgICAgIGZvciAoaW50IGkgPSBNQVhMRyAtIDE7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgICAgIGlmIChkZXB0aFt1XSAtICgxPDxpKSA+PSBkZXB0aFt2XSkKICAgICAgICAgICAgICAgIHUgPSBMW2ldW3VdOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gTUFYTEcgLSAxOyBpID49IDA7IGktLSkgewogICAgICAgICAgICBpZiAoTFtpXVt1XSAhPSBMW2ldW3ZdKQogICAgICAgICAgICAgICAgdSA9IExbaV1bdV0sIHYgPSBMW2ldW3ZdOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdSA9PSB2ID8gdSA6IExbMF1bdV07CiAgICB9CgogICAgaW50IGRpc3QoaW50IHUsIGludCB2KSB7CiAgICAgICAgcmV0dXJuIGRlcHRoW3VdICsgZGVwdGhbdl0gLSAyICogZGVwdGhbbGNhKHUsIHYpXTsKICAgIH0KCiAgICB2b2lkIGRmc19zeihpbnQgbiwgaW50IHA9LTEpIHsKICAgICAgICBzeltuXSA9IDE7CiAgICAgICAgZm9yIChpbnQgdiA6IGFkaltuXSkgaWYgKHYgIT0gcCAmJiAhdmlzW3ZdKSB7CiAgICAgICAgICAgIGRmc19zeih2LCBuKTsKICAgICAgICAgICAgc3pbbl0gKz0gc3pbdl07CiAgICAgICAgfQogICAgfQoKICAgIGludCBjZW50cm9pZChpbnQgbikgewogICAgICAgIGRmc19zeihuKTsKICAgICAgICBpbnQgbnVtID0gc3pbbl07CiAgICAgICAgaW50IHAgPSAtMTsKICAgICAgICBkbyB7CiAgICAgICAgICAgIGludCBueHQgPSAtMTsKICAgICAgICAgICAgZm9yIChpbnQgdiA6IGFkaltuXSkgaWYgKCF2aXNbdl0gJiYgdiAhPSBwKSB7CiAgICAgICAgICAgICAgICBpZiAoMiAqIHN6W3ZdID4gbnVtKQogICAgICAgICAgICAgICAgICAgIG54dCA9IHY7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcCA9IG4sIG4gPSBueHQ7CiAgICAgICAgfSB3aGlsZSAofm4pOwogICAgICAgIHJldHVybiBwOwogICAgfQogICAgaW50IGNsb3Nlc3QoaW50IHYpewogICAgICAgIGludCBhbnMgPSBnZXREaXN0KHYpOwogICAgICAgLy8gY291dCA8PCBhbnMgPDwgIiBhc2RmICI8PCB2IDw8IGVuZGw7CiAgICAgICAgZm9yIChpbnQgbiA9IHY7IG4+PTA7IG4gPSBjcGFyW25dKXsKICAgICAgICAgICAgYW5zID0gbWluKGFucywgZ2V0RGlzdChuKSArIGRpc3QobiwgdikpOwogICAgICAgICAgICAvL2NvdXQgPDwgZ2V0RGlzdChuKSA8PCAiYXNkZiIgPDwg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A+PiB2OwoJCXUtLTsgdi0tOwoJCWNkLmFkZEVkZ2UodSwgdik7Cgl9CgoJY2QuZGZzKDAsIDApOwoJY2QuY2VudHJvaWRfZGVjb21wKDApOwogICAgY2luID4+IHE7Cglmb3IoaW50IGkgPSAwOyBpPHE7IGkrKykgewoJCWludCB0LCB2OwoJCWNpbiA+PiB0ID4+IHY7CgkJdi0tOwoJCWlmICh0ID09IDApIHsKICAgICAgICAgICAgY2QudG9nZ2xlKHYpOwoJCX0KCQllbHNlIHsKICAgICAgICAgICAgaW50IGFucyA9IGNkLmNsb3Nlc3Qodik7CiAgICAgICAgICAgIGlmKGFucyA+PSBuIHx8IGFucyA8MCl7CiAgICAgICAgICAgICAgICBjb3V0IDw8IC0xIDw8IGVuZGw7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQoJCQljb3V0IDw8ICBhbnMgPDwgZW5kbDs7CgkJfQoJfQoJcmV0dXJuIDA7Cn0K