#include <bits/stdc++.h>
using namespace std;
struct state {
  int len, link;
  map<char, int> next;
};
state st[300000];
int pos[300000], beg[300000], shes[300000], fin[300000], suru[300000],
    L[300000];
int tree[2500000], lefto[2500000], righto[2500000];
int nw, ps, ps2;
int sz, last;
vector<int> vc[300000], tree2[300000];
vector<char> cost[300000];
vector<pair<char, pair<int, int> > > dc3[300000];
char str[400];
void sa_init() {
  st[0].len = 0;
  st[0].link = -1;
  ++sz;
}
int sa_extend(char c, int last, int indx) {
  int cur = sz++;
  pos[indx] = cur;
  st[cur].len = st[last].len + 1;
  int p;
  for (p = last; p != -1 && !st[p].next.count(c); p = st[p].link)
    st[p].next[c] = cur;
  if (p == -1)
    st[cur].link = 0;
  else {
    int q = st[p].next[c];
    if (st[p].len + 1 == st[q].len)
      st[cur].link = q;
    else {
      int clone = sz++;
      st[clone].len = st[p].len + 1;
      st[clone].next = st[q].next;
      st[clone].link = st[q].link;
      for (; p != -1 && st[p].next[c] == q; p = st[p].link)
        st[p].next[c] = clone;
      st[q].link = st[cur].link = clone;
    }
  }
  last = cur;
  return last;
}
int update(int node, int st, int en, int l) {
  if (st > l or en < l) return node;
  int ps = ++nw;
  if (st == en) {
    tree[ps] = tree[node] + 1;
    return ps;
  }
  int mid = (st + en) >> 1;
  lefto[ps] = update(lefto[node], st, mid, l);
  righto[ps] = update(righto[node], mid + 1, en, l);
  tree[ps] = tree[lefto[ps]] + tree[righto[ps]];
  return ps;
}
int query(int node, int node2, int st, int en, int l, int r) {
  if (st >= l and en <= r) {
    return tree[node] - tree[node2];
  }
  int mid = (st + en) >> 1;
  int rr = 0;
  if (!(st > r or mid < l))
    rr += query(lefto[node], lefto[node2], st, mid, l, r);
  if (!(mid + 1 > r or en < l))
    rr += query(righto[node], righto[node2], mid + 1, en, l, r);
  return rr;
}
void dfs2(int v) {
  suru[v] = ++ps;
  for (int i = 0; i < tree2[v].size(); i++) {
    int w = tree2[v][i];
    dfs2(w);
  }
  shes[v] = ps;
}
int P[300000][18];
void dfs(int v, int par, int lev, int last, char c) {
  P[v][0] = par;
  beg[v] = ++ps2;
  L[v] = lev;
  if (v != 1) last = sa_extend(c, last, v);
  for (int i = 0; i < vc[v].size(); i++) {
    int w = vc[v][i];
    if (w == par) continue;
    dfs(w, v, lev + 1, last, cost[v][i]);
    dc3[v].push_back(make_pair(cost[v][i], make_pair(fin[w], w)));
  }
  fin[v] = ps2;
}
int findo(int x, int st, int en) {
  if (beg[x] <= st && fin[x] >= en) return x;
  for (int i = 17; i >= 0; i--) {
    if (P[x][i] == -1) continue;
    int w = P[x][i];
    if (beg[w] <= st && fin[w] >= en) continue;
    x = P[x][i];
  }
  return P[x][0];
}
int findo2(int x, int l) {
  for (int i = 17; i >= 0; i--) {
    if (P[x][i] == -1) continue;
    int w = P[x][i];
    if (L[w] <= l) continue;
    x = P[x][i];
  }
  return P[x][0];
}
int root[300000];
void dfs3(int v, int par) {
  root[v] = root[par];
  if (v > 1) {
    root[v] = update(root[v], 1, ps, suru[pos[v]]);
  }
  for (int i = 0; i < dc3[v].size(); i++) {
    dfs3(dc3[v][i].second.second, v);
  }
}
string pat, txt;
int lps[400];
void computeLPSArray() {
  int len = 0;
  lps[0] = 0;
  int i = 1;
  int M = pat.size();
  while (i < M) {
    if (pat[i] == pat[len]) {
      len++;
      lps[i] = len;
      i++;
    } else {
      if (len != 0) {
        len = lps[len - 1];
      } else {
        lps[i] = 0;
        i++;
      }
    }
  }
}
int kmpsear() {
  computeLPSArray();
  int M = pat.size();
  int N = txt.size();
  int i = 0;
  int j = 0, cnt = 0;
  while (i < N) {
    if (pat[j] == txt[i]) {
      j++;
      i++;
    }
    if (j == M) {
      cnt++;
      j = lps[j - 1];
    } else if (i < N && pat[j] != txt[i]) {
      if (j != 0)
        j = lps[j - 1];
      else
        i = i + 1;
    }
  }
  return cnt;
}
void dfs4(int v, int len, int tar) {
  if (len == 0 || v == tar) return;
  int lo = 0, high = dc3[v].size() - 1, ans = -1;
  while (lo <= high) {
    int mid = (lo + high) >> 1;
    if (dc3[v][mid].second.first >= beg[tar]) {
      ans = mid;
      high = mid - 1;
    } else
      lo = mid + 1;
  }
  if (ans == -1) return;
  char ch = dc3[v][ans].first;
  txt += ch;
  dfs4(dc3[v][ans].second.second, len - 1, tar);
}
int main() {
  int i, j, k, l, m, n;
  memset(P, -1, sizeof P);
  scanf("%d", &n);
  sa_init();
  for (int i = 1; i < n; i++) {
    scanf("%d%d%s", &l, &k, str);
    vc[l].push_back(k);
    vc[k].push_back(l);
    cost[l].push_back(str[0]);
    cost[k].push_back(str[0]);
  }
  dfs(1, -1, 0, 0, 'a');
  for (int j = 1; j <= 17; j++) {
    for (int i = 1; i <= n; i++) {
      if (P[i][j - 1] == -1) continue;
      P[i][j] = P[P[i][j - 1]][j - 1];
    }
  }
  for (int i = 1; i < sz; i++) {
    int w = st[i].link;
    tree2[w].push_back(i);
  }
  dfs2(0);
  dfs3(1, 0);
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y, ans = 0;
    char str[400];
    scanf("%d%d%s", &x, &y, str);
    if (x == y) {
      printf("0\n");
      continue;
    }
    int lc = findo(x, min(beg[x], beg[y]), max(beg[x], beg[y]));
    int g = strlen(str);
    int l = L[lc] + g - 1;
    if (l < L[x]) {
      int rot = 0, ck = 0;
      for (int i = g - 1; i >= 0; i--) {
        if (st[rot].next.find(str[i]) == st[rot].next.end()) {
          ck = -1;
          break;
        }
        rot = st[rot].next[str[i]];
      }
      if (ck != -1) {
        int f = findo2(x, l);
        ans += query(root[x], root[f], 1, ps, suru[rot], shes[rot]);
      }
    }
    if (l < L[y]) {
      int rot = 0, ck = 0;
      for (int i = 0; i < g; i++) {
        if (st[rot].next.find(str[i]) == st[rot].next.end()) {
          ck = -1;
          break;
        }
        rot = st[rot].next[str[i]];
      }
      if (ck != -1) {
        int f = findo2(y, l);
        ans += query(root[y], root[f], 1, ps, suru[rot], shes[rot]);
      }
    }
    pat.clear();
    pat += str;
    txt.clear();
    dfs4(lc, g - 1, x);
    reverse(txt.begin(), txt.end());
    dfs4(lc, g - 1, y);
    ans += kmpsear();
    printf("%d\n", ans);
  }
}

