static long LCA(int p, int q) {
		int tmp, log, i;
		// if p is situated on a higher level than q then we swap them
		if (L[p] < L[q]) {
			tmp = p;
			p = q;
			q = tmp;
		}
		// we compute the value of [log(L[p)]
		for (log = 1; 1 << log <= L[p]; log++)
			; // ----
		log--;
		long sum = 0;
		// we find the ancestor of node p situated on the same level
		// with q using the values in P
		for (i = log; i >= 0; i--)
			if (L[p] - (1 << i) >= L[q]) {
				sum += D[p][i];
				p = P[p][i];
			}
		if (p == q)
			// [return p; LCA] [return max; MAX]
			return sum; // SUM

		// we compute LCA(p, q) using the values in P
		for (i = log; i >= 0; i--)
			if (P[p][i] != -1 && P[p][i] != P[q][i]) {
				sum += D[p][i] + D[q][i];
				p = P[p][i];
				q = P[q][i];
			}
		sum += D[p][0] + D[q][0];
		// [return T[p]; LCA] [return max; MAX]
		return sum; // SUM
	}