#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &item) {
  out << '(' << item.first << ", " << item.second << ')';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const auto &item : v) out << item << ' ';
  return out;
}
const int S = 105;
const int B = 37;
const int LOGN = 18;
const int N = 100010;
const int Q = 100010;
struct Event {
  ull h;
  int len, mul, id;
  Event(ull h, int len, int mul, int id) : h(h), len(len), mul(mul), id(id) {}
  bool operator<(const Event &o) const { return len < o.len; }
};
int h[N], ans[Q], ptr[N];
int fth[LOGN][N];
char fthCh[N], path[N];
ull pwb[N];
vector<pair<int, ull>> stk;
vector<pair<int, char>> adj[N];
vector<Event> ev[N];
unordered_map<ull, int> hashes;
ull getHash(const string &s) {
  ull hsh = 0;
  for (auto ch : s) hsh = hsh * B + ch - 'a';
  return hsh;
}
void dfs(int v, int p = -1) {
  for (auto [u, ch] : adj[v]) {
    if (u == p) continue;
    h[u] = 1 + h[v];
    fth[0][u] = v;
    fthCh[u] = ch;
    dfs(u, v);
  }
}
void solve(int v, ull h, int len, int p = -1) {
  if (len <= stk.size()) {
    ull hsh = h - stk[stk.size() - len].second * pwb[len];
    ++hashes[hsh];
  }
  while (ptr[v] < ev[v].size() && ev[v][ptr[v]].len == len) {
    const auto &[hsh, len, mul, id] = ev[v][ptr[v]];
    ans[id] += mul * hashes[hsh];
    ++ptr[v];
  }
  stk.emplace_back(v, h);
  for (auto [u, ch] : adj[v]) {
    if (u == p) continue;
    solve(u, h * B + ch - 'a', len, v);
  }
  stk.pop_back();
  if (len <= stk.size()) {
    ull hsh = h - stk[stk.size() - len].second * pwb[len];
    --hashes[hsh];
  }
}
void prepareLCA(int n) {
  dfs(1);
  for (int i = 1; i < LOGN; ++i)
    for (int j = 1; j <= n; ++j) fth[i][j] = fth[i - 1][fth[i - 1][j]];
}
int getFth(int v, int d) {
  for (int i = 0; (1 << i) <= d; ++i)
    if (d & (1 << i)) v = fth[i][v];
  return v;
}
int getLCA(int v, int u) {
  if (h[v] < h[u]) swap(v, u);
  for (int i = LOGN - 1; i >= 0; --i)
    if (h[v] - (1 << i) >= h[u]) v = fth[i][v];
  if (v == u) return v;
  for (int i = LOGN - 1; i >= 0; --i)
    if (fth[i][v] != fth[i][u]) v = fth[i][v], u = fth[i][u];
  return fth[0][v];
}
int go(int v, int lca, int u, ull hsh, int len) {
  int k, kk;
  for (k = 0; v != lca; v = fth[0][v], ++k) path[k] = fthCh[v];
  for (kk = k; u != lca; u = fth[0][u], ++kk) path[kk] = fthCh[u];
  reverse(path + k, path + kk);
  int res = 0;
  ull h = 0;
  for (int i = 0; i < kk; ++i) {
    h = h * B + path[i] - 'a';
    if (i >= len - 1 && h == hsh) ++res;
    if (i >= len - 1) h -= (path[i - len + 1] - 'a') * pwb[len - 1];
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, a, b, q;
  char c;
  string s;
  pwb[0] = 1;
  for (int i = 1; i < N; ++i) pwb[i] = pwb[i - 1] * B;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> a >> b >> c;
    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);
  }
  prepareLCA(n);
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a >> b >> s;
    int lca = getLCA(a, b);
    ull hsh = getHash(s);
    reverse(s.begin(), s.end());
    ull hrev = getHash(s);
    int v = a, u = b;
    if (h[a] - h[lca] >= s.length()) {
      v = getFth(a, h[a] - h[lca] - s.length() + 1);
      ev[a].emplace_back(hrev, s.length(), 1, i);
      ev[v].emplace_back(hrev, s.length(), -1, i);
    }
    if (h[b] - h[lca] >= s.length()) {
      u = getFth(b, h[b] - h[lca] - s.length() + 1);
      ev[b].emplace_back(hsh, s.length(), 1, i);
      ev[u].emplace_back(hsh, s.length(), -1, i);
    }
    if (v != lca && u != lca) ans[i] += go(v, lca, u, hsh, s.length());
  }
  for (int i = 1; i <= n; ++i) sort(ev[i].begin(), ev[i].end());
  for (int i = 1; i < S; ++i) {
    hashes.clear();
    solve(1, 0, i);
  }
  for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
  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;
