int main(void) { freopen("A.in", "r", stdin); int a, b, N, q; for (readint(N); N; readint(N)) { long long w; P[0][0] = -1; L[0] = W[0] = 0; for (int i = 1; i < N; ++i) { readint(a), readll(w); for (int j = 0; (1 << j) < N; ++j) P[i][j] = -1; P[i][0] = a; L[i] = L[a] + 1; W[i] = W[a] + w; } for (int j = 1; (1 << j) < N; ++j) for (int i = 0; i < N; ++i) if (P[i][j - 1] != -1) P[i][j] = P[P[i][j - 1]][j - 1]; readint(q); while (q--) { readint(a), readint(b); printf("%lld", W[a] + W[b] - (W[query(a, b)] << 1)); if (q) putc(32, stdout); } putc(10, stdout); } return 0; }
readztm (string sciez, hafas_center* h) : haf(h) { fstream plik; plik.open(sciez.c_str()); char data[100000]; while(plik.getline(data,100000)) { stringstream plt; plt<<data; string lll; plt>>lll; int s9=lll.size(); string roftl; for(int i=1; i<s9; i++) { roftl+=lll[i]; } if(roftl=="LL") readll(roftl, plik); else { if(roftl=="ZP") readzp(roftl, plik); else readobszar(roftl, plik); } } }