504_E. Misha and LCP on Tree

Misha has a tree with characters written on the vertices. He can choose two vertices s and t of this tree and write down characters of vertices lying on a path from s to t. We'll say that such string corresponds to pair (s, t).

Misha has m queries of type: you are given 4 vertices a, b, c, d; you need to find the largest common prefix of the strings that correspond to pairs (a, b) and (c, d). Your task is to help him.

Input

The first line contains integer n (1 ≤ n ≤ 300 000) — the number of vertices in the tree.

Next follows a line consisting of n small English letters. The i-th character of the string corresponds to the character written on the i-th vertex. 

Next n - 1 lines contain information about edges. An edge is defined by a pair of integers u, v (1 ≤ u, v ≤ n, u ≠ v), separated by spaces.

The next line contains integer m (1 ≤ m ≤ 1 000 000) — the number of queries.

Next m lines contain information about queries. A query is defined by four integers a, b, c, d (1 ≤ a, b, c, d ≤ n), separated by spaces.

Output

For each query print the length of the largest common prefix on a separate line.

Examples

Input

6
bbbabb
2 1
3 2
4 3
5 2
6 5
6
2 5 3 1
1 5 2 3
5 6 5 6
6 3 4 1
6 2 3 4
2 2 4 5


Output

2
2
2
0
1
0

Example input:
6
bbbabb
2 1
3 2
4 3
5 2
6 5
6
2 5 3 1
1 5 2 3
5 6 5 6
6 3 4 1
6 2 3 4
2 2 4 5


Example output:
2
2
2
0
1
0


Tags: binary search
dfs and similar
hashing
string suffix structures
trees
Difficulty rating: 3000

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, mod = 1e9 + 7, base = 233, ibase = 90128756;
int n, m, a, b, c, d, u, v, tot;
char s[N];
int bpow[N], ibpow[N], head[N], h[N], top[N], fa[N], son[N], size[N], id[N],
    rev[N], val[2][N];
struct edge {
  int v, next;
} e[N << 1];
inline void add(int u, int v) {
  static int k = 0;
  e[++k] = (edge){v, head[u]};
  head[u] = k;
}
void DFS1(int u, int p) {
  fa[u] = p, h[u] = h[p] + 1, size[u] = 1;
  val[0][u] = (1ll * val[0][p] * base + s[u]) % mod;
  val[1][u] = (val[1][p] + 1ll * s[u] * bpow[h[u] - 1]) % mod;
  for (register int i = head[u]; i; i = e[i].next)
    if (e[i].v != p) {
      DFS1(e[i].v, u);
      size[u] += size[e[i].v];
      if (size[e[i].v] > size[son[u]]) son[u] = e[i].v;
    }
}
void DFS2(int u, int t) {
  top[u] = t, id[u] = ++tot, rev[tot] = u;
  if (!son[u]) return;
  DFS2(son[u], t);
  for (register int i = head[u]; i; i = e[i].next)
    if (e[i].v != fa[u] && e[i].v != son[u]) DFS2(e[i].v, e[i].v);
}
inline int LCA(int x, int y) {
  while (top[x] != top[y]) {
    if (h[top[x]] < h[top[y]]) swap(x, y);
    x = fa[top[x]];
  }
  return h[x] < h[y] ? x : y;
}
inline int find(int x, int k) {
  while (k && k >= h[x] - h[fa[top[x]]])
    k -= h[x] - h[fa[top[x]]], x = fa[top[x]];
  return rev[id[x] - k];
}
inline int Hash(int x, int y, int z, int k) {
  if (!k) return 0;
  if (h[x] - h[z] + 1 >= k)
    return (val[0][x] - 1ll * val[0][find(x, k)] * bpow[k] % mod + mod) % mod;
  int len1 = h[x] - h[z], len2 = h[y] - h[z];
  return ((val[0][x] - 1ll * val[0][fa[z]] * bpow[len1 + 1] % mod + mod) +
          1ll * (val[1][find(y, len2 - (k - len1) + 1)] - val[1][z] + mod) *
              ibpow[h[z]] % mod * bpow[len1 + 1] % mod) %
         mod;
}
int main(void) {
  scanf("%d", &n);
  bpow[0] = 1, ibpow[0] = 1;
  ;
  for (register int i = 1; i <= n; ++i)
    bpow[i] = 1ll * bpow[i - 1] * base % mod,
    ibpow[i] = 1ll * ibpow[i - 1] * ibase % mod;
  scanf("%s", s + 1);
  for (register int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v), add(v, u);
  }
  DFS1(1, 0), DFS2(1, 1);
  scanf("%d", &m);
  while (m--) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    int x = LCA(a, b), y = LCA(c, d);
    int l = 0, r = min(h[a] + h[b] - 2 * h[x] + 1, h[c] + h[d] - 2 * h[y] + 1),
        ans;
    while (l <= r) {
      int mid = l + r >> 1;
      if (Hash(a, b, x, mid) == Hash(c, d, y, mid))
        l = mid + 1, ans = mid;
      else
        r = mid - 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}