#include <bits/stdc++.h>
using namespace std;
const unsigned long long mod = (1ull << 61) - 1, base = 233;
inline unsigned long long mult(unsigned long long a, unsigned long long b) {
  unsigned long long l1 = (unsigned int)a, h1 = a >> 32, l2 = (unsigned int)b,
                     h2 = b >> 32;
  unsigned long long l = l1 * l2, m = l1 * h2 + l2 * h1, h = h1 * h2;
  unsigned long long ret =
      (l & mod) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
  ret = (ret & mod) + (ret >> 61);
  ret = (ret & mod) + (ret >> 61);
  return ret - 1;
}
inline unsigned long long add(unsigned long long a, unsigned long long b) {
  if ((a += b) >= mod) a -= mod;
  return a;
}
inline unsigned long long dec(unsigned long long a, unsigned long long b) {
  return add(a, mod - b);
}
unsigned long long hs[100010][110], pw[100010];
int n, Q, st[100010][17], dep[100010];
char fc[100010];
inline int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 16; i >= 0; i--)
    if (dep[st[x][i]] >= dep[y]) x = st[x][i];
  if (x == y) return x;
  for (int i = 16; i >= 0; i--)
    if (st[x][i] != st[y][i]) x = st[x][i], y = st[y][i];
  return st[x][0];
}
inline int jump(int p, int t) {
  for (int i = 16; i >= 0; i--)
    if ((t >> i) & 1) p = st[p][i];
  return p;
}
int ans[100010];
vector<pair<int, int> > q[100010];
vector<pair<int, char> > v[100010];
unordered_map<unsigned long long, int> cnt;
string s[200010];
unsigned long long vl[200010];
int len[200010];
void dfs1(int np, int fath, char ic) {
  dep[np] = dep[fath] + 1;
  st[np][0] = fath;
  fc[np] = ic;
  for (int i = 1; i < 17; i++) st[np][i] = st[st[np][i - 1]][i - 1];
  hs[np][0] = 0;
  for (int i = 1; i <= 100 && i < dep[np]; i++)
    hs[np][i] = add(mult(ic - 'a' + 1, pw[i - 1]), hs[fath][i - 1]);
  for (auto &x : v[np]) {
    if (x.first == fath) continue;
    dfs1(x.first, np, x.second);
  }
}
inline unsigned long long gethash(const string &s) {
  unsigned long long h = 0;
  for (int i = 0; i < s.size(); i++) h = add(mult(h, base), s[i] - 'a' + 1);
  return h;
}
int cmp(const vector<char> &a, unsigned long long key, int len) {
  if (a.size() < len) return 0;
  unsigned long long h = 0;
  int ret = 0;
  for (int i = 0; i < len - 1; i++) h = add(mult(h, base), a[i] - 'a' + 1);
  for (int i = len - 1; i < a.size(); i++) {
    h = add(mult(h, base), a[i] - 'a' + 1);
    ret += (h == key);
    h = dec(h, mult(pw[len - 1], a[i - len + 1] - 'a' + 1));
  }
  return ret;
}
void dfs2(int np, int fath, int l) {
  if (dep[np] > l) cnt[hs[np][l]]++;
  for (auto &x : q[np]) {
    if (len[x.first] == l) {
      if (x.second == 1)
        ans[x.first >> 1] += cnt[vl[x.first]];
      else
        ans[x.first >> 1] -= cnt[vl[x.first]];
    }
  }
  for (auto &x : v[np])
    if (x.first != fath) dfs2(x.first, np, l);
  if (dep[np] > l) cnt[hs[np][l]]--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b;
    char s[5];
    scanf("%d%d%s", &a, &b, s);
    v[a].push_back(make_pair(b, s[0]));
    v[b].push_back(make_pair(a, s[0]));
  }
  pw[0] = 1;
  for (int i = 1; i <= 100; i++) pw[i] = mult(pw[i - 1], base);
  dfs1(1, 0, 'a');
  scanf("%d", &Q);
  for (int i = 1, a, b; i <= Q; i++) {
    scanf("%d%d", &a, &b);
    cin >> s[i * 2];
    int cl = s[i * 2].size();
    s[i * 2 + 1] = s[i * 2];
    reverse(s[i * 2 + 1].begin(), s[i * 2 + 1].end());
    vl[i * 2] = gethash(s[i * 2]);
    vl[i * 2 + 1] = gethash(s[i * 2 + 1]);
    len[i * 2] = s[i * 2].size();
    len[i * 2 + 1] = s[i * 2 + 1].size();
    int L = lca(a, b);
    vector<char> v1, v2;
    int p;
    if (dep[a] - dep[L] < cl - 1)
      p = a;
    else
      p = jump(a, dep[a] - dep[L] - (cl - 1));
    if (a != p) {
      q[a].push_back(make_pair(i * 2, 1));
      q[p].push_back(make_pair(i * 2, -1));
    }
    while (p != L) v1.push_back(fc[p]), p = st[p][0];
    if (dep[b] - dep[L] < cl)
      p = b;
    else
      p = jump(b, dep[b] - dep[L] - (cl - 1));
    if (b != p) {
      q[b].push_back(make_pair(i * 2 + 1, 1));
      q[p].push_back(make_pair(i * 2 + 1, -1));
    }
    while (p != L) v2.push_back(fc[p]), p = st[p][0];
    reverse(v2.begin(), v2.end());
    v1.insert(v1.end(), v2.begin(), v2.end());
    ans[i] = cmp(v1, vl[i << 1], s[i * 2].size());
  }
  for (int i = 1; i <= 100; i++) {
    cnt.clear();
    dfs2(1, 0, i);
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}

