#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;
pair<lint, int> sor[10000005];
int n, q, Z;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
char pae[MAXN], buf[105];
vector<pair<int, int> > gph[MAXN];
void dfs(int x) {
  sz[x] = 1;
  for (auto &i : gph[x]) {
    dep[i.second] = dep[x] + 1;
    par[0][i.second] = x;
    pae[i.second] = i.first;
    gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(),
                             pair<int, int>(i.first, x)));
    dfs(i.second);
    sz[x] += sz[i.second];
  }
}
void hld(int x) {
  dfn[x] = ++piv;
  if (gph[x].empty()) return;
  chn[gph[x][0].second] = chn[x];
  hld(gph[x][0].second);
  for (int i = 1; i < gph[x].size(); i++) {
    chn[gph[x][i].second] = gph[x][i].second;
    hld(gph[x][i].second);
  }
}
int anc(int s, int v) {
  for (int i = 0; i < 17; i++) {
    if ((v >> i) & 1) s = par[i][s];
  }
  return s;
}
int lca(int s, int e) {
  if (dep[s] < dep[e]) swap(s, e);
  s = anc(s, dep[s] - dep[e]);
  for (int i = 16; i >= 0; i--) {
    if (par[i][s] != par[i][e]) {
      s = par[i][s];
      e = par[i][e];
    }
  }
  if (s != e) return par[0][s];
  return s;
}
lint getHash(string s) {
  lint h1 = 0, h2 = 0;
  for (auto &i : s) {
    h1 = ((h1 << 8) + h1) + i;
    h2 = ((h2 << 8) + h2) + i;
    h1 %= mod1;
    h2 %= mod2;
  }
  return h1 * mod2 + h2;
}
int kmp(string s, string t) {
  vector<int> fail(s.size() + 1);
  int p = 0;
  for (int i = 1; i < s.size(); i++) {
    while (p && s[i] != s[p]) p = fail[p];
    if (s[i] == s[p]) p++;
    fail[i + 1] = p;
  }
  p = 0;
  int ans = 0;
  for (int i = 0; i < t.size(); i++) {
    while (p && t[i] != s[p]) p = fail[p];
    if (t[i] == s[p]) p++;
    if (p == s.size()) {
      ans++;
      p = fail[p];
    }
  }
  return ans;
}
int up(int x, int y, lint h, int l) {
  int ans = 0;
  while (chn[x] != chn[y]) {
    int st = dfn[chn[x]];
    int ed = dfn[x];
    ans += upper_bound(sor, sor + Z, pair<lint, int>(h, ed)) -
           lower_bound(sor, sor + Z, pair<lint, int>(h, st));
    x = par[0][chn[x]];
  }
  int st = dfn[y] + 1;
  int ed = dfn[x];
  ans += upper_bound(sor, sor + Z, pair<lint, int>(h, ed)) -
         lower_bound(sor, sor + Z, pair<lint, int>(h, st));
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int s, e;
    scanf("%d %d %s", &s, &e, buf);
    gph[s].emplace_back(*buf, e);
    gph[e].emplace_back(*buf, s);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    sort(gph[i].begin(), gph[i].end(),
         [&](const pair<int, int> &a, const pair<int, int> &b) {
           return sz[a.second] > sz[b.second];
         });
  }
  for (int i = 1; i < 17; i++) {
    for (int j = 1; j <= n; j++) {
      par[i][j] = par[i - 1][par[i - 1][j]];
    }
  }
  chn[1] = 1;
  hld(1);
  for (int i = 2; i <= n; i++) {
    int ptr = i;
    lint h1 = 0, h2 = 0;
    for (int j = 1; j <= 100; j++) {
      if (ptr == 1) {
        continue;
      }
      h1 = ((h1 << 8) + h1) + pae[ptr];
      h2 = ((h2 << 8) + h2) + pae[ptr];
      h1 %= mod1;
      h2 %= mod2;
      sor[Z++] = pair<lint, int>(h1 * mod2 + h2, dfn[i]);
      ptr = par[0][ptr];
    }
  }
  sort(sor, sor + Z);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int s, e;
    scanf("%d %d %s", &s, &e, buf);
    int l = lca(s, e);
    int ns = anc(s, max(0, dep[s] - dep[l] - 100));
    int ne = anc(e, max(0, dep[e] - dep[l] - 100));
    string str = buf;
    lint fwHash = getHash(str);
    reverse(str.begin(), str.end());
    lint bwHash = getHash(str);
    reverse(str.begin(), str.end());
    int ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
    string L, R;
    while (ns != l) {
      L.push_back(pae[ns]);
      ns = par[0][ns];
    }
    while (ne != l) {
      R.push_back(pae[ne]);
      ne = par[0][ne];
    }
    reverse(R.begin(), R.end());
    ans += kmp(str, L + R);
    printf("%d\n", ans);
  }
}

### Prompt

