#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const char lf = '\n';
const int _ = 100007, __ = _ << 1, P = 100, _P = P + 3, lglg = 18;
const unsigned long long bas = 1145141;
int to[__], ne[__], ev[__], he[_] = {0}, ecnt = 1;
inline void adde(int a, int b, int c) {
  to[++ecnt] = b, ev[ecnt] = c, ne[ecnt] = he[a], he[a] = ecnt;
}
inline void addde(int a, int b, int c) { adde(a, b, c), adde(b, a, c); }
int n, fa[_] = {0}, dep[_] = {0}, pfa[_][lglg + 1] = {0};
unsigned long long val[_][_P] = {0};
void dfs(int x, int ff) {
  fa[x] = ff, dep[x] = dep[ff] + 1, pfa[x][0] = fa[x];
  for (int i = 2, j = ff; i <= P; i++, j = fa[j])
    val[x][i] = val[x][i - 1] * bas + val[j][1];
  for (int i = 1; i <= lglg; i++) pfa[x][i] = pfa[pfa[x][i - 1]][i - 1];
  for (int i = he[x]; i; i = ne[i]) {
    int b = to[i];
    if (b == ff) continue;
    val[b][1] = ev[i], dfs(b, x);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int i = lglg; i >= 0; i--)
    if (dep[pfa[a][i]] >= dep[b]) a = pfa[a][i];
  if (a == b) return a;
  for (int i = lglg; i >= 0; i--)
    if (pfa[a][i] != pfa[b][i]) a = pfa[a][i], b = pfa[b][i];
  return fa[a];
}
int jmp(int a, int d) {
  for (int i = lglg; i >= 0; i--)
    if ((d >> i) & 1) a = pfa[a][i];
  return a;
}
struct jt {
  static const int _t = _ * 70;
  int lc[_t], rc[_t], t[_t], tcnt;
  int alloc(int fr = 0) {
    int x = ++tcnt;
    lc[x] = lc[fr], rc[x] = rc[fr], t[x] = t[fr];
    return x;
  }
  void change(int &x, int y, int l, int r, int tar) {
    x = alloc(y), t[x]++;
    if (l == r) return;
    if (tar <= ((l + r) >> 1))
      change(lc[x], lc[y], l, ((l + r) >> 1), tar);
    else
      change(rc[x], rc[y], ((l + r) >> 1) + 1, r, tar);
  }
  int finder(int x, int y, int l, int r, int tar) {
    if (l == r) return t[x] - t[y];
    if (tar <= ((l + r) >> 1))
      return finder(lc[x], lc[y], l, ((l + r) >> 1), tar);
    return finder(rc[x], rc[y], ((l + r) >> 1) + 1, r, tar);
  }
  jt() {
    memset(lc, 0, sizeof(lc));
    memset(rc, 0, sizeof(rc));
    memset(t, 0, sizeof(t));
    tcnt = 0;
  }
} t;
int rt[_] = {0};
struct cxk {
  int id, a, b;
  string s;
  cxk(int id_ = 0, int a_ = 0, int b_ = 0, string s_ = "") {
    id = id_, a = a_, b = b_, s = s_;
  }
};
vector<cxk> dlq[_P];
int m;
unsigned long long ha[_] = {0};
int lha = 0;
int hashie(unsigned long long x) {
  return lower_bound(ha + 1, ha + lha + 1, x) - ha;
}
int hloc[_] = {0};
void dfsplant(int x) {
  t.change(rt[x], rt[fa[x]], 1, lha, hloc[x]);
  for (int i = he[x]; i; i = ne[i])
    if (to[i] != fa[x]) dfsplant(to[i]);
}
int ans[_] = {0};
unsigned long long hs[_] = {0}, rhs[_] = {0};
int finder(int a, int b, string s) {
  int ans = 0, c = lca(a, b), len = s.size();
  for (int i = 1; i <= len; i++) hs[i] = hs[i - 1] * bas + s[i - 1];
  for (int i = 1; i <= len; i++) rhs[i] = rhs[i - 1] * bas + s[len - i];
  if (dep[a] - dep[c] >= len) {
    int loc = hashie(hs[len]);
    if (loc <= lha && ha[loc] == hs[len])
      ans +=
          t.finder(rt[a], rt[jmp(a, dep[a] - dep[c] - len + 1)], 1, lha, loc);
  }
  if (dep[b] - dep[c] >= len) {
    int loc = hashie(rhs[len]);
    if (loc <= lha && ha[loc] == rhs[len])
      ans +=
          t.finder(rt[b], rt[jmp(b, dep[b] - dep[c] - len + 1)], 1, lha, loc);
  }
  for (int i = jmp(a, max(0, dep[a] - dep[c] - len + 1)); i != c; i = fa[i]) {
    int temp = dep[b] - dep[c] - len + dep[i] - dep[c];
    if (temp < 0) continue;
    int j = jmp(b, temp);
    if (val[i][dep[i] - dep[c]] == hs[dep[i] - dep[c]] &&
        val[j][dep[j] - dep[c]] == rhs[dep[j] - dep[c]])
      ans++;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c, addde(a, b, c);
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    string s;
    cin >> a >> b >> s, dlq[s.size()].emplace_back(i, a, b, s);
  }
  dfs(1, 0);
  for (int now = 1; now <= P; now++) {
    if (dlq[now].empty()) continue;
    lha = 0;
    for (int i = 1; i <= n; i++) ha[++lha] = val[i][now];
    sort(ha + 1, ha + lha + 1), lha = unique(ha + 1, ha + lha + 1) - ha - 1;
    for (int i = 1; i <= n; i++) hloc[i] = hashie(val[i][now]);
    t.tcnt = 0, dfsplant(1);
    for (auto i : dlq[now]) ans[i.id] = finder(i.a, i.b, i.s);
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << lf;
  return 0;
}

### Prompt

Create a solution in cpp for 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;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const char lf = '\n';
const int _ = 100007, __ = _ << 1, P = 100, _P = P + 3, lglg = 18;
const unsigned long long bas = 1145141;
int to[__], ne[__], ev[__], he[_] = {0}, ecnt = 1;
inline void adde(int a, int b, int c) {
  to[++ecnt] = b, ev[ecnt] = c, ne[ecnt] = he[a], he[a] = ecnt;
}
inline void addde(int a, int b, int c) { adde(a, b, c), adde(b, a, c); }
int n, fa[_] = {0}, dep[_] = {0}, pfa[_][lglg + 1] = {0};
unsigned long long val[_][_P] = {0};
void dfs(int x, int ff) {
  fa[x] = ff, dep[x] = dep[ff] + 1, pfa[x][0] = fa[x];
  for (int i = 2, j = ff; i <= P; i++, j = fa[j])
    val[x][i] = val[x][i - 1] * bas + val[j][1];
  for (int i = 1; i <= lglg; i++) pfa[x][i] = pfa[pfa[x][i - 1]][i - 1];
  for (int i = he[x]; i; i = ne[i]) {
    int b = to[i];
    if (b == ff) continue;
    val[b][1] = ev[i], dfs(b, x);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int i = lglg; i >= 0; i--)
    if (dep[pfa[a][i]] >= dep[b]) a = pfa[a][i];
  if (a == b) return a;
  for (int i = lglg; i >= 0; i--)
    if (pfa[a][i] != pfa[b][i]) a = pfa[a][i], b = pfa[b][i];
  return fa[a];
}
int jmp(int a, int d) {
  for (int i = lglg; i >= 0; i--)
    if ((d >> i) & 1) a = pfa[a][i];
  return a;
}
struct jt {
  static const int _t = _ * 70;
  int lc[_t], rc[_t], t[_t], tcnt;
  int alloc(int fr = 0) {
    int x = ++tcnt;
    lc[x] = lc[fr], rc[x] = rc[fr], t[x] = t[fr];
    return x;
  }
  void change(int &x, int y, int l, int r, int tar) {
    x = alloc(y), t[x]++;
    if (l == r) return;
    if (tar <= ((l + r) >> 1))
      change(lc[x], lc[y], l, ((l + r) >> 1), tar);
    else
      change(rc[x], rc[y], ((l + r) >> 1) + 1, r, tar);
  }
  int finder(int x, int y, int l, int r, int tar) {
    if (l == r) return t[x] - t[y];
    if (tar <= ((l + r) >> 1))
      return finder(lc[x], lc[y], l, ((l + r) >> 1), tar);
    return finder(rc[x], rc[y], ((l + r) >> 1) + 1, r, tar);
  }
  jt() {
    memset(lc, 0, sizeof(lc));
    memset(rc, 0, sizeof(rc));
    memset(t, 0, sizeof(t));
    tcnt = 0;
  }
} t;
int rt[_] = {0};
struct cxk {
  int id, a, b;
  string s;
  cxk(int id_ = 0, int a_ = 0, int b_ = 0, string s_ = "") {
    id = id_, a = a_, b = b_, s = s_;
  }
};
vector<cxk> dlq[_P];
int m;
unsigned long long ha[_] = {0};
int lha = 0;
int hashie(unsigned long long x) {
  return lower_bound(ha + 1, ha + lha + 1, x) - ha;
}
int hloc[_] = {0};
void dfsplant(int x) {
  t.change(rt[x], rt[fa[x]], 1, lha, hloc[x]);
  for (int i = he[x]; i; i = ne[i])
    if (to[i] != fa[x]) dfsplant(to[i]);
}
int ans[_] = {0};
unsigned long long hs[_] = {0}, rhs[_] = {0};
int finder(int a, int b, string s) {
  int ans = 0, c = lca(a, b), len = s.size();
  for (int i = 1; i <= len; i++) hs[i] = hs[i - 1] * bas + s[i - 1];
  for (int i = 1; i <= len; i++) rhs[i] = rhs[i - 1] * bas + s[len - i];
  if (dep[a] - dep[c] >= len) {
    int loc = hashie(hs[len]);
    if (loc <= lha && ha[loc] == hs[len])
      ans +=
          t.finder(rt[a], rt[jmp(a, dep[a] - dep[c] - len + 1)], 1, lha, loc);
  }
  if (dep[b] - dep[c] >= len) {
    int loc = hashie(rhs[len]);
    if (loc <= lha && ha[loc] == rhs[len])
      ans +=
          t.finder(rt[b], rt[jmp(b, dep[b] - dep[c] - len + 1)], 1, lha, loc);
  }
  for (int i = jmp(a, max(0, dep[a] - dep[c] - len + 1)); i != c; i = fa[i]) {
    int temp = dep[b] - dep[c] - len + dep[i] - dep[c];
    if (temp < 0) continue;
    int j = jmp(b, temp);
    if (val[i][dep[i] - dep[c]] == hs[dep[i] - dep[c]] &&
        val[j][dep[j] - dep[c]] == rhs[dep[j] - dep[c]])
      ans++;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c, addde(a, b, c);
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    string s;
    cin >> a >> b >> s, dlq[s.size()].emplace_back(i, a, b, s);
  }
  dfs(1, 0);
  for (int now = 1; now <= P; now++) {
    if (dlq[now].empty()) continue;
    lha = 0;
    for (int i = 1; i <= n; i++) ha[++lha] = val[i][now];
    sort(ha + 1, ha + lha + 1), lha = unique(ha + 1, ha + lha + 1) - ha - 1;
    for (int i = 1; i <= n; i++) hloc[i] = hashie(val[i][now]);
    t.tcnt = 0, dfsplant(1);
    for (auto i : dlq[now]) ans[i.id] = finder(i.a, i.b, i.s);
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << lf;
  return 0;
}

```