#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
  unsigned long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() { return abs((int)mt()); }
struct KMP {
  string s;
  int n;
  vector<int> link;
  void build(char* str) {
    s = str, n = (int)s.size();
    link.resize(n);
    int cur = link[0] = -1;
    for (int i = 1; i < n; i++) {
      while (cur >= 0 && s[i] != s[cur + 1]) cur = link[cur];
      if (s[i] == s[cur + 1]) cur++;
      link[i] = cur;
    }
  }
  vector<int> query(char* t) {
    vector<int> res;
    int k = strlen(t);
    int cur = -1;
    for (int i = 0; i < k; i++) {
      while (cur >= 0 && t[i] != s[cur + 1]) cur = link[cur];
      if (t[i] == s[cur + 1]) cur++;
      if (cur == n - 1) {
        res.push_back(i - n + 1);
        cur = link[cur];
      }
    }
    return res;
  }
} kmp;
const int maxn = 1e5 + 5;
const int logn = 20;
int n, q;
vector<pair<int, char> > adj[maxn];
int u[maxn];
int v[maxn];
string s[maxn];
int lev[maxn];
int par[logn][maxn];
string g[maxn];
string gi[maxn];
vector<string> vals;
int res[maxn];
vector<pair<int, pair<int, int> > > que[maxn];
char from[maxn];
void dfs(int u, int dad) {
  for (int i = (1); i < (logn); ++i) par[i][u] = par[i - 1][par[i - 1][u]];
  for (auto e : adj[u]) {
    int v = e.first;
    char c = e.second;
    if (v != dad) {
      lev[v] = lev[u] + 1;
      par[0][v] = u;
      from[v] = c;
      dfs(v, u);
    }
  }
}
int lca(int u, int v) {
  if (lev[u] < lev[v]) swap(u, v);
  for (int i = (logn)-1; i >= (0); --i) {
    if (lev[par[i][u]] >= lev[v]) {
      u = par[i][u];
    }
  }
  if (u == v) return u;
  for (int i = (logn)-1; i >= (0); --i) {
    if (par[i][u] != par[i][v]) {
      u = par[i][u];
      v = par[i][v];
    }
  }
  return par[0][u];
}
int jump(int u, int k) {
  for (int i = (logn)-1; i >= (0); --i) {
    if ((1 << i) <= k) {
      k -= 1 << i;
      u = par[i][u];
    }
  }
  return u;
}
int cnt[maxn * 2];
void upd_dfs(int u, int dad) {
  int w = u, rt = 0;
  string t = "";
  for (int i = (0); i < (min(100, lev[u])); ++i) {
    char c = from[w];
    t += c;
    int k = lower_bound((vals).begin(), (vals).end(), t) - vals.begin();
    if (k < int((vals).size()) && vals[k] == t) {
      cnt[k]++;
    }
    w = par[0][w];
  }
  for (auto it : que[u]) {
    int ix = it.first;
    int u = it.second.first;
    int s = it.second.second;
    res[ix] += cnt[u] * s;
  }
  for (auto e : adj[u]) {
    int v = e.first;
    if (v != dad) {
      upd_dfs(v, u);
    }
  }
  w = u, rt = 0;
  t = "";
  for (int i = (0); i < (min(100, lev[u])); ++i) {
    char c = from[w];
    t += c;
    int k = lower_bound((vals).begin(), (vals).end(), t) - vals.begin();
    if (k < int((vals).size()) && vals[k] == t) {
      cnt[k]--;
    }
    w = par[0][w];
  }
}
void chemthan() {
  cin >> n;
  for (int i = (0); i < (n - 1); ++i) {
    int u, v;
    char c;
    cin >> u >> v >> c;
    u--, v--;
    adj[u].push_back(make_pair(v, c));
    adj[v].push_back(make_pair(u, c));
  }
  cin >> q;
  for (int i = (0); i < (q); ++i) {
    cin >> u[i] >> v[i] >> s[i], u[i]--, v[i]--;
    reverse((s[i]).begin(), (s[i]).end());
    gi[i] = s[i];
    reverse((s[i]).begin(), (s[i]).end());
    g[i] = s[i];
    vals.push_back(g[i]), vals.push_back(gi[i]);
  }
  sort((vals).begin(), (vals).end()),
      (vals).erase(unique((vals).begin(), (vals).end()), (vals).end());
  static int h[maxn];
  static int hi[maxn];
  for (int i = (0); i < (q); ++i)
    h[i] = lower_bound((vals).begin(), (vals).end(), g[i]) - vals.begin();
  for (int i = (0); i < (q); ++i)
    hi[i] = lower_bound((vals).begin(), (vals).end(), gi[i]) - vals.begin();
  dfs(0, -1);
  for (int i = (0); i < (q); ++i) {
    if (u[i] == v[i]) {
      continue;
    }
    int w = lca(u[i], v[i]);
    int iter = 0;
    for (int t : {u[i], v[i]}) {
      if (lev[t] - lev[w] >= int((s[i]).size())) {
        int k = lev[t] - (lev[w] + int((s[i]).size()) - 1);
        int nt = jump(t, k);
        if (!iter) {
          que[t].push_back(make_pair(i, make_pair(h[i], 1)));
          que[nt].push_back(make_pair(i, make_pair(h[i], -1)));
        } else {
          que[t].push_back(make_pair(i, make_pair(hi[i], 1)));
          que[nt].push_back(make_pair(i, make_pair(hi[i], -1)));
        }
      }
      iter++;
    }
    if (w != u[i] && w != v[i]) {
      int du = lev[u[i]] - lev[w];
      int dv = lev[v[i]] - lev[w];
      int mu = min(du, int((s[i]).size()) - 1);
      int mv = min(dv, int((s[i]).size()) - 1);
      int su = jump(u[i], du - mu);
      int sv = jump(v[i], dv - mv);
      string tu;
      while (su != w) {
        char c = from[su];
        tu += c;
        su = par[0][su];
      }
      string tv;
      while (sv != w) {
        char c = from[sv];
        tv += c;
        sv = par[0][sv];
      }
      reverse((tv).begin(), (tv).end());
      string tt = tu + tv;
      kmp.build((char*)s[i].data());
      res[i] += int((kmp.query((char*)tt.data())).size());
    }
  }
  upd_dfs(0, -1);
  for (int i = (0); i < (q); ++i) cout << res[i] << "\n";
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0);
  if (argc > 1) {
    assert(freopen(argv[1], "r", stdin));
  }
  if (argc > 2) {
    assert(freopen(argv[2], "wb", stdout));
  }
  chemthan();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

