Mike has a tree with n nodes. On each node u, there is an integer cu.
A number is interesting if and only if its digits in the decimal representation are distinct. For example, the numbers 1546,198,1 are interesting but the numbers 1222,1001,9011 are not.
Consider a length-k path on the tree P1,P2,...,Pk, a subpath is determined by two parameters l and r (1≤l≤r≤k). The subpath starting from lth node and ending on rth node is Pl,Pl+1,...,Pr. Therefore, a length-k path has k×(k+1)2 subpaths.
He has q queries. Each query consists of two numbers, u and v. Considering the path starting from u and ending at v, he wants to know the number of interesting subpaths. A subpath is interesting if and only if there exists at least one node on the subpath whose assigned number is interesting.
Input
The first line contains a number n (1≤n≤500000).
The next line contains n numbers c1,c2,...,cn - ci is the assigned number to node i (1≤ci≤109).
The next n−1 lines contains two numbers separated by space.The (i+2)th line contains ai,bi (1≤ai,bi≤n) representing that there is an edge from the node ai to the node bi.
The next line contains a number q (1≤q≤500000).
The next q lines contains two numbers separated by space.The (n+1+i)th line contains ui,vi (1≤ui,vi≤n).
Output
Output q lines. The ith line must contain the answer for the ith query.
For first query, there are 11 interesting subpaths in the path starting from 4 and ending at 7.