import algorithm, deques, heapqueue, math, sets, sequtils, strutils, sugar, tables proc input*(): string = return stdin.readLine proc chmax*[T: SomeNumber](num0: var T, num1: T) = num0 = max(num0, num1) proc chmin*[T: SomeNumber](num0: var T, num1: T) = num0 = min(num0, num1) proc `%=`*[T: SomeInteger](num0: var T, num1: T) = num0 = num0 mod num1 proc bit_length(n: Natural): Natural = const BIT_SIZE = 24 if n == 0: return 0 let s = toBin(n, BIT_SIZE) return BIT_SIZE - s.find('1') type LowestCommonAncestor* = ref object size: Positive LV: Natural depth: seq[int] tree, parent: seq[seq[int]] proc initLowestCommonAncestor*(tree: seq[seq[int]]): LowestCommonAncestor = let size = tree.len LV = bit_length(size) depth = newSeq[int](size) parent = newSeqWith(LV, newSeqWith(size, -1)) return LowestCommonAncestor(size: size, LV: LV, depth: depth, tree: tree, parent: parent) proc build*(self: var LowestCommonAncestor, root: Natural) = var que = initDeque[(int, int, int)]() que.addLast((root, -1, 0)) var cur, par, dist: int while que.len != 0: (cur, par, dist) = que.popFirst() self.parent[0][cur] = par self.depth[cur] = dist for child in self.tree[cur]: if child != par: self.depth[child] = dist + 1 que.addLast((child, cur, dist + 1)) for i in 1.. b) var xi, yi, li, ri: int for _ in 0..