### 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;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
  unsigned long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() { return abs((int)mt()); }
struct KMP {
  string s;
  int n;
  vector<int> link;
  void build(char* str) {
    s = str, n = (int)s.size();
    link.resize(n);
    int cur = link[0] = -1;
    for (int i = 1; i < n; i++) {
      while (cur >= 0 && s[i] != s[cur + 1]) cur = link[cur];
      if (s[i] == s[cur + 1]) cur++;
      link[i] = cur;
    }
  }
  vector<int> query(char* t) {
    vector<int> res;
    int k = strlen(t);
    int cur = -1;
    for (int i = 0; i < k; i++) {
      while (cur >= 0 && t[i] != s[cur + 1]) cur = link[cur];
      if (t[i] == s[cur + 1]) cur++;
      if (cur == n - 1) {
        res.push_back(i - n + 1);
        cur = link[cur];
      }
    }
    return res;
  }
} kmp;
const int maxn = 1e5 + 5;
const int logn = 20;
int n, q;
vector<pair<int, char> > adj[maxn];
int u[maxn];
int v[maxn];
string s[maxn];
int lev[maxn];
int par[logn][maxn];
string g[maxn];
string gi[maxn];
vector<string> vals;
int res[maxn];
vector<pair<int, pair<int, int> > > que[maxn];
char from[maxn];
void dfs(int u, int dad) {
  for (int i = (1); i < (logn); ++i) par[i][u] = par[i - 1][par[i - 1][u]];
  for (auto e : adj[u]) {
    int v = e.first;
    char c = e.second;
    if (v != dad) {
      lev[v] = lev[u] + 1;
      par[0][v] = u;
      from[v] = c;
      dfs(v, u);
    }
  }
}
int lca(int u, int v) {
  if (lev[u] < lev[v]) swap(u, v);
  for (int i = (logn)-1; i >= (0); --i) {
    if (lev[par[i][u]] >= lev[v]) {
      u = par[i][u];
    }
  }
  if (u == v) return u;
  for (int i = (logn)-1; i >= (0); --i) {
    if (par[i][u] != par[i][v]) {
      u = par[i][u];
      v = par[i][v];
    }
  }
  return par[0][u];
}
int jump(int u, int k) {
  for (int i = (logn)-1; i >= (0); --i) {
    if ((1 << i) <= k) {
      k -= 1 << i;
      u = par[i][u];
    }
  }
  return u;
}
int cnt[maxn * 2];
void upd_dfs(int u, int dad) {
  int w = u, rt = 0;
  string t = "";
  for (int i = (0); i < (min(100, lev[u])); ++i) {
    char c = from[w];
    t += c;
    int k = lower_bound((vals).begin(), (vals).end(), t) - vals.begin();
    if (k < int((vals).size()) && vals[k] == t) {
      cnt[k]++;
    }
    w = par[0][w];
  }
  for (auto it : que[u]) {
    int ix = it.first;
    int u = it.second.first;
    int s = it.second.second;
    res[ix] += cnt[u] * s;
  }
  for (auto e : adj[u]) {
    int v = e.first;
    if (v != dad) {
      upd_dfs(v, u);
    }
  }
  w = u, rt = 0;
  t = "";
  for (int i = (0); i < (min(100, lev[u])); ++i) {
    char c = from[w];
    t += c;
    int k = lower_bound((vals).begin(), (vals).end(), t) - vals.begin();
    if (k < int((vals).size()) && vals[k] == t) {
      cnt[k]--;
    }
    w = par[0][w];
  }
}
void chemthan() {
  cin >> n;
  for (int i = (0); i < (n - 1); ++i) {
    int u, v;
    char c;
    cin >> u >> v >> c;
    u--, v--;
    adj[u].push_back(make_pair(v, c));
    adj[v].push_back(make_pair(u, c));
  }
  cin >> q;
  for (int i = (0); i < (q); ++i) {
    cin >> u[i] >> v[i] >> s[i], u[i]--, v[i]--;
    reverse((s[i]).begin(), (s[i]).end());
    gi[i] = s[i];
    reverse((s[i]).begin(), (s[i]).end());
    g[i] = s[i];
    vals.push_back(g[i]), vals.push_back(gi[i]);
  }
  sort((vals).begin(), (vals).end()),
      (vals).erase(unique((vals).begin(), (vals).end()), (vals).end());
  static int h[maxn];
  static int hi[maxn];
  for (int i = (0); i < (q); ++i)
    h[i] = lower_bound((vals).begin(), (vals).end(), g[i]) - vals.begin();
  for (int i = (0); i < (q); ++i)
    hi[i] = lower_bound((vals).begin(), (vals).end(), gi[i]) - vals.begin();
  dfs(0, -1);
  for (int i = (0); i < (q); ++i) {
    if (u[i] == v[i]) {
      continue;
    }
    int w = lca(u[i], v[i]);
    int iter = 0;
    for (int t : {u[i], v[i]}) {
      if (lev[t] - lev[w] >= int((s[i]).size())) {
        int k = lev[t] - (lev[w] + int((s[i]).size()) - 1);
        int nt = jump(t, k);
        if (!iter) {
          que[t].push_back(make_pair(i, make_pair(h[i], 1)));
          que[nt].push_back(make_pair(i, make_pair(h[i], -1)));
        } else {
          que[t].push_back(make_pair(i, make_pair(hi[i], 1)));
          que[nt].push_back(make_pair(i, make_pair(hi[i], -1)));
        }
      }
      iter++;
    }
    if (w != u[i] && w != v[i]) {
      int du = lev[u[i]] - lev[w];
      int dv = lev[v[i]] - lev[w];
      int mu = min(du, int((s[i]).size()) - 1);
      int mv = min(dv, int((s[i]).size()) - 1);
      int su = jump(u[i], du - mu);
      int sv = jump(v[i], dv - mv);
      string tu;
      while (su != w) {
        char c = from[su];
        tu += c;
        su = par[0][su];
      }
      string tv;
      while (sv != w) {
        char c = from[sv];
        tv += c;
        sv = par[0][sv];
      }
      reverse((tv).begin(), (tv).end());
      string tt = tu + tv;
      kmp.build((char*)s[i].data());
      res[i] += int((kmp.query((char*)tt.data())).size());
    }
  }
  upd_dfs(0, -1);
  for (int i = (0); i < (q); ++i) cout << res[i] << "\n";
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0);
  if (argc > 1) {
    assert(freopen(argv[1], "r", stdin));
  }
  if (argc > 2) {
    assert(freopen(argv[2], "wb", stdout));
  }
  chemthan();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

```