### Prompt

Please provide a CPP coded solution to the problem described below:

Since astronauts from BubbleCup XI mission finished their mission on the Moon and are big fans of famous singer, they decided to spend some fun time before returning to the Earth and hence created a so called "Moonwalk challenge" game.

Teams of astronauts are given the map of craters on the Moon and direct bidirectional paths from some craters to others that are safe for "Moonwalking". Each of those direct paths is colored in one color and there is unique path between each two craters. Goal of the game is to find two craters such that given array of colors appears most times as continuous subarray on the path between those two craters (overlapping appearances should be counted).

To help your favorite team win, you should make a program that, given the map, answers the queries of the following type: For two craters and array of colors answer how many times given array appears as continuous subarray on the path from the first crater to the second.

Colors are represented as lowercase English alphabet letters.

Input

In the first line, integer N (2 ≤ N ≤ 10^5) — number of craters on the Moon. Craters are numerated with numbers 1 to N.

In next N-1 lines, three values u, v, L (1 ≤ u, v ≤ N, L ∈ \\{a, ..., z\}) — denoting that there is a direct path with color L between craters u and v.

Next line contains integer Q (1 ≤ Q ≤ 10^5) — number of queries.

Next Q lines contain three values u, v (1 ≤ u, v ≤ N) and S (|S| ≤ 100), where u and v are the two cratersfor which you should find how many times array of colors S (represented as string) appears on the path from u to v. 

