#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;
struct LCA {
const int n = 0;
const int log2_n = 0;
vector<vector<int>> g;
vector<vector<int>> par; // par[2^i上][頂点v]
vector<int> dep;
void dfs(int v, int p, int d) {
par[0][v] = p;
dep[v] = d;
for(auto to: g[v]) {
if(to == p) continue;
dfs(to, v, d+1);
}
}
LCA() {}
LCA(int n) : n(n), log2_n(log2(n)+1), g(n),
par(log2_n, vector<int>(n)), dep(n) {}
void add_edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
}
void build(ll root=0) {
dfs(root, -1, 0);
for(int i=0; i+1 < log2_n; ++i) {
for(int j = 0; j < n; ++j) {
if(par[i][j] < 0) par[i+1][j] = -1;
else par[i+1][j] = par[i][par[i][j]];
}
}
}
int get(int u, int v) {
if(dep[u] > dep[v]) swap(u, v);
REP(i, log2_n) {
if((dep[v] - dep[u]) >> i & 1) {
v = par[i][v];
}
}
if(u == v) return u;
for(int i=log2_n-1; i>=0; --i) {
if(par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
ll dist(ll u, ll v) {
return dep[u] + dep[v] - 2 * dep[get(u, v)];
}
};
int main(void) {
ll n;
cin >> n;
LCA lca(n);
vector<vector<ll>> g(n);
REP(i, n-1) {
ll a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
lca.add_edge(a, b);
}
lca.build();
vector<ll> sz(n), dead(n);
auto find_centroid = [&](ll root) {
auto get_size = [&](auto &&self, ll v, ll p) -> void {
sz[v] = 1;
for(auto to: g[v]) if(to != p && !dead[to]) {
self(self, to, v);
sz[v] += sz[to];
}
};
get_size(get_size, root, -1);
auto dfs = [&](auto &&self, ll v, ll p) -> ll {
for(auto to: g[v]) if(to != p && !dead[to]) {
if(sz[to] > sz[root]/2) return self(self, to, v);
}
return v;
};
return dfs(dfs, root, -1);
};
vector<ll> par(n);
auto centroid_decomposition = [&](auto &&self, ll root, ll p) -> void {
ll c = find_centroid(root);
dead[c] = true;
par[c] = p;
for(auto to: g[c]) if(!dead[to]) {
self(self, to, c);
}
dead[c] = false;
};
centroid_decomposition(centroid_decomposition, 0, -1);
vector<ll> col(n);
vector<set<PII>> dist_to_white(n);
auto paint = [&](ll v) {
ll u = v;
while(u != -1) {
if(col[v] == 0) dist_to_white[u].insert({lca.dist(u, v), v});
else dist_to_white[u].erase({lca.dist(u, v), v});
u = par[u];
}
col[v] = 1-col[v];
};
auto query = [&](ll v) {
ll u = v, ret = INF;
while(u != -1) {
if(dist_to_white[u].size()) {
chmin(ret, dist_to_white[u].begin()->first + lca.dist(u, v));
}
u = par[u];
}
return ret == INF ? -1 : ret;
};
ll q;
cin >> q;
REP(i, q) {
ll t, v;
cin >> t >> v;
v--;
if(t == 0) paint(v);
else cout << query(v) << "\n";
}
cout << flush;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwp1c2luZyBQSUkgPSBwYWlyPGxsLCBsbD47CiNkZWZpbmUgRk9SKGksIGEsIG4pIGZvciAobGwgaSA9IChsbClhOyBpIDwgKGxsKW47ICsraSkKI2RlZmluZSBSRVAoaSwgbikgRk9SKGksIDAsIG4pCiNkZWZpbmUgQUxMKHgpIHguYmVnaW4oKSwgeC5lbmQoKQp0ZW1wbGF0ZTx0eXBlbmFtZSBUPiB2b2lkIGNobWluKFQgJmEsIGNvbnN0IFQ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dl0ucHVzaF9iYWNrKHUpOwogICAgfQoKICAgIHZvaWQgYnVpbGQobGwgcm9vdD0wKSB7CiAgICAgICAgZGZzKHJvb3QsIC0xLCAwKTsKICAgICAgICBmb3IoaW50IGk9MDsgaSsxIDwgbG9nMl9uOyArK2kpIHsKICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IG47ICsraikgewogICAgICAgICAgICAgICAgaWYocGFyW2ldW2pdIDwgMCkgcGFyW2krMV1bal0gPSAtMTsKICAgICAgICAgICAgICAgIGVsc2UgcGFyW2krMV1bal0gPSBwYXJbaV1bcGFyW2ldW2pdXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBpbnQgZ2V0KGludCB1LCBpbnQgdikgewogICAgICAgIGlmKGRlcFt1XSA+IGRlcFt2XSkgc3dhcCh1LCB2KTsKICAgICAgICBSRVAoaSwgbG9nMl9uKSB7CiAgICAgICAgICAgIGlmKChkZXBbdl0gLSBkZXBbdV0pID4+IGkgJiAxKSB7CiAgICAgICAgICAgICAgICB2ID0gcGFyW2ldW3ZdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKHUgPT0gdikgcmV0dXJuIHU7CiAgICAgICAgZm9yKGludCBpPWxvZzJfbi0xOyBpPj0wOyAtLWkpIHsKICAgICAgICAgICAgaWYocGFyW2ldW3VdICE9IHBhcltpXVt2XSkgewogICAgICAgICAgICAgICAgdSA9IHBhcltpXVt1XTsKICAgICAgICAgICAgICAgIHYgPSBwYXJbaV1bdl07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIHBhclswXVt1XTsKICAgIH0KCiAgICBsbCBkaXN0KGxsIHUsIGxsIHYpIHsKICAgICAgICByZXR1cm4gZGVwW3VdICsgZGVwW3ZdIC0gMiAqIGRlcFtnZXQodSwgdildOwogICAgfQp9OwoKaW50IG1haW4odm9pZCkgewogICAgbGwgbjsKICAgIGNpbiA+PiBuOwogICAgTENBIGxjYShuKTsKICAgIHZlY3Rvcjx2ZWN0b3I8bGw+PiBnKG4pOwogICAgUkVQKGksIG4tMSkgewogICAgICAgIGxsIGEsIGI7CiAgICAgICAgY2luID4+IGEgPj4gYjsKICAgICAgICBhLS0sIGItLTsKICAgICAgICBnW2FdLnB1c2hfYmFjayhiKTsKICAgICAgICBnW2JdLnB1c2hfYmFjayhhKTsKICAgICAgICBsY2EuYWRkX2VkZ2UoYSwgYik7CiAgICB9CiAgICBsY2EuYnVpbGQoKTsKCiAgICB2ZWN0b3I8bGw+IHN6KG4pLCBkZWFkKG4pOwogICAgYXV0byBmaW5kX2NlbnRyb2lkID0gWyZdKGxsIHJvb3QpIHsKICAgICAgICBhdXRvIGdldF9zaXplID0gWyZdKGF1dG8gJiZzZWxmLCBsbCB2LCBsbCBwKSAtPiB2b2lkIHsKICAgICAgICAgICAgc3pbdl0gPSAxOwogICAgICAgICAgICBmb3IoYXV0byB0bzogZ1t2XSkgaWYodG8gIT0gcCAmJiAhZGVhZFt0b10pIHsKICAgICAgICAgICAgICAgIHNlbGYoc2VsZiwgdG8sIHYpOwogICAgICAgICAgICAgICAgc3pbdl0gKz0gc3pbdG9dOwogICAgICAgICAgICB9CiAgICAgICAgfTsKICAgICAgICBnZXRfc2l6ZShnZXRfc2l6ZSwgcm9vdCwgLTEpOwogICAgICAgIGF1dG8gZGZzID0gWyZdKGF1dG8gJiZzZWxmLCBsbCB2LCBsbCBwKSAtPiBsbCB7CiAgICAgICAgICAgIGZvcihhdXRvIHRvOiBnW3ZdKSBpZih0byAhPSBwICYmICFkZWFkW3RvXSkgewogICAgICAgICAgICAgICAgaWYoc3pbdG9dID4gc3pbcm9vdF0vMikgcmV0dXJuIHNlbGYoc2VsZiwgdG8sIHYpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB2OwogICAgICAgIH07CiAgICAgICAgcmV0dXJuIGRmcyhkZnMsIHJvb3QsIC0xKTsKICAgIH07CgogICAgdmVjdG9yPGxsPiBwYXIobik7CiAgICBhdXRvIGNlbnRyb2lkX2RlY29tcG9zaXRpb24gPSBbJl0oYXV0byAmJnNlbGYsIGxsIHJvb3QsIGxsIHApIC0+IHZvaWQgewogICAgICAgIGxsIGMgPSBmaW5kX2NlbnRyb2lkKHJvb3QpOwogICAgICAgIGRlYWRbY10gPSB0cnVlOwogICAgICAgIHBhcltjXSA9IHA7CiAgICAgICAgZm9yKGF1dG8gdG86IGdbY10pIGlmKCFkZWFkW3RvXSkgewogICAgICAgICAgICBzZWxmKHNlbGYsIHRvLCBjKTsKICAgICAgICB9CiAgICAgICAgZGVhZFtjXSA9IGZhbHNlOwogICAgfTsKICAgIGNlbnRyb2lkX2RlY29tcG9zaXRpb24oY2VudHJvaWRfZGVjb21wb3NpdGlvbiwgMCwgLTEpOwoKICAgIHZlY3RvcjxsbD4gY29sKG4pOwogICAgdmVjdG9yPHNldDxQSUk+PiBkaXN0X3RvX3doaXRlKG4pOwogICAgYXV0byBwYWludCA9IFsmXShsbCB2KSB7CiAgICAgICAgbGwgdSA9IHY7CiAgICAgICAgd2hpbGUodSAhPSAtMSkgewogICAgICAgICAgICBpZihjb2xbdl0gPT0gMCkgZGlzdF90b193aGl0ZVt1XS5pbnNlcnQoe2xjYS5kaXN0KHUsIHYpLCB2fSk7CiAgICAgICAgICAgIGVsc2UgZGlzdF90b193aGl0ZVt1XS5lcmFzZSh7bGNhLmRpc3QodSwgdiksIHZ9KTsKICAgICAgICAgICAgdSA9IHBhclt1XTsKICAgICAgICB9CiAgICAgICAgY29sW3ZdID0gMS1jb2xbdl07CiAgICB9OwogICAgYXV0byBxdWVyeSA9IFsmXShsbCB2KSB7CiAgICAgICAgbGwgdSA9IHYsIHJldCA9IElORjsKICAgICAgICB3aGlsZSh1ICE9IC0xKSB7CiAgICAgICAgICAgIGlmKGRpc3RfdG9fd2hpdGVbdV0uc2l6ZSgpKSB7CiAgICAgICAgICAgICAgICBjaG1pbihyZXQsIGRpc3RfdG9fd2hpdGVbdV0uYmVnaW4oKS0+Zmlyc3QgKyBsY2EuZGlzdCh1LCB2KSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdSA9IHBhclt1XTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldCA9PSBJTkYgPyAtMSA6IHJldDsKICAgIH07CgogICAgbGwgcTsKICAgIGNpbiA+PiBxOwogICAgUkVQKGksIHEpIHsKICAgICAgICBsbCB0LCB2OwogICAgICAgIGNpbiA+PiB0ID4+IHY7CiAgICAgICAgdi0tOwogICAgICAgIGlmKHQgPT0gMCkgcGFpbnQodik7CiAgICAgICAgZWxzZSBjb3V0IDw8IHF1ZXJ5KHYpIDw8ICJcbiI7CiAgICB9CiAgICBjb3V0IDw8IGZsdXNoOwoKICAgIHJldHVybiAwOwp9