You are given a tree with nodes and queries. Each query asks for the distance between two nodes. Since can be large (up to ), you want queries. Use Euler tour + sparse table for LCA, then apply the distance formula. Preprocess: run DFS to compute depths, build Euler tour, build sparse table.
For each query (u, v), find lca(u, v) in , then compute depth[u] + depth[v] - 2 * depth[lca] in . Total time: . This is fast enough for the problem constraints. The insight is recognizing that distance queries reduce to LCA queries.