#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 pr, base;   int pr2, base2;   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];   pair<int, int> g[maxn];   pair<int, int> gi[maxn];   vector<pair<int, int> > 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;     pair<int, int> hs = make_pair(0, 0);     for (int i = (0); i < (min(100, lev[u])); ++i) {       char c = from[w];       hs.first = mult(hs.first, base, pr);       addmod(hs.first, c, pr);       hs.second = mult(hs.second, base2, pr2);       addmod(hs.second, c, pr2);       int k = lower_bound((vals).begin(), (vals).end(), hs) - vals.begin();       if (k < int((vals).size()) && vals[k] == hs) {         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;     hs = make_pair(0, 0);     for (int i = (0); i < (min(100, lev[u])); ++i) {       char c = from[w];       hs.first = mult(hs.first, base, pr);       addmod(hs.first, c, pr);       hs.second = mult(hs.second, base2, pr2);       addmod(hs.second, c, pr2);       int k = lower_bound((vals).begin(), (vals).end(), hs) - vals.begin();       if (k < int((vals).size()) && vals[k] == hs) {         cnt[k]--;       }       w = par[0][w];     }   }   int ispr(int k) {     for (int i = 2; i * i <= k; i++) {       if (k % i == 0) {         return 0;       }     }     return 1;   }   void chemthan() {     pr = INF + myrand() % (INF / 100);     while (!ispr(pr)) pr++;     base = 1234 + myrand() % 1234;     pr2 = INF + myrand() % (INF / 100);     while (!ispr(pr2)) pr2++;     base2 = 1234 + myrand() % 1234;     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());       for (char c : s[i]) {         g[i].first = mult(g[i].first, base, pr);         addmod(g[i].first, c, pr);         g[i].second = mult(g[i].second, base2, pr2);         addmod(g[i].second, c, pr2);       }       reverse((s[i]).begin(), (s[i]).end());       for (char c : s[i]) {         gi[i].first = mult(gi[i].first, base, pr);         addmod(gi[i].first, c, pr);         gi[i].second = mult(gi[i].second, base2, pr2);         addmod(gi[i].second, c, pr2);       }       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(hi[i], 1)));             que[nt].push_back(make_pair(i, make_pair(hi[i], -1)));           } else {             que[t].push_back(make_pair(i, make_pair(h[i], 1)));             que[nt].push_back(make_pair(i, make_pair(h[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;   }