Output

For each query output one number that represents number of occurrences of array S on the path from u to v.

Example

Input

6
2 3 g
3 4 n
5 3 o
6 1 n
1 2 d
7
1 6 n
6 4 dg
6 4 n
2 5 og
1 2 d
6 5 go
2 3 g


Output

1
1
2
0
1
1
1

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
struct state {
  int len, link;
  map<char, int> next;
};
state st[300000];
int pos[300000], beg[300000], shes[300000], fin[300000], suru[300000],
    L[300000];
int tree[2500000], lefto[2500000], righto[2500000];
int nw, ps, ps2;
int sz, last;
vector<int> vc[300000], tree2[300000];
vector<char> cost[300000];
vector<pair<char, pair<int, int> > > dc3[300000];
char str[400];
void sa_init() {
  st[0].len = 0;
  st[0].link = -1;
  ++sz;
}
int sa_extend(char c, int last, int indx) {
  int cur = sz++;
  pos[indx] = cur;
  st[cur].len = st[last].len + 1;
  int p;
  for (p = last; p != -1 && !st[p].next.count(c); p = st[p].link)
    st[p].next[c] = cur;
  if (p == -1)
    st[cur].link = 0;
  else {
    int q = st[p].next[c];
    if (st[p].len + 1 == st[q].len)
      st[cur].link = q;
    else {
      int clone = sz++;
      st[clone].len = st[p].len + 1;
      st[clone].next = st[q].next;
      st[clone].link = st[q].link;
      for (; p != -1 && st[p].next[c] == q; p = st[p].link)
        st[p].next[c] = clone;
      st[q].link = st[cur].link = clone;
    }
  }
  last = cur;
  return last;
}
int update(int node, int st, int en, int l) {
  if (st > l or en < l) return node;
  int ps = ++nw;
  if (st == en) {
    tree[ps] = tree[node] + 1;
    return ps;
  }
  int mid = (st + en) >> 1;
  lefto[ps] = update(lefto[node], st, mid, l);
  righto[ps] = update(righto[node], mid + 1, en, l);
  tree[ps] = tree[lefto[ps]] + tree[righto[ps]];
  return ps;
}
int query(int node, int node2, int st, int en, int l, int r) {
  if (st >= l and en <= r) {
    return tree[node] - tree[node2];
  }
  int mid = (st + en) >> 1;
  int rr = 0;
  if (!(st > r or mid < l))
    rr += query(lefto[node], lefto[node2], st, mid, l, r);
  if (!(mid + 1 > r or en < l))
    rr += query(righto[node], righto[node2], mid + 1, en, l, r);
  return rr;
}
void dfs2(int v) {
  suru[v] = ++ps;
  for (int i = 0; i < tree2[v].size(); i++) {
    int w = tree2[v][i];
    dfs2(w);
  }
  shes[v] = ps;
}
int P[300000][18];
void dfs(int v, int par, int lev, int last, char c) {
  P[v][0] = par;
  beg[v] = ++ps2;
  L[v] = lev;
  if (v != 1) last = sa_extend(c, last, v);
  for (int i = 0; i < vc[v].size(); i++) {
    int w = vc[v][i];
    if (w == par) continue;
    dfs(w, v, lev + 1, last, cost[v][i]);
    dc3[v].push_back(make_pair(cost[v][i], make_pair(fin[w], w)));
  }
  fin[v] = ps2;
}
int findo(int x, int st, int en) {
  if (beg[x] <= st && fin[x] >= en) return x;
  for (int i = 17; i >= 0; i--) {
    if (P[x][i] == -1) continue;
    int w = P[x][i];
    if (beg[w] <= st && fin[w] >= en) continue;
    x = P[x][i];
  }
  return P[x][0];
}
int findo2(int x, int l) {
  for (int i = 17; i >= 0; i--) {
    if (P[x][i] == -1) continue;
    int w = P[x][i];
    if (L[w] <= l) continue;
    x = P[x][i];
  }
  return P[x][0];
}
int root[300000];
void dfs3(int v, int par) {
  root[v] = root[par];
  if (v > 1) {
    root[v] = update(root[v], 1, ps, suru[pos[v]]);
  }
  for (int i = 0; i < dc3[v].size(); i++) {
    dfs3(dc3[v][i].second.second, v);
  }
}
string pat, txt;
int lps[400];
void computeLPSArray() {
  int len = 0;
  lps[0] = 0;
  int i = 1;
  int M = pat.size();
  while (i < M) {
    if (pat[i] == pat[len]) {
      len++;
      lps[i] = len;
      i++;
    } else {
      if (len != 0) {
        len = lps[len - 1];
      } else {
        lps[i] = 0;
        i++;
      }
    }
  }
}
int kmpsear() {
  computeLPSArray();
  int M = pat.size();
  int N = txt.size();
  int i = 0;
  int j = 0, cnt = 0;
  while (i < N) {
    if (pat[j] == txt[i]) {
      j++;
      i++;
    }
    if (j == M) {
      cnt++;
      j = lps[j - 1];
    } else if (i < N && pat[j] != txt[i]) {
      if (j != 0)
        j = lps[j - 1];
      else
        i = i + 1;
    }
  }
  return cnt;
}
void dfs4(int v, int len, int tar) {
  if (len == 0 || v == tar) return;
  int lo = 0, high = dc3[v].size() - 1, ans = -1;
  while (lo <= high) {
    int mid = (lo + high) >> 1;
    if (dc3[v][mid].second.first >= beg[tar]) {
      ans = mid;
      high = mid - 1;
    } else
      lo = mid + 1;
  }
  if (ans == -1) return;
  char ch = dc3[v][ans].first;
  txt += ch;
  dfs4(dc3[v][ans].second.second, len - 1, tar);
}
int main() {
  int i, j, k, l, m, n;
  memset(P, -1, sizeof P);
  scanf("%d", &n);
  sa_init();
  for (int i = 1; i < n; i++) {
    scanf("%d%d%s", &l, &k, str);
    vc[l].push_back(k);
    vc[k].push_back(l);
    cost[l].push_back(str[0]);
    cost[k].push_back(str[0]);
  }
  dfs(1, -1, 0, 0, 'a');
  for (int j = 1; j <= 17; j++) {
    for (int i = 1; i <= n; i++) {
      if (P[i][j - 1] == -1) continue;
      P[i][j] = P[P[i][j - 1]][j - 1];
    }
  }
  for (int i = 1; i < sz; i++) {
    int w = st[i].link;
    tree2[w].push_back(i);
  }
  dfs2(0);
  dfs3(1, 0);
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y, ans = 0;
    char str[400];
    scanf("%d%d%s", &x, &y, str);
    if (x == y) {
      printf("0\n");
      continue;
    }
    int lc = findo(x, min(beg[x], beg[y]), max(beg[x], beg[y]));
    int g = strlen(str);
    int l = L[lc] + g - 1;
    if (l < L[x]) {
      int rot = 0, ck = 0;
      for (int i = g - 1; i >= 0; i--) {
        if (st[rot].next.find(str[i]) == st[rot].next.end()) {
          ck = -1;
          break;
        }
        rot = st[rot].next[str[i]];
      }
      if (ck != -1) {
        int f = findo2(x, l);
        ans += query(root[x], root[f], 1, ps, suru[rot], shes[rot]);
      }
    }
    if (l < L[y]) {
      int rot = 0, ck = 0;
      for (int i = 0; i < g; i++) {
        if (st[rot].next.find(str[i]) == st[rot].next.end()) {
          ck = -1;
          break;
        }
        rot = st[rot].next[str[i]];
      }
      if (ck != -1) {
        int f = findo2(y, l);
        ans += query(root[y], root[f], 1, ps, suru[rot], shes[rot]);
      }
    }
    pat.clear();
    pat += str;
    txt.clear();
    dfs4(lc, g - 1, x);
    reverse(txt.begin(), txt.end());
    dfs4(lc, g - 1, y);
    ans += kmpsear();
    printf("%d\n", ans);
  }
}

```