### Prompt

Generate a CPP solution 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;
const unsigned long long mod = (1ull << 61) - 1, base = 233;
inline unsigned long long mult(unsigned long long a, unsigned long long b) {
  unsigned long long l1 = (unsigned int)a, h1 = a >> 32, l2 = (unsigned int)b,
                     h2 = b >> 32;
  unsigned long long l = l1 * l2, m = l1 * h2 + l2 * h1, h = h1 * h2;
  unsigned long long ret =
      (l & mod) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
  ret = (ret & mod) + (ret >> 61);
  ret = (ret & mod) + (ret >> 61);
  return ret - 1;
}
inline unsigned long long add(unsigned long long a, unsigned long long b) {
  if ((a += b) >= mod) a -= mod;
  return a;
}
inline unsigned long long dec(unsigned long long a, unsigned long long b) {
  return add(a, mod - b);
}
unsigned long long hs[100010][110], pw[100010];
int n, Q, st[100010][17], dep[100010];
char fc[100010];
inline int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 16; i >= 0; i--)
    if (dep[st[x][i]] >= dep[y]) x = st[x][i];
  if (x == y) return x;
  for (int i = 16; i >= 0; i--)
    if (st[x][i] != st[y][i]) x = st[x][i], y = st[y][i];
  return st[x][0];
}
inline int jump(int p, int t) {
  for (int i = 16; i >= 0; i--)
    if ((t >> i) & 1) p = st[p][i];
  return p;
}
int ans[100010];
vector<pair<int, int> > q[100010];
vector<pair<int, char> > v[100010];
unordered_map<unsigned long long, int> cnt;
string s[200010];
unsigned long long vl[200010];
int len[200010];
void dfs1(int np, int fath, char ic) {
  dep[np] = dep[fath] + 1;
  st[np][0] = fath;
  fc[np] = ic;
  for (int i = 1; i < 17; i++) st[np][i] = st[st[np][i - 1]][i - 1];
  hs[np][0] = 0;
  for (int i = 1; i <= 100 && i < dep[np]; i++)
    hs[np][i] = add(mult(ic - 'a' + 1, pw[i - 1]), hs[fath][i - 1]);
  for (auto &x : v[np]) {
    if (x.first == fath) continue;
    dfs1(x.first, np, x.second);
  }
}
inline unsigned long long gethash(const string &s) {
  unsigned long long h = 0;
  for (int i = 0; i < s.size(); i++) h = add(mult(h, base), s[i] - 'a' + 1);
  return h;
}
int cmp(const vector<char> &a, unsigned long long key, int len) {
  if (a.size() < len) return 0;
  unsigned long long h = 0;
  int ret = 0;
  for (int i = 0; i < len - 1; i++) h = add(mult(h, base), a[i] - 'a' + 1);
  for (int i = len - 1; i < a.size(); i++) {
    h = add(mult(h, base), a[i] - 'a' + 1);
    ret += (h == key);
    h = dec(h, mult(pw[len - 1], a[i - len + 1] - 'a' + 1));
  }
  return ret;
}
void dfs2(int np, int fath, int l) {
  if (dep[np] > l) cnt[hs[np][l]]++;
  for (auto &x : q[np]) {
    if (len[x.first] == l) {
      if (x.second == 1)
        ans[x.first >> 1] += cnt[vl[x.first]];
      else
        ans[x.first >> 1] -= cnt[vl[x.first]];
    }
  }
  for (auto &x : v[np])
    if (x.first != fath) dfs2(x.first, np, l);
  if (dep[np] > l) cnt[hs[np][l]]--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b;
    char s[5];
    scanf("%d%d%s", &a, &b, s);
    v[a].push_back(make_pair(b, s[0]));
    v[b].push_back(make_pair(a, s[0]));
  }
  pw[0] = 1;
  for (int i = 1; i <= 100; i++) pw[i] = mult(pw[i - 1], base);
  dfs1(1, 0, 'a');
  scanf("%d", &Q);
  for (int i = 1, a, b; i <= Q; i++) {
    scanf("%d%d", &a, &b);
    cin >> s[i * 2];
    int cl = s[i * 2].size();
    s[i * 2 + 1] = s[i * 2];
    reverse(s[i * 2 + 1].begin(), s[i * 2 + 1].end());
    vl[i * 2] = gethash(s[i * 2]);
    vl[i * 2 + 1] = gethash(s[i * 2 + 1]);
    len[i * 2] = s[i * 2].size();
    len[i * 2 + 1] = s[i * 2 + 1].size();
    int L = lca(a, b);
    vector<char> v1, v2;
    int p;
    if (dep[a] - dep[L] < cl - 1)
      p = a;
    else
      p = jump(a, dep[a] - dep[L] - (cl - 1));
    if (a != p) {
      q[a].push_back(make_pair(i * 2, 1));
      q[p].push_back(make_pair(i * 2, -1));
    }
    while (p != L) v1.push_back(fc[p]), p = st[p][0];
    if (dep[b] - dep[L] < cl)
      p = b;
    else
      p = jump(b, dep[b] - dep[L] - (cl - 1));
    if (b != p) {
      q[b].push_back(make_pair(i * 2 + 1, 1));
      q[p].push_back(make_pair(i * 2 + 1, -1));
    }
    while (p != L) v2.push_back(fc[p]), p = st[p][0];
    reverse(v2.begin(), v2.end());
    v1.insert(v1.end(), v2.begin(), v2.end());
    ans[i] = cmp(v1, vl[i << 1], s[i * 2].size());
  }
  for (int i = 1; i <= 100; i++) {
    cnt.clear();
    dfs2(1, 0, i);
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}

```