Please create a solution in Cpp to the following problem:

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;
using lint = long long;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;
pair<lint, int> sor[10000005];
int n, q, Z;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
char pae[MAXN], buf[105];
vector<pair<int, int> > gph[MAXN];
void dfs(int x) {
  sz[x] = 1;
  for (auto &i : gph[x]) {
    dep[i.second] = dep[x] + 1;
    par[0][i.second] = x;
    pae[i.second] = i.first;
    gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(),
                             pair<int, int>(i.first, x)));
    dfs(i.second);
    sz[x] += sz[i.second];
  }
}
void hld(int x) {
  dfn[x] = ++piv;
  if (gph[x].empty()) return;
  chn[gph[x][0].second] = chn[x];
  hld(gph[x][0].second);
  for (int i = 1; i < gph[x].size(); i++) {
    chn[gph[x][i].second] = gph[x][i].second;
    hld(gph[x][i].second);
  }
}
int anc(int s, int v) {
  for (int i = 0; i < 17; i++) {
    if ((v >> i) & 1) s = par[i][s];
  }
  return s;
}
int lca(int s, int e) {
  if (dep[s] < dep[e]) swap(s, e);
  s = anc(s, dep[s] - dep[e]);
  for (int i = 16; i >= 0; i--) {
    if (par[i][s] != par[i][e]) {
      s = par[i][s];
      e = par[i][e];
    }
  }
  if (s != e) return par[0][s];
  return s;
}
lint getHash(string s) {
  lint h1 = 0, h2 = 0;
  for (auto &i : s) {
    h1 = ((h1 << 8) + h1) + i;
    h2 = ((h2 << 8) + h2) + i;
    h1 %= mod1;
    h2 %= mod2;
  }
  return h1 * mod2 + h2;
}
int kmp(string s, string t) {
  vector<int> fail(s.size() + 1);
  int p = 0;
  for (int i = 1; i < s.size(); i++) {
    while (p && s[i] != s[p]) p = fail[p];
    if (s[i] == s[p]) p++;
    fail[i + 1] = p;
  }
  p = 0;
  int ans = 0;
  for (int i = 0; i < t.size(); i++) {
    while (p && t[i] != s[p]) p = fail[p];
    if (t[i] == s[p]) p++;
    if (p == s.size()) {
      ans++;
      p = fail[p];
    }
  }
  return ans;
}
int up(int x, int y, lint h, int l) {
  int ans = 0;
  while (chn[x] != chn[y]) {
    int st = dfn[chn[x]];
    int ed = dfn[x];
    ans += upper_bound(sor, sor + Z, pair<lint, int>(h, ed)) -
           lower_bound(sor, sor + Z, pair<lint, int>(h, st));
    x = par[0][chn[x]];
  }
  int st = dfn[y] + 1;
  int ed = dfn[x];
  ans += upper_bound(sor, sor + Z, pair<lint, int>(h, ed)) -
         lower_bound(sor, sor + Z, pair<lint, int>(h, st));
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int s, e;
    scanf("%d %d %s", &s, &e, buf);
    gph[s].emplace_back(*buf, e);
    gph[e].emplace_back(*buf, s);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    sort(gph[i].begin(), gph[i].end(),
         [&](const pair<int, int> &a, const pair<int, int> &b) {
           return sz[a.second] > sz[b.second];
         });
  }
  for (int i = 1; i < 17; i++) {
    for (int j = 1; j <= n; j++) {
      par[i][j] = par[i - 1][par[i - 1][j]];
    }
  }
  chn[1] = 1;
  hld(1);
  for (int i = 2; i <= n; i++) {
    int ptr = i;
    lint h1 = 0, h2 = 0;
    for (int j = 1; j <= 100; j++) {
      if (ptr == 1) {
        continue;
      }
      h1 = ((h1 << 8) + h1) + pae[ptr];
      h2 = ((h2 << 8) + h2) + pae[ptr];
      h1 %= mod1;
      h2 %= mod2;
      sor[Z++] = pair<lint, int>(h1 * mod2 + h2, dfn[i]);
      ptr = par[0][ptr];
    }
  }
  sort(sor, sor + Z);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int s, e;
    scanf("%d %d %s", &s, &e, buf);
    int l = lca(s, e);
    int ns = anc(s, max(0, dep[s] - dep[l] - 100));
    int ne = anc(e, max(0, dep[e] - dep[l] - 100));
    string str = buf;
    lint fwHash = getHash(str);
    reverse(str.begin(), str.end());
    lint bwHash = getHash(str);
    reverse(str.begin(), str.end());
    int ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
    string L, R;
    while (ns != l) {
      L.push_back(pae[ns]);
      ns = par[0][ns];
    }
    while (ne != l) {
      R.push_back(pae[ne]);
      ne = par[0][ne];
    }
    reverse(R.begin(), R.end());
    ans += kmp(str, L + R);
    printf("%d\n", ans);
  }
}

```