Here is the implementation. First, preprocess to build the binary lifting table and compute depths. Then use the two-step algorithm to answer queries. The preprocessing computes up[v][i] = -th ancestor of v, and depth[v] = distance to root. This takes time. For each query, level both nodes to the same depth using binary jumps.
Then jump them upward together in decreasing powers of . Stop when they are about to become the same node. At that point, up[u][0] is the LCA. Time: per query after preprocessing. Space: for the table. This is the standard approach for LCA in competitive programming when you need to handle many queries.