#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 60;
const int mods = 998244353;
const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       !isalpha(c) && c != EOF;
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
inline void reads(char *st) {
  char c;
  int n = 0;
  getc(st[++n]);
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       isalpha(c);
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    st[++n] = c;
  st[++n] = '\0';
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::getc;
using FastIO ::print;
using FastIO ::putc;
using FastIO ::read;
using FastIO ::reads;
const int SZ = 127;
struct hnode {
  unsigned long long x;
  hnode() {}
  hnode(unsigned long long x) : x(x){};
  friend hnode operator+(hnode a, hnode b) { return hnode(a.x + b.x); }
  friend hnode operator-(hnode a, hnode b) { return hnode(a.x - b.x); }
  friend hnode operator*(hnode a, hnode b) { return hnode(a.x * b.x); }
  friend hnode operator*(hnode a, unsigned long long b) {
    return hnode(a.x * b);
  }
  friend bool operator<(hnode a, hnode b) { return a.x < b.x; }
  friend bool operator==(hnode a, hnode b) { return a.x == b.x; }
} H[MAXN], h[MAXN], _h[MAXN], pw[MAXN];
hnode add(hnode x, int y) { return x * SZ + hnode(y); }
unordered_map<unsigned long long, int> Map;
char S[MAXN], stk[MAXN], frm[MAXN], st[MAXN];
vector<int> Vu[MAXN], Vd[MAXN];
vector<pair<int, int> > e[MAXN];
int dep[MAXN], fa[MAXN][20], Len[MAXN], Fa[MAXN], Ans[MAXN], nxt[MAXN],
    LSTu[MAXN], LSTd[MAXN], n, m;
void dfs(int x, int father) {
  fa[x][0] = father, dep[x] = dep[father] + 1;
  for (int i = 1; i < 20; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
  for (auto v : e[x]) {
    if (v.first == father) continue;
    frm[v.first] = v.second, H[v.first] = add(H[x], v.second), dfs(v.first, x);
  }
}
int getlca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 19; i >= 0; --i)
    if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
  if (u == v) return u;
  for (int i = 19; i >= 0; --i)
    if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
void solve(int u, int v, char *st, int id) {
  Len[id] = strlen(st + 1);
  for (int i = 1; i <= Len[id]; ++i) h[id] = add(h[id], st[i]);
  for (int i = Len[id]; i >= 1; --i) _h[id] = add(_h[id], st[i]);
  int lca = getlca(u, v), uu = u, vv = v;
  for (int i = 19; i >= 0; --i)
    if (fa[uu][i] && dep[fa[uu][i]] >= dep[lca] + Len[id] - 1) uu = fa[uu][i];
  for (int i = 19; i >= 0; --i)
    if (fa[vv][i] && dep[fa[vv][i]] >= dep[lca] + Len[id] - 1) vv = fa[vv][i];
  Vu[u].push_back(id), Vd[v].push_back(id), Vu[uu].push_back(-id),
      Vd[vv].push_back(-id);
  if (Len[id] < 2) return;
  int LEN = 0, top = 0;
  for (int i = uu; i != lca; i = fa[i][0]) S[++LEN] = frm[i];
  for (int i = vv; i != lca; i = fa[i][0]) stk[top++] = frm[i];
  while (top) S[++LEN] = stk[--top];
  for (int i = 2, j = 0; i <= Len[id]; ++i) {
    while (j && st[j + 1] != st[i]) j = nxt[j];
    if (st[j + 1] == st[i]) ++j;
    nxt[i] = j;
  }
  for (int i = 1, j = 0; i <= LEN; ++i) {
    while (j && st[j + 1] != S[i]) j = nxt[j];
    if (st[j + 1] == S[i] && (++j) == Len[id]) ++Ans[id], j = nxt[j];
  }
}
void getans(int x, int len) {
  Fa[x] = fa[Fa[x]][0];
  ++Map[(H[x] - H[Fa[x]] * pw[len]).x];
  for (auto v : e[x]) {
    if (v.first == fa[x][0]) continue;
    getans(v.first, len);
  }
  if (dep[x] > len) {
    for (int &i = LSTd[x]; i < (int)Vd[x].size() && Len[abs(Vd[x][i])] == len;
         ++i) {
      int v = Vd[x][i], t = abs(v);
      if (Map.find(h[t].x) == Map.end()) continue;
      Ans[t] += Map[h[t].x] * (v > 0 ? 1 : -1);
    }
    for (int &i = LSTu[x]; i < (int)Vu[x].size() && Len[abs(Vu[x][i])] == len;
         ++i) {
      int v = Vu[x][i], t = abs(v);
      if (Map.find(_h[t].x) == Map.end()) continue;
      Ans[t] += Map[_h[t].x] * (v > 0 ? 1 : -1);
    }
  }
  --Map[(H[x] - H[Fa[x]] * pw[len]).x];
}
signed main() {
  read(n), pw[0] = hnode(1);
  for (int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * SZ;
  for (int i = 1; i < n; ++i) {
    int u, v;
    char c;
    read(u), read(v), getc(c);
    e[u].push_back(make_pair(v, c)), e[v].push_back(make_pair(u, c));
  }
  dfs(1, 0);
  read(m);
  for (int i = 1, u, v; i <= m; ++i)
    read(u), read(v), reads(st), solve(u, v, st, i);
  for (int i = 1; i <= n; ++i) Fa[i] = i;
  for (int i = 1; i <= n; ++i) {
    sort(Vu[i].begin(), Vu[i].end(),
         [&](int x, int y) { return Len[abs(x)] < Len[abs(y)]; });
    sort(Vd[i].begin(), Vd[i].end(),
         [&](int x, int y) { return Len[abs(x)] < Len[abs(y)]; });
  }
  for (int i = 1; i <= 100; ++i) Map.clear(), getans(1, i);
  for (int i = 1; i <= m; ++i) print(Ans[i]), putc('\n');
  return 0;
}

### Prompt

Develop a solution in cpp 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;
template <typename T>
inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 60;
const int mods = 998244353;
const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       !isalpha(c) && c != EOF;
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
inline void reads(char *st) {
  char c;
  int n = 0;
  getc(st[++n]);
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       isalpha(c);
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    st[++n] = c;
  st[++n] = '\0';
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::getc;
using FastIO ::print;
using FastIO ::putc;
using FastIO ::read;
using FastIO ::reads;
const int SZ = 127;
struct hnode {
  unsigned long long x;
  hnode() {}
  hnode(unsigned long long x) : x(x){};
  friend hnode operator+(hnode a, hnode b) { return hnode(a.x + b.x); }
  friend hnode operator-(hnode a, hnode b) { return hnode(a.x - b.x); }
  friend hnode operator*(hnode a, hnode b) { return hnode(a.x * b.x); }
  friend hnode operator*(hnode a, unsigned long long b) {
    return hnode(a.x * b);
  }
  friend bool operator<(hnode a, hnode b) { return a.x < b.x; }
  friend bool operator==(hnode a, hnode b) { return a.x == b.x; }
} H[MAXN], h[MAXN], _h[MAXN], pw[MAXN];
hnode add(hnode x, int y) { return x * SZ + hnode(y); }
unordered_map<unsigned long long, int> Map;
char S[MAXN], stk[MAXN], frm[MAXN], st[MAXN];
vector<int> Vu[MAXN], Vd[MAXN];
vector<pair<int, int> > e[MAXN];
int dep[MAXN], fa[MAXN][20], Len[MAXN], Fa[MAXN], Ans[MAXN], nxt[MAXN],
    LSTu[MAXN], LSTd[MAXN], n, m;
void dfs(int x, int father) {
  fa[x][0] = father, dep[x] = dep[father] + 1;
  for (int i = 1; i < 20; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
  for (auto v : e[x]) {
    if (v.first == father) continue;
    frm[v.first] = v.second, H[v.first] = add(H[x], v.second), dfs(v.first, x);
  }
}
int getlca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 19; i >= 0; --i)
    if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
  if (u == v) return u;
  for (int i = 19; i >= 0; --i)
    if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
void solve(int u, int v, char *st, int id) {
  Len[id] = strlen(st + 1);
  for (int i = 1; i <= Len[id]; ++i) h[id] = add(h[id], st[i]);
  for (int i = Len[id]; i >= 1; --i) _h[id] = add(_h[id], st[i]);
  int lca = getlca(u, v), uu = u, vv = v;
  for (int i = 19; i >= 0; --i)
    if (fa[uu][i] && dep[fa[uu][i]] >= dep[lca] + Len[id] - 1) uu = fa[uu][i];
  for (int i = 19; i >= 0; --i)
    if (fa[vv][i] && dep[fa[vv][i]] >= dep[lca] + Len[id] - 1) vv = fa[vv][i];
  Vu[u].push_back(id), Vd[v].push_back(id), Vu[uu].push_back(-id),
      Vd[vv].push_back(-id);
  if (Len[id] < 2) return;
  int LEN = 0, top = 0;
  for (int i = uu; i != lca; i = fa[i][0]) S[++LEN] = frm[i];
  for (int i = vv; i != lca; i = fa[i][0]) stk[top++] = frm[i];
  while (top) S[++LEN] = stk[--top];
  for (int i = 2, j = 0; i <= Len[id]; ++i) {
    while (j && st[j + 1] != st[i]) j = nxt[j];
    if (st[j + 1] == st[i]) ++j;
    nxt[i] = j;
  }
  for (int i = 1, j = 0; i <= LEN; ++i) {
    while (j && st[j + 1] != S[i]) j = nxt[j];
    if (st[j + 1] == S[i] && (++j) == Len[id]) ++Ans[id], j = nxt[j];
  }
}
void getans(int x, int len) {
  Fa[x] = fa[Fa[x]][0];
  ++Map[(H[x] - H[Fa[x]] * pw[len]).x];
  for (auto v : e[x]) {
    if (v.first == fa[x][0]) continue;
    getans(v.first, len);
  }
  if (dep[x] > len) {
    for (int &i = LSTd[x]; i < (int)Vd[x].size() && Len[abs(Vd[x][i])] == len;
         ++i) {
      int v = Vd[x][i], t = abs(v);
      if (Map.find(h[t].x) == Map.end()) continue;
      Ans[t] += Map[h[t].x] * (v > 0 ? 1 : -1);
    }
    for (int &i = LSTu[x]; i < (int)Vu[x].size() && Len[abs(Vu[x][i])] == len;
         ++i) {
      int v = Vu[x][i], t = abs(v);
      if (Map.find(_h[t].x) == Map.end()) continue;
      Ans[t] += Map[_h[t].x] * (v > 0 ? 1 : -1);
    }
  }
  --Map[(H[x] - H[Fa[x]] * pw[len]).x];
}
signed main() {
  read(n), pw[0] = hnode(1);
  for (int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * SZ;
  for (int i = 1; i < n; ++i) {
    int u, v;
    char c;
    read(u), read(v), getc(c);
    e[u].push_back(make_pair(v, c)), e[v].push_back(make_pair(u, c));
  }
  dfs(1, 0);
  read(m);
  for (int i = 1, u, v; i <= m; ++i)
    read(u), read(v), reads(st), solve(u, v, st, i);
  for (int i = 1; i <= n; ++i) Fa[i] = i;
  for (int i = 1; i <= n; ++i) {
    sort(Vu[i].begin(), Vu[i].end(),
         [&](int x, int y) { return Len[abs(x)] < Len[abs(y)]; });
    sort(Vd[i].begin(), Vd[i].end(),
         [&](int x, int y) { return Len[abs(x)] < Len[abs(y)]; });
  }
  for (int i = 1; i <= 100; ++i) Map.clear(), getans(1, i);
  for (int i = 1; i <= m; ++i) print(Ans[i]), putc('\n');
  return 0;
}

```