using uint = unsigned int;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &item) {
  out << '(' << item.first << ", " << item.second << ')';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const auto &item : v) out << item << ' ';
  return out;
}
const int S = 105;
const int B = 37;
const int LOGN = 18;
const int N = 100010;
const int Q = 100010;
struct Event {
  ull h;
  int len, mul, id;
  Event(ull h, int len, int mul, int id) : h(h), len(len), mul(mul), id(id) {}
  bool operator<(const Event &o) const { return len < o.len; }
};
int h[N], ans[Q], ptr[N];
int fth[LOGN][N];
char fthCh[N], path[N];
ull pwb[N];
vector<pair<int, ull>> stk;
vector<pair<int, char>> adj[N];
vector<Event> ev[N];
unordered_map<ull, int> hashes;
ull getHash(const string &s) {
  ull hsh = 0;
  for (auto ch : s) hsh = hsh * B + ch - 'a';
  return hsh;
}
void dfs(int v, int p = -1) {
  for (auto [u, ch] : adj[v]) {
    if (u == p) continue;
    h[u] = 1 + h[v];
    fth[0][u] = v;
    fthCh[u] = ch;
    dfs(u, v);
  }
}
void solve(int v, ull h, int len, int p = -1) {
  if (len <= stk.size()) {
    ull hsh = h - stk[stk.size() - len].second * pwb[len];
    ++hashes[hsh];
  }
  while (ptr[v] < ev[v].size() && ev[v][ptr[v]].len == len) {
    const auto &[hsh, len, mul, id] = ev[v][ptr[v]];
    ans[id] += mul * hashes[hsh];
    ++ptr[v];
  }
  stk.emplace_back(v, h);
  for (auto [u, ch] : adj[v]) {
    if (u == p) continue;
    solve(u, h * B + ch - 'a', len, v);
  }
  stk.pop_back();
  if (len <= stk.size()) {
    ull hsh = h - stk[stk.size() - len].second * pwb[len];
    --hashes[hsh];
  }
}
void prepareLCA(int n) {
  dfs(1);
  for (int i = 1; i < LOGN; ++i)
    for (int j = 1; j <= n; ++j) fth[i][j] = fth[i - 1][fth[i - 1][j]];
}
int getFth(int v, int d) {
  for (int i = 0; (1 << i) <= d; ++i)
    if (d & (1 << i)) v = fth[i][v];
  return v;
}
int getLCA(int v, int u) {
  if (h[v] < h[u]) swap(v, u);
  for (int i = LOGN - 1; i >= 0; --i)
    if (h[v] - (1 << i) >= h[u]) v = fth[i][v];
  if (v == u) return v;
  for (int i = LOGN - 1; i >= 0; --i)
    if (fth[i][v] != fth[i][u]) v = fth[i][v], u = fth[i][u];
  return fth[0][v];
}
int go(int v, int lca, int u, ull hsh, int len) {
  int k, kk;
  for (k = 0; v != lca; v = fth[0][v], ++k) path[k] = fthCh[v];
  for (kk = k; u != lca; u = fth[0][u], ++kk) path[kk] = fthCh[u];
  reverse(path + k, path + kk);
  int res = 0;
  ull h = 0;
  for (int i = 0; i < kk; ++i) {
    h = h * B + path[i] - 'a';
    if (i >= len - 1 && h == hsh) ++res;
    if (i >= len - 1) h -= (path[i - len + 1] - 'a') * pwb[len - 1];
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, a, b, q;
  char c;
  string s;
  pwb[0] = 1;
  for (int i = 1; i < N; ++i) pwb[i] = pwb[i - 1] * B;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> a >> b >> c;
    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);
  }
  prepareLCA(n);
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a >> b >> s;
    int lca = getLCA(a, b);
    ull hsh = getHash(s);
    reverse(s.begin(), s.end());
    ull hrev = getHash(s);
    int v = a, u = b;
    if (h[a] - h[lca] >= s.length()) {
      v = getFth(a, h[a] - h[lca] - s.length() + 1);
      ev[a].emplace_back(hrev, s.length(), 1, i);
      ev[v].emplace_back(hrev, s.length(), -1, i);
    }
    if (h[b] - h[lca] >= s.length()) {
      u = getFth(b, h[b] - h[lca] - s.length() + 1);
      ev[b].emplace_back(hsh, s.length(), 1, i);
      ev[u].emplace_back(hsh, s.length(), -1, i);
    }
    if (v != lca && u != lca) ans[i] += go(v, lca, u, hsh, s.length());
  }
  for (int i = 1; i <= n; ++i) sort(ev[i].begin(), ev[i].end());
  for (int i = 1; i < S; ++i) {
    hashes.clear();
    solve(1, 0, i);
  }
  for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
  return 0;
}

```