#include <bits/stdc++.h>
namespace bf {
const int N = 101000, M = N * 10, MOD = 1000000009, H = 19260817;
int pow[M];
struct value {
  int w, len;
  value(int x = 0) { w = x, len = 1; }
  value(int x, int y) : w(x), len(y) {}
  value operator+(const value &A) const {
    return value((w + (long long)pow[len] * A.w) % MOD, len + A.len);
  }
  bool operator==(const value &A) const { return w == A.w && len == A.len; }
};
struct atom {
  int x, y, ty, id;
  atom(int a = 0, int b = 0, int c = 0, int d = 0) : x(a), y(b), ty(c), id(d) {}
  bool operator<(const atom &A) const {
    return x == A.x ? ty == 0 && A.ty != 0 : x < A.x;
  }
};
struct failtree {
  int begin[M], next[M * 2], to[M * 2];
  int L[M], R[M];
  int e, clk;
  void add(int x, int y) {
    to[++e] = y;
    next[e] = begin[x];
    begin[x] = e;
  }
  void dfs(int p) {
    L[p] = ++clk;
    for (int i = begin[p]; i; i = next[i]) dfs(to[i]);
    R[p] = clk;
  }
} T;
struct bit {
  int s[N];
  int lowbit(int x) { return x & (-x); }
  void modify(int x, int y) {
    for (; x < N; x += lowbit(x)) s[x] += y;
  }
  int ask(int x) {
    int ret = 0;
    for (; x; x -= lowbit(x)) ret += s[x];
    return ret;
  }
} BIT;
int ans[N];
int maxlen[M], size[M];
int st[M][26], fail[M];
int cntnode;
int newnode() { return ++cntnode; }
struct worker {
  std::vector<atom> V;
  int root, lst;
  void init() { lst = root = newnode(); }
  void extend(int c) {
    int p = lst, np = newnode(), q, nq;
    lst = np, maxlen[np] = maxlen[p] + 1, size[np] = 1;
    for (; p && !st[p][c]; p = fail[p]) st[p][c] = np;
    if (!p) {
      fail[np] = root;
      return;
    }
    q = st[p][c];
    if (maxlen[q] == maxlen[p] + 1) {
      fail[np] = q;
      return;
    }
    nq = newnode();
    memcpy(st[nq], st[q], sizeof(st[nq]));
    fail[nq] = fail[q], maxlen[nq] = maxlen[p] + 1;
    fail[q] = fail[np] = nq;
    for (; p && st[p][c] == q; p = fail[p]) st[p][c] = nq;
  }
  void setit() {
    for (int i = root + 1; i <= cntnode; i++) T.add(fail[i], i);
    T.dfs(root);
    for (int i = root + 1; i <= cntnode; i++)
      if (size[i] > 0) V.push_back(atom(T.L[i], maxlen[i], 0));
  }
  void addstr(char *s, int l, int r, int id) {
    if (l > r) return;
    int len = strlen(s), p = root;
    for (int i = 0; i < len; i++) {
      if (!st[p][s[i] - 'a']) return;
      p = st[p][s[i] - 'a'];
    }
    V.push_back(atom(T.R[p], r, 1, id));
    V.push_back(atom(T.L[p] - 1, r, -1, id));
    V.push_back(atom(T.R[p], l - 1, -1, id));
    V.push_back(atom(T.L[p] - 1, l - 1, 1, id));
  }
  void solve() {
    std::sort(V.begin(), V.end());
    for (int i = 0; i < (int)V.size(); i++) {
      if (V[i].ty == 0)
        BIT.modify(V[i].y, 1);
      else
        ans[V[i].id] += V[i].ty * BIT.ask(V[i].y);
    }
    for (int i = 0; i < (int)V.size(); i++)
      if (V[i].ty == 0) BIT.modify(V[i].y, -1);
  }
};
int begin[N], next[N * 2], to[N * 2];
char ch[N * 2];
int n, m, e;
void add(int x, int y, char z, bool k = 1) {
  to[++e] = y;
  next[e] = begin[x];
  begin[x] = e;
  ch[e] = z;
  if (k) add(y, x, z, 0);
}
worker s[N], t[N];
char tf[N];
int fa[N][20];
value up[N][20], down[N][20];
int top[N], son[N], siz[N], dep[N];
void predfs(int p = 1, int h = 0) {
  dep[p] = dep[h] + 1, fa[p][0] = h, up[p][0] = down[p][0] = value(tf[p]);
  for (int k = 1; k < 20; k++) fa[p][k] = fa[fa[p][k - 1]][k - 1];
  for (int k = 1; k < 20; k++)
    up[p][k] = up[p][k - 1] + up[fa[p][k - 1]][k - 1];
  for (int k = 1; k < 20; k++)
    down[p][k] = down[fa[p][k - 1]][k - 1] + down[p][k - 1];
  top[p] = p, son[p] = 0, siz[p] = 1;
  for (int i = begin[p], q; i; i = next[i])
    if ((q = to[i]) != h) {
      tf[q] = ch[i], predfs(q, p), siz[p] += siz[q];
      if (siz[q] > siz[son[p]]) son[p] = q;
    }
  if (son[p]) top[son[p]] = p;
}
void redfs(int p = 1, int h = 0) {
  siz[p] = 1;
  for (int i = begin[p], q; i; i = next[i])
    if (top[q = to[i]] == p) top[q] = top[p], redfs(q, p), siz[p] = siz[q] + 1;
  for (int i = begin[p], q; i; i = next[i])
    if ((q = to[i]) != h && top[q] != top[p]) redfs(q, p);
  if (p == top[p]) {
    s[p].init();
    int x = son[p], bot = p;
    for (; x; bot = x, x = son[x]) s[p].extend(tf[x] - 'a');
    s[p].setit();
    t[p].init();
    for (; bot != p; bot = fa[bot][0]) t[p].extend(tf[bot] - 'a');
    t[p].setit();
  }
}
int jump(int p, int x) {
  for (int k = 19; ~k; k--)
    if (x & (1 << k)) p = fa[p][k];
  return p;
}
value getup(int p, int x) {
  value ret(0, 0);
  for (int k = 19; ~k; k--)
    if (x & (1 << k)) ret = ret + up[p][k], p = fa[p][k];
  return ret;
}
value getdown(int p, int x) {
  value ret(0, 0);
  for (int k = 19; ~k; k--)
    if (x & (1 << k)) ret = down[p][k] + ret, p = fa[p][k];
  return ret;
}
int getlca(int u, int v) {
  if (top[u] == top[v]) return dep[u] < dep[v] ? u : v;
  if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
  return getlca(fa[top[u]][0], v);
}
char str[N];
value hash;
int len;
void gogogo(int u, int v, int bot, int id, bool dir) {
  int tu = top[u];
  if (top[u] == top[v]) {
    if (dir == 0) {
      t[tu].addstr(str, dep[tu] + siz[tu] - dep[u] + len - 1,
                   dep[tu] + siz[tu] - dep[v] - 1, id);
    } else {
      s[tu].addstr(str, dep[v] - dep[tu] + 1 + len - 1, dep[u] - dep[tu], id);
    }
    return;
  }
  if (dir == 0) {
    t[tu].addstr(str, dep[tu] + siz[tu] - dep[u] + len - 1, siz[tu] - 1, id);
  } else {
    s[tu].addstr(str, 1 + len - 1, dep[u] - dep[tu], id);
  }
  for (int x = 0; x < len; x++) {
    if (x > dep[u] - dep[tu] || len - x - 1 > dep[tu] - dep[v] - 1) continue;
    int q = jump(bot, dep[bot] - dep[tu] - x);
    value val;
    if (dir == 0)
      val = getup(q, len);
    else
      val = getdown(q, len);
    if (val == hash) ans[id]++;
  }
  gogogo(fa[tu][0], v, bot, id, dir);
}
void putsquery(int u, int v, int id) {
  hash = value(0, 0), len = strlen(str);
  int p = getlca(u, v);
  for (int i = 0; i < len; i++) hash = hash + value(str[i]);
  for (int x = 1; x < len; x++) {
    if (x > dep[u] - dep[p] || len - x > dep[v] - dep[p]) continue;
    int du = jump(u, dep[u] - dep[p] - x),
        dv = jump(v, dep[v] - dep[p] - (len - x));
    if (hash == getup(du, x) + getdown(dv, len - x)) ans[id]++;
  }
  gogogo(u, p, u, id, 0), gogogo(v, p, v, id, 1);
}
void initialize() {
  scanf("%d", &n);
  int u, v;
  char w;
  for (int i = 1; i < n; i++) scanf("%d %d %c", &u, &v, &w), add(u, v, w);
  pow[0] = 1;
  for (int i = 1; i < M; i++) pow[i] = (long long)pow[i - 1] * H % MOD;
  predfs(), redfs();
  scanf("%d", &m);
  for (int i = 1; i <= m; i++)
    scanf("%d%d", &u, &v), scanf("%s", str), putsquery(u, v, i);
}
void solve() {
  initialize();
  for (int i = 1; i <= n; i++)
    if (i == top[i]) s[i].solve(), t[i].solve();
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
}  // namespace bf
int main() {
  bf::solve();
  return 0;
}

### Prompt

Your challenge is to write 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>
namespace bf {
const int N = 101000, M = N * 10, MOD = 1000000009, H = 19260817;
int pow[M];
struct value {
  int w, len;
  value(int x = 0) { w = x, len = 1; }
  value(int x, int y) : w(x), len(y) {}
  value operator+(const value &A) const {
    return value((w + (long long)pow[len] * A.w) % MOD, len + A.len);
  }
  bool operator==(const value &A) const { return w == A.w && len == A.len; }
};
struct atom {
  int x, y, ty, id;
  atom(int a = 0, int b = 0, int c = 0, int d = 0) : x(a), y(b), ty(c), id(d) {}
  bool operator<(const atom &A) const {
    return x == A.x ? ty == 0 && A.ty != 0 : x < A.x;
  }
};
struct failtree {
  int begin[M], next[M * 2], to[M * 2];
  int L[M], R[M];
  int e, clk;
  void add(int x, int y) {
    to[++e] = y;
    next[e] = begin[x];
    begin[x] = e;
  }
  void dfs(int p) {
    L[p] = ++clk;
    for (int i = begin[p]; i; i = next[i]) dfs(to[i]);
    R[p] = clk;
  }
} T;
struct bit {
  int s[N];
  int lowbit(int x) { return x & (-x); }
  void modify(int x, int y) {
    for (; x < N; x += lowbit(x)) s[x] += y;
  }
  int ask(int x) {
    int ret = 0;
    for (; x; x -= lowbit(x)) ret += s[x];
    return ret;
  }
} BIT;
int ans[N];
int maxlen[M], size[M];
int st[M][26], fail[M];
int cntnode;
int newnode() { return ++cntnode; }
struct worker {
  std::vector<atom> V;
  int root, lst;
  void init() { lst = root = newnode(); }
  void extend(int c) {
    int p = lst, np = newnode(), q, nq;
    lst = np, maxlen[np] = maxlen[p] + 1, size[np] = 1;
    for (; p && !st[p][c]; p = fail[p]) st[p][c] = np;
    if (!p) {
      fail[np] = root;
      return;
    }
    q = st[p][c];
    if (maxlen[q] == maxlen[p] + 1) {
      fail[np] = q;
      return;
    }
    nq = newnode();
    memcpy(st[nq], st[q], sizeof(st[nq]));
    fail[nq] = fail[q], maxlen[nq] = maxlen[p] + 1;
    fail[q] = fail[np] = nq;
    for (; p && st[p][c] == q; p = fail[p]) st[p][c] = nq;
  }
  void setit() {
    for (int i = root + 1; i <= cntnode; i++) T.add(fail[i], i);
    T.dfs(root);
    for (int i = root + 1; i <= cntnode; i++)
      if (size[i] > 0) V.push_back(atom(T.L[i], maxlen[i], 0));
  }
  void addstr(char *s, int l, int r, int id) {
    if (l > r) return;
    int len = strlen(s), p = root;
    for (int i = 0; i < len; i++) {
      if (!st[p][s[i] - 'a']) return;
      p = st[p][s[i] - 'a'];
    }
    V.push_back(atom(T.R[p], r, 1, id));
    V.push_back(atom(T.L[p] - 1, r, -1, id));
    V.push_back(atom(T.R[p], l - 1, -1, id));
    V.push_back(atom(T.L[p] - 1, l - 1, 1, id));
  }
  void solve() {
    std::sort(V.begin(), V.end());
    for (int i = 0; i < (int)V.size(); i++) {
      if (V[i].ty == 0)
        BIT.modify(V[i].y, 1);
      else
        ans[V[i].id] += V[i].ty * BIT.ask(V[i].y);
    }
    for (int i = 0; i < (int)V.size(); i++)
      if (V[i].ty == 0) BIT.modify(V[i].y, -1);
  }
};
int begin[N], next[N * 2], to[N * 2];
char ch[N * 2];
int n, m, e;
void add(int x, int y, char z, bool k = 1) {
  to[++e] = y;
  next[e] = begin[x];
  begin[x] = e;
  ch[e] = z;
  if (k) add(y, x, z, 0);
}
worker s[N], t[N];
char tf[N];
int fa[N][20];
value up[N][20], down[N][20];
int top[N], son[N], siz[N], dep[N];
void predfs(int p = 1, int h = 0) {
  dep[p] = dep[h] + 1, fa[p][0] = h, up[p][0] = down[p][0] = value(tf[p]);
  for (int k = 1; k < 20; k++) fa[p][k] = fa[fa[p][k - 1]][k - 1];
  for (int k = 1; k < 20; k++)
    up[p][k] = up[p][k - 1] + up[fa[p][k - 1]][k - 1];
  for (int k = 1; k < 20; k++)
    down[p][k] = down[fa[p][k - 1]][k - 1] + down[p][k - 1];
  top[p] = p, son[p] = 0, siz[p] = 1;
  for (int i = begin[p], q; i; i = next[i])
    if ((q = to[i]) != h) {
      tf[q] = ch[i], predfs(q, p), siz[p] += siz[q];
      if (siz[q] > siz[son[p]]) son[p] = q;
    }
  if (son[p]) top[son[p]] = p;
}
void redfs(int p = 1, int h = 0) {
  siz[p] = 1;
  for (int i = begin[p], q; i; i = next[i])
    if (top[q = to[i]] == p) top[q] = top[p], redfs(q, p), siz[p] = siz[q] + 1;
  for (int i = begin[p], q; i; i = next[i])
    if ((q = to[i]) != h && top[q] != top[p]) redfs(q, p);
  if (p == top[p]) {
    s[p].init();
    int x = son[p], bot = p;
    for (; x; bot = x, x = son[x]) s[p].extend(tf[x] - 'a');
    s[p].setit();
    t[p].init();
    for (; bot != p; bot = fa[bot][0]) t[p].extend(tf[bot] - 'a');
    t[p].setit();
  }
}
int jump(int p, int x) {
  for (int k = 19; ~k; k--)
    if (x & (1 << k)) p = fa[p][k];
  return p;
}
value getup(int p, int x) {
  value ret(0, 0);
  for (int k = 19; ~k; k--)
    if (x & (1 << k)) ret = ret + up[p][k], p = fa[p][k];
  return ret;
}
value getdown(int p, int x) {
  value ret(0, 0);
  for (int k = 19; ~k; k--)
    if (x & (1 << k)) ret = down[p][k] + ret, p = fa[p][k];
  return ret;
}
int getlca(int u, int v) {
  if (top[u] == top[v]) return dep[u] < dep[v] ? u : v;
  if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
  return getlca(fa[top[u]][0], v);
}
char str[N];
value hash;
int len;
void gogogo(int u, int v, int bot, int id, bool dir) {
  int tu = top[u];
  if (top[u] == top[v]) {
    if (dir == 0) {
      t[tu].addstr(str, dep[tu] + siz[tu] - dep[u] + len - 1,
                   dep[tu] + siz[tu] - dep[v] - 1, id);
    } else {
      s[tu].addstr(str, dep[v] - dep[tu] + 1 + len - 1, dep[u] - dep[tu], id);
    }
    return;
  }
  if (dir == 0) {
    t[tu].addstr(str, dep[tu] + siz[tu] - dep[u] + len - 1, siz[tu] - 1, id);
  } else {
    s[tu].addstr(str, 1 + len - 1, dep[u] - dep[tu], id);
  }
  for (int x = 0; x < len; x++) {
    if (x > dep[u] - dep[tu] || len - x - 1 > dep[tu] - dep[v] - 1) continue;
    int q = jump(bot, dep[bot] - dep[tu] - x);
    value val;
    if (dir == 0)
      val = getup(q, len);
    else
      val = getdown(q, len);
    if (val == hash) ans[id]++;
  }
  gogogo(fa[tu][0], v, bot, id, dir);
}
void putsquery(int u, int v, int id) {
  hash = value(0, 0), len = strlen(str);
  int p = getlca(u, v);
  for (int i = 0; i < len; i++) hash = hash + value(str[i]);
  for (int x = 1; x < len; x++) {
    if (x > dep[u] - dep[p] || len - x > dep[v] - dep[p]) continue;
    int du = jump(u, dep[u] - dep[p] - x),
        dv = jump(v, dep[v] - dep[p] - (len - x));
    if (hash == getup(du, x) + getdown(dv, len - x)) ans[id]++;
  }
  gogogo(u, p, u, id, 0), gogogo(v, p, v, id, 1);
}
void initialize() {
  scanf("%d", &n);
  int u, v;
  char w;
  for (int i = 1; i < n; i++) scanf("%d %d %c", &u, &v, &w), add(u, v, w);
  pow[0] = 1;
  for (int i = 1; i < M; i++) pow[i] = (long long)pow[i - 1] * H % MOD;
  predfs(), redfs();
  scanf("%d", &m);
  for (int i = 1; i <= m; i++)
    scanf("%d%d", &u, &v), scanf("%s", str), putsquery(u, v, i);
}
void solve() {
  initialize();
  for (int i = 1; i <= n; i++)
    if (i == top[i]) s[i].solve(), t[i].solve();
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
}  // namespace bf
int main() {
  bf::solve();
  return 0;
}

```