Rick is in love with Unity. But Mr. Meeseeks also love Unity, so Rick and Mr. Meeseeks are "love rivals".
Unity loves rap, so it decided that they have to compete in a rap game (battle) in order to choose the best. Rick is too nerds, so instead he's gonna make his verse with running his original algorithm on lyrics "Rap God" song.
His algorithm is a little bit complicated. He's made a tree with
n vertices numbered from
1 to
n and there's a lowercase english letter written on each edge. He denotes
str(a,b) to be the string made by writing characters on edges on the shortest path from
a to
b one by one (a string of length equal to distance of
a to
b). Note that
str(a,b) is reverse of
str(b,a) and
str(a,a) is empty.
In order to make the best verse he can, he needs to answer some queries, but he's not a computer scientist and is not able to answer those queries, so he asked you to help him. Each query is characterized by two vertices
x and
y (
x≠y). Answer to this query is the number of vertices like
z such that
z≠x,z≠y and
str(x,y) is lexicographically larger than
str(x,z).
String
x=x1x2...x|x| is lexicographically larger than string
y=y1y2...y|y|, if either
|x|>|y| and
x1=y1,x2=y2,...,x|y|=y|y|, or exists such number
r (r<|x|,r<|y|), that
x1=y1,x2=y2,...,xr=yr and
xr+1>yr+1. Characters are compared like their ASCII codes (or alphabetic order).
Help Rick get the girl (or whatever gender Unity has).