#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXQ = 100000;
const int MAXLEN = 100;
const int ALPH = 26;
const int MAXLG = 16;
const int BATCHSIZE = 2000;
int n;
vector<pair<int, char>> e[MAXN];
int nq;
int qa[MAXQ], qb[MAXQ];
char qs[MAXQ][MAXLEN + 1];
int qlen[MAXQ], qans[MAXQ];
struct AhoNode {
  int fail, head;
  AhoNode() : fail(-1), head(-1) {}
};
struct AhoEdge {
  int nxt;
  char c;
  int to;
  AhoEdge() {}
  AhoEdge(int nxt, char c, int to) : nxt(nxt), c(c), to(to) {}
};
struct Aho {
  AhoNode nodes[BATCHSIZE * MAXLEN + 1];
  int nnodes;
  AhoEdge edges[BATCHSIZE * MAXLEN];
  int nedges;
  int root;
  int addnode() {
    int ret = nnodes;
    nodes[nnodes++] = AhoNode();
    return ret;
  }
  void addedge(int at, char c, int to) {
    edges[nedges++] = AhoEdge(nodes[at].head, c, to);
    nodes[at].head = nedges - 1;
  }
  int getnxt(int at, char c) {
    for (int x = nodes[at].head; x != -1; x = edges[x].nxt)
      if (edges[x].c == c) return edges[x].to;
    return -1;
  }
  void init() {
    nnodes = nedges = 0;
    root = addnode();
  }
  int add(char *s) {
    int at = root;
    for (int i = 0; s[i] != '\0'; ++i) {
      int to = getnxt(at, s[i]);
      if (to == -1) {
        to = addnode();
        addedge(at, s[i], to);
      }
      at = to;
    }
    return at;
  }
  vector<int> bfs;
  void build() {
    bfs = vector<int>(1, root);
    for (int i = (0); i < (((int)(bfs).size())); ++i) {
      int at = bfs[i];
      for (int x = nodes[at].head; x != -1; x = edges[x].nxt) {
        int to = edges[x].to;
        bfs.push_back(to);
      }
    }
    assert(((int)(bfs).size()) == nnodes);
    nodes[root].fail = root;
    for (int i = (0); i < (((int)(bfs).size())); ++i) {
      int at = bfs[i];
      for (int x = nodes[at].head; x != -1; x = edges[x].nxt) {
        char c = edges[x].c;
        int to = nodes[at].fail;
        while (to != root && getnxt(to, c) == -1) to = nodes[to].fail;
        to = getnxt(to, c);
        if (at == root || to == -1) to = root;
        nodes[edges[x].to].fail = to;
      }
    }
  }
};
struct Kmp {
  vector<int> nxt;
  char *s;
  int slen;
  void init(char *_s) {
    s = _s, slen = strlen(s);
    nxt = vector<int>(slen + 1);
    nxt[0] = nxt[1] = 0;
    for (int i = (2); i <= (slen); ++i) {
      int at = nxt[i - 1];
      while (at != 0 && s[i - 1] != s[at]) at = nxt[at];
      if (s[i - 1] == s[at]) ++at;
      nxt[i] = at;
    }
  }
  int count(char *t) {
    int ret = 0, at = 0, tlen = strlen(t);
    for (int i = (0); i < (tlen); ++i) {
      while (at != 0 && t[i] != s[at]) at = nxt[at];
      if (t[i] == s[at]) ++at;
      if (at == slen) ++ret, at = nxt[at];
    }
    return ret;
  }
};
int par[MAXN], up[MAXN][MAXLG + 1], sz[MAXN], dep[MAXN], heavy[MAXN];
char parc[MAXN];
int hroot[MAXN], hlen[MAXN], hdn[MAXN], hup[MAXN];
char s[2 * MAXN + 1];
int slen;
void dfsinit(int at) {
  dep[at] = par[at] == -1 ? 0 : dep[par[at]] + 1, sz[at] = 1, heavy[at] = -1;
  up[at][0] = par[at] == -1 ? at : par[at];
  for (int k = (0); k < (MAXLG); ++k) up[at][k + 1] = up[up[at][k]][k];
  for (int i = (0); i < (((int)(e[at]).size())); ++i) {
    int to = e[at][i].first;
    if (to == par[at]) continue;
    par[to] = at, parc[to] = e[at][i].second;
    dfsinit(to);
    sz[at] += sz[to];
    if (heavy[at] == -1 || sz[to] > sz[heavy[at]]) heavy[at] = to;
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int k = MAXLG; k >= 0; --k)
    if (dep[a] - (1 << k) >= dep[b]) a = up[a][k];
  for (int k = MAXLG; k >= 0; --k)
    if (up[a][k] != up[b][k]) a = up[a][k], b = up[b][k];
  return a == b ? a : up[a][0];
}
vector<pair<int, int>> getpath(int a, int b) {
  int c = lca(a, b);
  vector<pair<int, int>> retup, retdn;
  while (hroot[a] != hroot[c]) {
    int x = hroot[a], off = dep[a] - dep[x];
    retup.push_back(
        make_pair(hup[x] + hlen[x] - off - 1, hup[x] + hlen[x] - 1));
    a = par[hroot[a]];
  }
  if (a != c) {
    int x = hroot[a], off1 = dep[a] - dep[x], off2 = dep[c] - dep[x] + 1;
    retup.push_back(
        make_pair(hup[x] + hlen[x] - off1 - 1, hup[x] + hlen[x] - off2 - 1));
    a = c;
  }
  while (hroot[b] != hroot[c]) {
    int x = hroot[b], off = dep[b] - dep[x];
    retdn.push_back(make_pair(hdn[x], hdn[x] + off));
    b = par[hroot[b]];
  }
  if (b != c) {
    int x = hroot[b], off1 = dep[b] - dep[x], off2 = dep[c] - dep[x] + 1;
    retdn.push_back(make_pair(hdn[x] + off2, hdn[x] + off1));
    b = c;
  }
  reverse(retdn.begin(), retdn.end());
  vector<pair<int, int>> ret = retup;
  for (int i = (0); i < (((int)(retdn).size())); ++i) ret.push_back(retdn[i]);
  return ret;
}
int qid[MAXQ];
Aho aho;
Kmp kmp;
char t[2 * (MAXLEN - 1) + 1];
int tlen;
vector<vector<int>> pos;
void solve() {
  par[0] = -1, parc[0] = '-';
  dfsinit(0);
  for (int i = (0); i < (n); ++i)
    if (par[i] != -1 && heavy[par[i]] == i)
      hlen[i] = -1;
    else {
      hlen[i] = 0;
      for (int at = i; at != -1; at = heavy[at]) hroot[at] = i, ++hlen[i];
    }
  slen = 0;
  for (int i = (0); i < (n); ++i)
    if (hlen[i] == -1)
      hdn[i] = hup[i] = -1;
    else {
      hdn[i] = slen;
      slen += hlen[i];
      hup[i] = slen;
      slen += hlen[i];
      for (int at = i, j = 0; at != -1; at = heavy[at], ++j)
        s[hdn[i] + j] = s[hup[i] + hlen[i] - j - 1] = parc[at];
    }
  s[slen] = '\0';
  for (int bs = 0; bs < nq; bs += BATCHSIZE) {
    int be = min(bs + BATCHSIZE, nq);
    aho.init();
    for (int i = (bs); i < (be); ++i) qid[i] = aho.add(qs[i]);
    aho.build();
    {
      pos = vector<vector<int>>(aho.nnodes);
      int at = aho.root;
      for (int i = (0); i < (slen); ++i) {
        while (at != aho.root && aho.getnxt(at, s[i]) == -1)
          at = aho.nodes[at].fail;
        at = aho.getnxt(at, s[i]);
        if (at == -1) at = aho.root;
        for (int to = at; to != aho.root; to = aho.nodes[to].fail)
          pos[to].push_back(i);
      }
    }
    for (int i = (bs); i < (be); ++i) {
      vector<pair<int, int>> path = getpath(qa[i], qb[i]);
      kmp.init(qs[i]);
      qans[i] = 0;
      for (int j = (0); j < (((int)(path).size())); ++j) {
        int plen = path[j].second - path[j].first + 1,
            nfst = min(qlen[i] - 1, plen);
        tlen = 0;
        for (int k = (0); k < (nfst); ++k)
          t[tlen++] = s[path[j].second + 1 - nfst + k];
        for (int jj = j + 1;
             jj < ((int)(path).size()) && tlen < nfst + qlen[i] - 1; ++jj)
          for (int k = path[jj].first;
               k <= path[jj].second && tlen < nfst + qlen[i] - 1; ++k)
            t[tlen++] = s[k];
        t[tlen] = '\0';
        qans[i] += kmp.count(t);
        if (plen >= qlen[i]) {
          int l = lower_bound(pos[qid[i]].begin(), pos[qid[i]].end(),
                              path[j].first + qlen[i] - 1) -
                  pos[qid[i]].begin();
          int r = lower_bound(pos[qid[i]].begin(), pos[qid[i]].end(),
                              path[j].second + 1) -
                  pos[qid[i]].begin();
          qans[i] += r - l;
        }
      }
    }
  }
}
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n - 1); ++i) {
    int a, b;
    char c;
    scanf("%d%d %c", &a, &b, &c);
    --a, --b;
    e[a].push_back(make_pair(b, c));
    e[b].push_back(make_pair(a, c));
  }
  scanf("%d", &nq);
  for (int i = (0); i < (nq); ++i) {
    scanf("%d%d%s", &qa[i], &qb[i], qs[i]);
    --qa[i], --qb[i], qlen[i] = strlen(qs[i]);
  }
  solve();
  for (int i = (0); i < (nq); ++i) printf("%d\n", qans[i]);
}
int myrand() { return rand() % 1000 * 1000 + rand() % 1000; }
void stress() {
  n = MAXN;
  for (int i = (0); i < (n - 1); ++i) {
    int a = i + 1, b = myrand() % (i + 1);
    char c = 'a' + rand() % 26;
    e[a].push_back(make_pair(b, c));
    e[b].push_back(make_pair(a, c));
  }
  nq = MAXQ;
  for (int i = (0); i < (nq); ++i) {
    qa[i] = myrand() % n, qb[i] = myrand() % n;
    for (int j = (0); j < (MAXLEN); ++j) qs[i][j] = 'a' + rand() % 26;
    qs[i][MAXLEN] = '\0';
    qlen[i] = MAXLEN;
  }
  solve();
}
int main() {
  run();
  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;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXQ = 100000;
const int MAXLEN = 100;
const int ALPH = 26;
const int MAXLG = 16;
const int BATCHSIZE = 2000;
int n;
vector<pair<int, char>> e[MAXN];
int nq;
int qa[MAXQ], qb[MAXQ];
char qs[MAXQ][MAXLEN + 1];
int qlen[MAXQ], qans[MAXQ];
struct AhoNode {
  int fail, head;
  AhoNode() : fail(-1), head(-1) {}
};
struct AhoEdge {
  int nxt;
  char c;
  int to;
  AhoEdge() {}
  AhoEdge(int nxt, char c, int to) : nxt(nxt), c(c), to(to) {}
};
struct Aho {
  AhoNode nodes[BATCHSIZE * MAXLEN + 1];
  int nnodes;
  AhoEdge edges[BATCHSIZE * MAXLEN];
  int nedges;
  int root;
  int addnode() {
    int ret = nnodes;
    nodes[nnodes++] = AhoNode();
    return ret;
  }
  void addedge(int at, char c, int to) {
    edges[nedges++] = AhoEdge(nodes[at].head, c, to);
    nodes[at].head = nedges - 1;
  }
  int getnxt(int at, char c) {
    for (int x = nodes[at].head; x != -1; x = edges[x].nxt)
      if (edges[x].c == c) return edges[x].to;
    return -1;
  }
  void init() {
    nnodes = nedges = 0;
    root = addnode();
  }
  int add(char *s) {
    int at = root;
    for (int i = 0; s[i] != '\0'; ++i) {
      int to = getnxt(at, s[i]);
      if (to == -1) {
        to = addnode();
        addedge(at, s[i], to);
      }
      at = to;
    }
    return at;
  }
  vector<int> bfs;
  void build() {
    bfs = vector<int>(1, root);
    for (int i = (0); i < (((int)(bfs).size())); ++i) {
      int at = bfs[i];
      for (int x = nodes[at].head; x != -1; x = edges[x].nxt) {
        int to = edges[x].to;
        bfs.push_back(to);
      }
    }
    assert(((int)(bfs).size()) == nnodes);
    nodes[root].fail = root;
    for (int i = (0); i < (((int)(bfs).size())); ++i) {
      int at = bfs[i];
      for (int x = nodes[at].head; x != -1; x = edges[x].nxt) {
        char c = edges[x].c;
        int to = nodes[at].fail;
        while (to != root && getnxt(to, c) == -1) to = nodes[to].fail;
        to = getnxt(to, c);
        if (at == root || to == -1) to = root;
        nodes[edges[x].to].fail = to;
      }
    }
  }
};
struct Kmp {
  vector<int> nxt;
  char *s;
  int slen;
  void init(char *_s) {
    s = _s, slen = strlen(s);
    nxt = vector<int>(slen + 1);
    nxt[0] = nxt[1] = 0;
    for (int i = (2); i <= (slen); ++i) {
      int at = nxt[i - 1];
      while (at != 0 && s[i - 1] != s[at]) at = nxt[at];
      if (s[i - 1] == s[at]) ++at;
      nxt[i] = at;
    }
  }
  int count(char *t) {
    int ret = 0, at = 0, tlen = strlen(t);
    for (int i = (0); i < (tlen); ++i) {
      while (at != 0 && t[i] != s[at]) at = nxt[at];
      if (t[i] == s[at]) ++at;
      if (at == slen) ++ret, at = nxt[at];
    }
    return ret;
  }
};
int par[MAXN], up[MAXN][MAXLG + 1], sz[MAXN], dep[MAXN], heavy[MAXN];
char parc[MAXN];
int hroot[MAXN], hlen[MAXN], hdn[MAXN], hup[MAXN];
char s[2 * MAXN + 1];
int slen;
void dfsinit(int at) {
  dep[at] = par[at] == -1 ? 0 : dep[par[at]] + 1, sz[at] = 1, heavy[at] = -1;
  up[at][0] = par[at] == -1 ? at : par[at];
  for (int k = (0); k < (MAXLG); ++k) up[at][k + 1] = up[up[at][k]][k];
  for (int i = (0); i < (((int)(e[at]).size())); ++i) {
    int to = e[at][i].first;
    if (to == par[at]) continue;
    par[to] = at, parc[to] = e[at][i].second;
    dfsinit(to);
    sz[at] += sz[to];
    if (heavy[at] == -1 || sz[to] > sz[heavy[at]]) heavy[at] = to;
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int k = MAXLG; k >= 0; --k)
    if (dep[a] - (1 << k) >= dep[b]) a = up[a][k];
  for (int k = MAXLG; k >= 0; --k)
    if (up[a][k] != up[b][k]) a = up[a][k], b = up[b][k];
  return a == b ? a : up[a][0];
}
vector<pair<int, int>> getpath(int a, int b) {
  int c = lca(a, b);
  vector<pair<int, int>> retup, retdn;
  while (hroot[a] != hroot[c]) {
    int x = hroot[a], off = dep[a] - dep[x];
    retup.push_back(
        make_pair(hup[x] + hlen[x] - off - 1, hup[x] + hlen[x] - 1));
    a = par[hroot[a]];
  }
  if (a != c) {
    int x = hroot[a], off1 = dep[a] - dep[x], off2 = dep[c] - dep[x] + 1;
    retup.push_back(
        make_pair(hup[x] + hlen[x] - off1 - 1, hup[x] + hlen[x] - off2 - 1));
    a = c;
  }
  while (hroot[b] != hroot[c]) {
    int x = hroot[b], off = dep[b] - dep[x];
    retdn.push_back(make_pair(hdn[x], hdn[x] + off));
    b = par[hroot[b]];
  }
  if (b != c) {
    int x = hroot[b], off1 = dep[b] - dep[x], off2 = dep[c] - dep[x] + 1;
    retdn.push_back(make_pair(hdn[x] + off2, hdn[x] + off1));
    b = c;
  }
  reverse(retdn.begin(), retdn.end());
  vector<pair<int, int>> ret = retup;
  for (int i = (0); i < (((int)(retdn).size())); ++i) ret.push_back(retdn[i]);
  return ret;
}
int qid[MAXQ];
Aho aho;
Kmp kmp;
char t[2 * (MAXLEN - 1) + 1];
int tlen;
vector<vector<int>> pos;
void solve() {
  par[0] = -1, parc[0] = '-';
  dfsinit(0);
  for (int i = (0); i < (n); ++i)
    if (par[i] != -1 && heavy[par[i]] == i)
      hlen[i] = -1;
    else {
      hlen[i] = 0;
      for (int at = i; at != -1; at = heavy[at]) hroot[at] = i, ++hlen[i];
    }
  slen = 0;
  for (int i = (0); i < (n); ++i)
    if (hlen[i] == -1)
      hdn[i] = hup[i] = -1;
    else {
      hdn[i] = slen;
      slen += hlen[i];
      hup[i] = slen;
      slen += hlen[i];
      for (int at = i, j = 0; at != -1; at = heavy[at], ++j)
        s[hdn[i] + j] = s[hup[i] + hlen[i] - j - 1] = parc[at];
    }
  s[slen] = '\0';
  for (int bs = 0; bs < nq; bs += BATCHSIZE) {
    int be = min(bs + BATCHSIZE, nq);
    aho.init();
    for (int i = (bs); i < (be); ++i) qid[i] = aho.add(qs[i]);
    aho.build();
    {
      pos = vector<vector<int>>(aho.nnodes);
      int at = aho.root;
      for (int i = (0); i < (slen); ++i) {
        while (at != aho.root && aho.getnxt(at, s[i]) == -1)
          at = aho.nodes[at].fail;
        at = aho.getnxt(at, s[i]);
        if (at == -1) at = aho.root;
        for (int to = at; to != aho.root; to = aho.nodes[to].fail)
          pos[to].push_back(i);
      }
    }
    for (int i = (bs); i < (be); ++i) {
      vector<pair<int, int>> path = getpath(qa[i], qb[i]);
      kmp.init(qs[i]);
      qans[i] = 0;
      for (int j = (0); j < (((int)(path).size())); ++j) {
        int plen = path[j].second - path[j].first + 1,
            nfst = min(qlen[i] - 1, plen);
        tlen = 0;
        for (int k = (0); k < (nfst); ++k)
          t[tlen++] = s[path[j].second + 1 - nfst + k];
        for (int jj = j + 1;
             jj < ((int)(path).size()) && tlen < nfst + qlen[i] - 1; ++jj)
          for (int k = path[jj].first;
               k <= path[jj].second && tlen < nfst + qlen[i] - 1; ++k)
            t[tlen++] = s[k];
        t[tlen] = '\0';
        qans[i] += kmp.count(t);
        if (plen >= qlen[i]) {
          int l = lower_bound(pos[qid[i]].begin(), pos[qid[i]].end(),
                              path[j].first + qlen[i] - 1) -
                  pos[qid[i]].begin();
          int r = lower_bound(pos[qid[i]].begin(), pos[qid[i]].end(),
                              path[j].second + 1) -
                  pos[qid[i]].begin();
          qans[i] += r - l;
        }
      }
    }
  }
}
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n - 1); ++i) {
    int a, b;
    char c;
    scanf("%d%d %c", &a, &b, &c);
    --a, --b;
    e[a].push_back(make_pair(b, c));
    e[b].push_back(make_pair(a, c));
  }
  scanf("%d", &nq);
  for (int i = (0); i < (nq); ++i) {
    scanf("%d%d%s", &qa[i], &qb[i], qs[i]);
    --qa[i], --qb[i], qlen[i] = strlen(qs[i]);
  }
  solve();
  for (int i = (0); i < (nq); ++i) printf("%d\n", qans[i]);
}
int myrand() { return rand() % 1000 * 1000 + rand() % 1000; }
void stress() {
  n = MAXN;
  for (int i = (0); i < (n - 1); ++i) {
    int a = i + 1, b = myrand() % (i + 1);
    char c = 'a' + rand() % 26;
    e[a].push_back(make_pair(b, c));
    e[b].push_back(make_pair(a, c));
  }
  nq = MAXQ;
  for (int i = (0); i < (nq); ++i) {
    qa[i] = myrand() % n, qb[i] = myrand() % n;
    for (int j = (0); j < (MAXLEN); ++j) qs[i][j] = 'a' + rand() % 26;
    qs[i][MAXLEN] = '\0';
    qlen[i] = MAXLEN;
  }
  solve();
}
int main() {
  run();
  return 0;
}

```