Define up[v][j] = the 2j-th ancestor of node v. This is the node you reach by jumping up 2j steps from v. For example, up[v][0] is the parent (jump 20=1 step). up[v][1] is the grandparent (jump 21=2 steps). up[v][3] is the 8th ancestor.
If you precompute this table, you can answer any k-th ancestor query by decomposing k into powers of 2 and making at most logk jumps.