#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD  1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef ll ValType;
template<typename  T> void maxtt(T& t1, T t2) {
    t1=max(t1,t2);
}
template<typename  T> void mintt(T& t1, T t2) {
    t1=min(t1,t2);
}
#define MAX 100135

bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"YES" : "NO");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    assert(y>=0);
    x+=y; if (x>=mod) x-=mod;
    assert(x>=0 && x<mod);
}
void et(int x=-1) {
    printf("%d\n", x); exit(0);
}
ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
ll gcd1(ll x, ll y) { return y?gcd1(y,x%y):x;}

vector<pair<int, char>> mp[MAX];
char fac[MAX];
//string ins="";
map<string, int> mpc;
int strId,Ans[MAX], dfn[MAX];
vector<int> spos[MAX*2];
int qylr(int id, int l, int r) {
    if(r<l) return 0;
    return upper_bound(spos[id].begin(), spos[id].end(), r) - lower_bound(spos[id].begin(), spos[id].end(), l);
}
struct mb {
    int u,v,id;
    string x, rx="";
    void  rd(int i) {
        id=i;
        cin>>u>>v>>x;
        if (mpc.find(x)==mpc.end()) mpc[x]=++strId;
        for(int i=(int)x.size()-1;~i;i--) rx.push_back(x[i]);
        if (mpc.find(rx)==mpc.end()) mpc[rx]=++strId;
    }
}qs[MAX];
struct kmp {
    int nxt[200];
    void init(string &str) {
        int k=-1,j=0;
        nxt[0]=-1;
        int plen=(int)str.size();
        while (j<plen) {
            while (k!=-1&&str[k]!=str[j]) k=nxt[k];
            nxt[++j]=++k;
        }
    }
    int Kmp(string &s, string &p) {
        int j=0,k=0,ans=0,slen=(int)s.size(),plen=(int)p.size();
        while (j<slen) {
            while (k!=-1&&s[j]!=p[k]) k=nxt[k];
            j++; k++;
            if (k==plen) {ans++;k=nxt[k];}
        }
        return ans;
    }
}KP;
struct Hld {
    int fa[MAX],bigSon[MAX],sz[MAX],deep[MAX], fap[MAX][20];
    int head[MAX];
    int dfid=0;
    void calSize(int x, int pa, char fc) {
        sz[x]=1; fa[x]=pa; deep[x]=deep[pa]+1;
        fac[x]=fc;
        fap[x][0]=pa;

        int mx = 0;
        for(auto cp: mp[x]) if (cp.first!=pa) {
            int c=cp.first;
            calSize(c, x, cp.second);
            sz[x]+=sz[c];
            if (mx < sz[c]) {
                mx = sz[c]; bigSon[x]=c;
            }
        }
    }
    void init(){
        for(int j=1; (1<<j)<=n; j++)
            for(int i=1; i<=n; i++)
                fap[i][j]=fap[fap[i][j-1]][j-1];
    }

    void build(int x, int H) {
        head[x]=H; dfn[x]=++dfid;

        if (mp[x].size()==1) {//leaf
            //build sam
            vector<int> ins;
            for(int tmp=x; ; tmp=fa[tmp]) {
                ins.push_back(tmp);
                if (tmp==H) break;
            }
            reverse(ins.begin(), ins.end());

            int N=ins.size();
            REP(i, N) {
                string x="";
                for(int j=i;j<min(N,i+100);j++) {
                    x.push_back(fac[ins[j]]);
                    if (mpc.find(x) != mpc.end()) {
                        spos[mpc[x]].push_back(dfn[ins[j]]);
                    }
                }
            }

        }
        if (bigSon[x] != 0) build(bigSon[x], H);
        for(auto c:mp[x]) if (c.first!=fa[x] && c.first!=bigSon[x]) {
            build(c.first, c.first);
        }
    }
    int lca(int x, int y) {
        while (head[x] != head[y]) {
            if (deep[head[x]] < deep[head[y]]) {
                swap(x,y);
            }
            x = fa[head[x]];
        }
        return deep[x]<deep[y] ? x : y;
    }

    string apped;
    int TT=0;
    void up(int x, int ff, string &s, int sn, int sid) {
        while(deep[head[x]]>deep[ff]) {
            int H=head[x];
            TT+=qylr(sid, dfn[head[x]], dfn[x]-sn+1);
            if (sn!=1) {
                int tmp = x;
                for(int i=18;i>=0;i--) {
                    if (deep[fap[tmp][i]]>= deep[head[x]]+sn-2) tmp=fap[tmp][i];
                }
                while(deep[tmp]> deep[head[x]]+sn-2) tmp=fa[tmp];

                string S="";
                while(tmp != ff && S.size()<sn+sn-2) {
                    S+=fac[tmp]; tmp=fa[tmp];
                }
                REP(j, apped.size()) if (S.size()==sn+sn-2) break;
                else S+=apped[j];

                reverse(S.begin(), S.end());
                TT+=KP.Kmp(S, s);
            }
            x=fa[H];
        }
        TT+=qylr(sid, dfn[ff]+1, dfn[x]-sn+1);
        if (sn<=1) return;

        int tmp = x;
        for(int i=18;i>=0;i--) {
            if (deep[fap[tmp][i]]>= deep[ff]+sn-1) tmp=fap[tmp][i];
        }
        while(deep[tmp]>deep[ff]+sn-1) tmp=fa[tmp];
        if(ff==tmp) return;
        string S="";
        while(tmp != ff && S.size()<sn+sn-2) {
            S+=fac[tmp]; tmp=fa[tmp];
        }
        REP(j, apped.size()) if (S.size()==sn+sn-2) break;
        else S+=apped[j];

        reverse(S.begin(), S.end());
        TT+=KP.Kmp(S, s);

    }
    bool DB=false;
    void down(int x, int ff, string &s, int sn, int sid) {
        while(deep[head[x]]>deep[ff]) {
            int H=head[x];
            if (DB && deep[x]==128) {
                int tmp=x;
                while(1) {
                    cout<<fac[tmp]; if(tmp==H) break;
                    tmp=fa[tmp];
                }cout<<endl;
                //13320
                int id =mpc["qx"];
                cout<<spos[id].size()<<endl;
                for(int z: spos[id]) if (z<=dfn[x] && z>=dfn[H]) cout<<z<<" ";
                exit(0);
            }
            TT+=qylr(sid, dfn[head[x]], dfn[x]-(sn-1));
            if (sn!=1) {
                int tmp = x;
                for(int i=18;i>=0;i--) {
                    if (deep[fap[tmp][i]]>= deep[head[x]]+sn-2) tmp=fap[tmp][i];
                }
                while(deep[tmp]> deep[head[x]]+sn-2) tmp=fa[tmp];

                string S="";
                while(tmp != ff && S.size()<sn+sn-2) {
                    S+=fac[tmp]; tmp=fa[tmp];
                }

                reverse(S.begin(), S.end());
                TT+=KP.Kmp(S, s);
            }
            x=fa[H];
        }
        TT+=qylr(sid, dfn[ff]+1, dfn[x]-sn+1);

    }

    void cal(int qid) {
        int x=qs[qid].u, y=qs[qid].v;
        if(x==y) return;
        int ff = lca(x, y);
        DB  =debug && qid==6730;
//        if (debug && qid==6730) {
//            cout<<ff<<endl;
//            int tmp=x;
//            while(tmp!=ff) {
//                cout<<fac[tmp]; tmp=fa[tmp];
//            }cout<<endl;
//            tmp=y;
//            while(tmp!=ff) {
//                cout<<fac[tmp]; tmp=fa[tmp];
//            }cout<<endl;
//            exit(0);
//        }
        apped="";
        int tmp = y;
        for(int i=18;i>=0;i--) {
            if (deep[fap[tmp][i]]>= deep[ff]+100) tmp=fap[tmp][i];
        }
        while(deep[tmp]>deep[ff]) {
            apped += fac[tmp]; tmp=fa[tmp];
        }
        reverse(apped.begin(), apped.end());
        TT=0;
        KP.init(qs[qid].rx);
        up(x,ff,qs[qid].rx, (int)qs[qid].rx.size(), mpc[qs[qid].rx]);
        KP.init(qs[qid].x);
        down(y,ff,qs[qid].x, (int)qs[qid].x.size(), mpc[qs[qid].x]);
        Ans[qs[qid].id] =TT;
        if (DB) exit(0);
    }
}ynk;
void fmain(int tid) {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin>>n;
    debug = n==50000;

    REP(i,n-1) {
        int u,v; char c; cin>>u>>v>>c;
        mp[u].push_back({v,c});
        mp[v].push_back({u,c});
    }
    cin>>m;
    REQ(i,m) {
        qs[i].rd(i);
    }

    ynk.calSize(1, 0, ' ');
    ynk.build(1, 1);
    ynk.init();

    REQ(i,m) {
        ynk.cal(i);
        if (debug && i==6730) {
            cout<<qs[i].u<<" "<<qs[i].v<<" "<<qs[i].x;
            return;
        }
    }
    REQ(i,m) cout<<Ans[i]<<endl;
}

int main() {
    int t=1;
//    scanf("%d", &t);
    REQ(i,t) {
        fmain(i);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD  1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef ll ValType;
template<typename  T> void maxtt(T& t1, T t2) {
    t1=max(t1,t2);
}
template<typename  T> void mintt(T& t1, T t2) {
    t1=min(t1,t2);
}
#define MAX 100035

bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"YES" : "NO");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    assert(y>=0);
    x+=y; if (x>=mod) x-=mod;
    assert(x>=0 && x<mod);
}
void et(int x=-1) {
    printf("%d\n", x); exit(0);
}
ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
ll gcd1(ll x, ll y) { return y?gcd1(y,x%y):x;}

vector<pair<int, char>> mp[MAX];
char fac[MAX];
//string ins="";
map<string, int> mpc;
int strId,Ans[MAX], dfn[MAX];
vector<int> spos[MAX*2];
int qylr(int id, int l, int r) {
    if(r<l) return 0;
    return upper_bound(spos[id].begin(), spos[id].end(), r) - lower_bound(spos[id].begin(), spos[id].end(), l);
}
struct mb {
    int u,v,id;
    string x, rx="";
    void  rd(int i) {
        id=i;
        cin>>u>>v>>x;
        if (mpc.find(x)==mpc.end()) mpc[x]=++strId;
        for(int i=(int)x.size()-1;~i;i--) rx.push_back(x[i]);
        if (mpc.find(rx)==mpc.end()) mpc[rx]=++strId;
    }
}qs[MAX];
struct kmp {
    int nxt[200];
    void init(string &str) {
        int k=-1,j=0;
        nxt[0]=-1;
        int plen=(int)str.size();
        while (j<plen) {
            while (k!=-1&&str[k]!=str[j]) k=nxt[k];
            nxt[++j]=++k;
        }
    }
    int Kmp(string &s, string &p) {
        int j=0,k=0,ans=0,slen=(int)s.size(),plen=(int)p.size();
        while (j<slen) {
            while (k!=-1&&s[j]!=p[k]) k=nxt[k];
            j++; k++;
            if (k==plen) {ans++;k=nxt[k];}
        }
        return ans;
    }
}KP;
struct Hld {
    int fa[MAX],bigSon[MAX],sz[MAX],deep[MAX], fap[MAX][20];
    int head[MAX];
    int dfid=0;
    void calSize(int x, int pa, char fc) {
        sz[x]=1; fa[x]=pa; deep[x]=deep[pa]+1;
        fac[x]=fc;
        fap[x][0]=pa;

        int mx = 0;
        for(auto cp: mp[x]) if (cp.first!=pa) {
            int c=cp.first;
            calSize(c, x, cp.second);
            sz[x]+=sz[c];
            if (mx < sz[c]) {
                mx = sz[c]; bigSon[x]=c;
            }
        }
    }
    void init(){
        for(int j=1; (1<<j)<=n; j++)
            for(int i=1; i<=n; i++)
                fap[i][j]=fap[fap[i][j-1]][j-1];
    }

    void build(int x, int H) {
        head[x]=H; dfn[x]=++dfid;

        if (mp[x].size()==1) {//leaf
            //build sam
            vector<int> ins;
            for(int tmp=x; ; tmp=fa[tmp]) {
                ins.push_back(tmp);
                if (tmp==H) break;
            }
            reverse(ins.begin(), ins.end());

            int N=ins.size();
            REP(i, N) {
                string x="";
                for(int j=i;j<min(N,i+100);j++) {
                    x.push_back(fac[ins[j]]);
                    if (mpc.find(x) != mpc.end()) {
                        spos[mpc[x]].push_back(dfn[ins[i]]);
                    }
                }
            }

        }
        if (bigSon[x] != 0) build(bigSon[x], H);
        for(auto c:mp[x]) if (c.first!=fa[x] && c.first!=bigSon[x]) {
            build(c.first, c.first);
        }
    }
    int lca(int x, int y) {
        while (head[x] != head[y]) {
            if (deep[head[x]] < deep[head[y]]) {
                swap(x,y);
            }
            x = fa[head[x]];
        }
        return deep[x]<deep[y] ? x : y;
    }

    string apped;
    int TT=0;
    void up(int x, int ff, string &s, int sn, int sid) {
        while(deep[head[x]]>deep[ff]) {
            int H=head[x];
            TT+=qylr(sid, dfn[head[x]], dfn[x]-sn+1);
            if (sn!=1) {
                int tmp = x;
                for(int i=18;i>=0;i--) {
                    if (deep[fap[tmp][i]]>= deep[head[x]]+sn-2) tmp=fap[tmp][i];
                }
                while(deep[tmp]> deep[head[x]]+sn-2) tmp=fa[tmp];

                string S="";
                while(tmp != ff && S.size()<sn+sn-2) {
                    S+=fac[tmp]; tmp=fa[tmp];
                }
                REP(j, apped.size()) if (S.size()==sn+sn-2) break;
                else S+=apped[j];

                reverse(S.begin(), S.end());
                TT+=KP.Kmp(S, s);
            }
            x=fa[H];
        }
        TT+=qylr(sid, dfn[ff]+1, dfn[x]-sn+1);
        if (sn<=1) return;

        int tmp = x;
        for(int i=18;i>=0;i--) {
            if (deep[fap[tmp][i]]>= deep[ff]+sn-1) tmp=fap[tmp][i];
        }
        while(deep[tmp]>deep[ff]+sn-1) tmp=fa[tmp];
        if(ff==tmp) return;
        string S="";
        while(tmp != ff && S.size()<sn+sn-2) {
            S+=fac[tmp]; tmp=fa[tmp];
        }
        REP(j, apped.size()) if (S.size()==sn+sn-2) break;
        else S+=apped[j];

        reverse(S.begin(), S.end());
        TT+=KP.Kmp(S, s);

    }
    bool DB=false;
    void down(int x, int ff, string &s, int sn, int sid) {
        while(deep[head[x]]>deep[ff]) {
            int H=head[x];
            if (DB && deep[x]==128) {
                int tmp=x;
                while(1) {
                    cout<<dfn[tmp]<<" ";
                    if(tmp==H) break;
                    tmp=fa[tmp];
                }cout<<endl;
                //13320
                int id =mpc["qx"];
                cout<<spos[id].size()<<endl;
                for(int z: spos[id]) if (z<=dfn[x] && z>=dfn[H]) cout<<z<<" ";
                exit(0);
            }
            TT+=qylr(sid, dfn[head[x]], dfn[x]-(sn-1));
            if (sn!=1) {
                int tmp = x;
                for(int i=18;i>=0;i--) {
                    if (deep[fap[tmp][i]]>= deep[head[x]]+sn-2) tmp=fap[tmp][i];
                }
                while(deep[tmp]> deep[head[x]]+sn-2) tmp=fa[tmp];

                string S="";
                while(tmp != ff && S.size()<sn+sn-2) {
                    S+=fac[tmp]; tmp=fa[tmp];
                }

                reverse(S.begin(), S.end());
                TT+=KP.Kmp(S, s);
            }
            x=fa[H];
        }
        TT+=qylr(sid, dfn[ff]+1, dfn[x]-sn+1);

    }

    void cal(int qid) {
        int x=qs[qid].u, y=qs[qid].v;
        if(x==y) return;
        int ff = lca(x, y);
//        DB  =debug && qid==6730;
//        if (debug && qid==6730) {
//            cout<<ff<<endl;
//            int tmp=x;
//            while(tmp!=ff) {
//                cout<<fac[tmp]; tmp=fa[tmp];
//            }cout<<endl;
//            tmp=y;
//            while(tmp!=ff) {
//                cout<<fac[tmp]; tmp=fa[tmp];
//            }cout<<endl;
//            exit(0);
//        }
        apped="";
        int tmp = y;
        for(int i=18;i>=0;i--) {
            if (deep[fap[tmp][i]]>= deep[ff]+100) tmp=fap[tmp][i];
        }
        while(deep[tmp]>deep[ff]) {
            apped += fac[tmp]; tmp=fa[tmp];
        }
        reverse(apped.begin(), apped.end());
        TT=0;
        KP.init(qs[qid].rx);
        up(x,ff,qs[qid].rx, (int)qs[qid].rx.size(), mpc[qs[qid].rx]);
        KP.init(qs[qid].x);
        down(y,ff,qs[qid].x, (int)qs[qid].x.size(), mpc[qs[qid].x]);
        Ans[qs[qid].id] =TT;
        if (DB) exit(0);
    }
}ynk;
void fmain(int tid) {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin>>n;
    debug = n==99900;

    REP(i,n-1) {
        int u,v; char c; cin>>u>>v>>c;
        mp[u].push_back({v,c});
        mp[v].push_back({u,c});
    }
    cin>>m;
    REQ(i,m) {
        qs[i].rd(i);
    }

    ynk.calSize(1, 0, ' ');
    ynk.build(1, 1);
    ynk.init();

    REQ(i,m) {
        ynk.cal(i);
        if (debug && i==10944) {
            cout<<qs[i].u<<" "<<qs[i].v<<" "<<qs[i].x;
            return;
        }
    }
    REQ(i,m) cout<<Ans[i]<<endl;
}

int main() {
    int t=1;
//    scanf("%d", &t);
    REQ(i,t) {
        fmain(i);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD  1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef ll ValType;
template<typename  T> void maxtt(T& t1, T t2) {
    t1=max(t1,t2);
}
template<typename  T> void mintt(T& t1, T t2) {
    t1=min(t1,t2);
}
#define MAX 100135

bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"YES" : "NO");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    assert(y>=0);
    x+=y; if (x>=mod) x-=mod;
    assert(x>=0 && x<mod);
}
void et(int x=-1) {
    printf("%d\n", x); exit(0);
}
ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
ll gcd1(ll x, ll y) { return y?gcd1(y,x%y):x;}

vector<pair<int, char>> mp[MAX];
char fac[MAX];
//string ins="";
map<string, int> mpc;
int strId,Ans[MAX], dfn[MAX];
vector<int> spos[MAX*2];
int qylr(int id, int l, int r) {
    if(r<l) return 0;
    return upper_bound(spos[id].begin(), spos[id].end(), r) - lower_bound(spos[id].begin(), spos[id].end(), l);
}
struct mb {
    int u,v,id;
    string x, rx="";
    void  rd(int i) {
        id=i;
        cin>>u>>v>>x;
        if (mpc.find(x)==mpc.end()) mpc[x]=++strId;
        for(int i=(int)x.size()-1;~i;i--) rx.push_back(x[i]);
        if (mpc.find(rx)==mpc.end()) mpc[rx]=++strId;
    }
}qs[MAX];
struct kmp {
    int nxt[200];
    void init(string &str) {
        int k=-1,j=0;
        nxt[0]=-1;
        int plen=(int)str.size();
        while (j<plen) {
            while (k!=-1&&str[k]!=str[j]) k=nxt[k];
            nxt[++j]=++k;
        }
    }
    int Kmp(string &s, string &p) {
        int j=0,k=0,ans=0,slen=(int)s.size(),plen=(int)p.size();
        while (j<slen) {
            while (k!=-1&&s[j]!=p[k]) k=nxt[k];
            j++; k++;
            if (k==plen) {ans++;k=nxt[k];}
        }
        return ans;
    }
}KP;
struct Hld {
    int fa[MAX],bigSon[MAX],sz[MAX],deep[MAX], fap[MAX][20];
    int head[MAX];
    int dfid=0;
    void calSize(int x, int pa, char fc) {
        sz[x]=1; fa[x]=pa; deep[x]=deep[pa]+1;
        fac[x]=fc;
        fap[x][0]=pa;

        int mx = 0;
        for(auto cp: mp[x]) if (cp.first!=pa) {
            int c=cp.first;
            calSize(c, x, cp.second);
            sz[x]+=sz[c];
            if (mx < sz[c]) {
                mx = sz[c]; bigSon[x]=c;
            }
        }
    }
    void init(){
        for(int j=1; (1<<j)<=n; j++)
            for(int i=1; i<=n; i++)
                fap[i][j]=fap[fap[i][j-1]][j-1];
    }

    void build(int x, int H) {
        head[x]=H; dfn[x]=++dfid;

        if (mp[x].size()==1) {//leaf
            //build sam
            vector<int> ins;
            for(int tmp=x; ; tmp=fa[tmp]) {
                ins.push_back(tmp);
                if (tmp==H) break;
            }
            reverse(ins.begin(), ins.end());

            int N=ins.size();
            REP(i, N) {
                string x="";
                for(int j=i;j<min(N,i+100);j++) {
                    x.push_back(fac[ins[j]]);
                    if (mpc.find(x) != mpc.end()) {
                        spos[mpc[x]].push_back(dfn[ins[i]]);
                    }
                }
            }

        }
        if (bigSon[x] != 0) build(bigSon[x], H);
        for(auto c:mp[x]) if (c.first!=fa[x] && c.first!=bigSon[x]) {
            build(c.first, c.first);
        }
    }
    int lca(int x, int y) {
        while (head[x] != head[y]) {
            if (deep[head[x]] < deep[head[y]]) {
                swap(x,y);
            }
            x = fa[head[x]];
        }
        return deep[x]<deep[y] ? x : y;
    }

    string apped;
    int TT=0;
    void up(int x, int ff, string &s, int sn, int sid) {
        while(deep[head[x]]>deep[ff]) {
            int H=head[x];
            TT+=qylr(sid, dfn[head[x]], dfn[x]-sn+1);
            if (sn!=1) {
                int tmp = x;
                for(int i=18;i>=0;i--) {
                    if (deep[fap[tmp][i]]>= deep[head[x]]+sn-2) tmp=fap[tmp][i];
                }
                while(deep[tmp]> deep[head[x]]+sn-2) tmp=fa[tmp];

                string S="";
                while(tmp != ff && S.size()<sn+sn-2) {
                    S+=fac[tmp]; tmp=fa[tmp];
                }
                REP(j, apped.size()) if (S.size()==sn+sn-2) break;
                else S+=apped[j];

                reverse(S.begin(), S.end());
                TT+=KP.Kmp(S, s);
            }
            x=fa[H];
        }
        TT+=qylr(sid, dfn[ff]+1, dfn[x]-sn+1);
        if (sn<=1) return;

        int tmp = x;
        for(int i=18;i>=0;i--) {
            if (deep[fap[tmp][i]]>= deep[ff]+sn-1) tmp=fap[tmp][i];
        }
        while(deep[tmp]>deep[ff]+sn-1) tmp=fa[tmp];
        if(ff==tmp) return;
        string S="";
        while(tmp != ff && S.size()<sn+sn-2) {
            S+=fac[tmp]; tmp=fa[tmp];
        }
        REP(j, apped.size()) if (S.size()==sn+sn-2) break;
        else S+=apped[j];

        reverse(S.begin(), S.end());
        TT+=KP.Kmp(S, s);

    }
    bool DB=false;
    void down(int x, int ff, string &s, int sn, int sid) {
        while(deep[head[x]]>deep[ff]) {
            int H=head[x];
            if (DB && deep[x]==128) {
                int tmp=x;
                while(1) {
                    cout<<dfn[tmp]<<" ";
                    if(tmp==H) break;
                    tmp=fa[tmp];
                }cout<<endl;
                //13320
                int id =mpc["qx"];
                cout<<spos[id].size()<<endl;
                for(int z: spos[id]) if (z<=dfn[x] && z>=dfn[H]) cout<<z<<" ";
                exit(0);
            }
            TT+=qylr(sid, dfn[head[x]], dfn[x]-(sn-1));
            if (sn!=1) {
                int tmp = x;
                for(int i=18;i>=0;i--) {
                    if (deep[fap[tmp][i]]>= deep[head[x]]+sn-2) tmp=fap[tmp][i];
                }
                while(deep[tmp]> deep[head[x]]+sn-2) tmp=fa[tmp];

                string S="";
                while(tmp != ff && S.size()<sn+sn-2) {
                    S+=fac[tmp]; tmp=fa[tmp];
                }

                reverse(S.begin(), S.end());
                TT+=KP.Kmp(S, s);
            }
            x=fa[H];
        }
        TT+=qylr(sid, dfn[ff]+1, dfn[x]-sn+1);

    }

    void cal(int qid) {
        int x=qs[qid].u, y=qs[qid].v;
        if(x==y) return;
        int ff = lca(x, y);
        DB  =debug && qid==6730;
//        if (debug && qid==6730) {
//            cout<<ff<<endl;
//            int tmp=x;
//            while(tmp!=ff) {
//                cout<<fac[tmp]; tmp=fa[tmp];
//            }cout<<endl;
//            tmp=y;
//            while(tmp!=ff) {
//                cout<<fac[tmp]; tmp=fa[tmp];
//            }cout<<endl;
//            exit(0);
//        }
        apped="";
        int tmp = y;
        for(int i=18;i>=0;i--) {
            if (deep[fap[tmp][i]]>= deep[ff]+100) tmp=fap[tmp][i];
        }
        while(deep[tmp]>deep[ff]) {
            apped += fac[tmp]; tmp=fa[tmp];
        }
        reverse(apped.begin(), apped.end());
        TT=0;
        KP.init(qs[qid].rx);
        up(x,ff,qs[qid].rx, (int)qs[qid].rx.size(), mpc[qs[qid].rx]);
        KP.init(qs[qid].x);
        down(y,ff,qs[qid].x, (int)qs[qid].x.size(), mpc[qs[qid].x]);
        Ans[qs[qid].id] =TT;
        if (DB) exit(0);
    }
}ynk;
void fmain(int tid) {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin>>n;
    debug = n==99900;

    REP(i,n-1) {
        int u,v; char c; cin>>u>>v>>c;
        mp[u].push_back({v,c});
        mp[v].push_back({u,c});
    }
    cin>>m;
    REQ(i,m) {
        qs[i].rd(i);
    }

    ynk.calSize(1, 0, ' ');
    ynk.build(1, 1);
    ynk.init();

    REQ(i,m) {
        ynk.cal(i);
        if (debug && i==10944) {
            cout<<qs[i].u<<" "<<qs[i].v<<" "<<qs[i].x;
            return;
        }
    }
    REQ(i,m) cout<<Ans[i]<<endl;
}

int main() {
    int t=1;
//    scanf("%d", &t);
    REQ(i,t) {
        fmain(i);
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=4200050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<22)-1;
const ll base1=51646229,base2=19260817;
const ll MOD1=1222827239,MOD2=998244353;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq],hsh3[maxq],hsh4[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k1,ll k2){
	ll k=k1*k2;
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k1,ll k2){
	ll k=k1*k2;
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k1,ll k2){
	ll k=k1*k2;
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id],hsh2[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id],hsh2[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh3[id],hsh4[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh3[id],hsh4[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	ll _hsh1,_hsh2;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		_hsh1=0;_hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base1%MOD1+stk[i])%MOD1;
			_hsh2=(_hsh2*base2%MOD2+stk[i])%MOD2;
			insert_hsh(_hsh1,_hsh2);
		}
		dfs2(v,u);
		_hsh1=0;_hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base1%MOD1+stk[i])%MOD1;
			_hsh2=(_hsh2*base2%MOD2+stk[i])%MOD2;
			delete_hsh(_hsh1,_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++){
			_hsh1=(_hsh1*base1%MOD1+t[i])%MOD1;
			_hsh2=(_hsh2*base2%MOD2+t[i])%MOD2;
		}
		hsh1[i]=_hsh1;hsh2[i]=_hsh2;
		ll _hsh3=0,_hsh4=0;
		for(int i=len_t-1;i>=0;i--){
			_hsh3=(_hsh3*base1%MOD1+t[i])%MOD1;
			_hsh4=(_hsh4*base2%MOD2+t[i])%MOD2;
		}
		hsh3[i]=_hsh3;hsh4[i]=_hsh4;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1000050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<20)-1;
const ll base=19260817;
const ll MOD=1e9+7;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	if(n>=500)	printf("fucker!\n");
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}
/*
15
1 2 z
1 3 l
4 2 l
5 4 t
3 6 m
7 1 j
5 8 l
8 9 h
8 10 i
6 11 s
12 4 z
13 9 d
11 14 k
15 12 f
14
15 7 fzlzj
13 10 dhi
13 10 ihd
13 14 l
13 14 dhltlzlmsk
13 15 l
13 15 z
15 13 l
15 13 z
7 3 j
13 14 zl
15 14 zl
10 14 l
14 10 l
*/


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1100050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<20)-1;
const ll base=19260817;
const ll MOD=998244353;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1100050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<20)-1;
const ll base=51646229;
const ll MOD=1222827239;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1+stk[i])*base%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2+stk[i])*base%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1+t[i])*base%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2+t[i])*base%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1100050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<20)-1;
const ll base=10009777;
const ll MOD=1222827239;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=4200050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<22)-1;
const ll base=51646229;
const ll xor_val=748329234;
const ll MOD=1222827239;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k){
	int x=(int)((k^xor_val)&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)((k^xor_val)&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)((k^xor_val)&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1+stk[i])*base%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2+stk[i])*base%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1+t[i])*base%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2+t[i])*base%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1000050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<20)-1;
const ll base=19260817;
const ll MOD=1e9+7;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	printf("fucker!\n");
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}
/*
15
1 2 z
1 3 l
4 2 l
5 4 t
3 6 m
7 1 j
5 8 l
8 9 h
8 10 i
6 11 s
12 4 z
13 9 d
11 14 k
15 12 f
14
15 7 fzlzj
13 10 dhi
13 10 ihd
13 14 l
13 14 dhltlzlmsk
13 15 l
13 15 z
15 13 l
15 13 z
7 3 j
13 14 zl
15 14 zl
10 14 l
14 10 l
*/


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1100050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<20)-1;
const ll base=10000019;
const ll MOD=998244353;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1000050;
const int maxq=100050;
const int maxt=205;
const ll bitmask=(1<<20)-1;
const ll base=19260817;
const ll MOD=1e9+7;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal1(int u0,int v0,int id){

}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		s[len_s]='\0';
		reverse(s+(dep[_lca]-dep[u0]),s+(dep[_lca]-dep[u0])+(dep[_lca]-dep[v0]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1000050;
const int maxq=100050;
const int maxt=205;
const ll bitmask=(1<<20)-1;
const ll base=19260817;
const ll MOD=1e9+7;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal1(int u0,int v0,int id){

}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		s[len_s]='\0';
		reverse(s+(dep[_lca]-dep[u0]),s+(dep[_lca]-dep[u0])+(dep[_lca]-dep[v0]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1000050;
const int maxq=100050;
const int maxt=205;
const ll bitmask=(1<<20)-1;
const ll base=19260817;
const ll MOD=1e9+7;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal1(int u0,int v0,int id){

}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		s[len_s]='\0';
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}
/*
15
1 2 z
1 3 l
4 2 l
5 4 t
3 6 m
7 1 j
5 8 l
8 9 h
8 10 i
6 11 s
12 4 z
13 9 d
11 14 k
15 12 f
14
15 7 fzlzj
13 10 dhi
13 10 ihd
13 14 l
13 14 dhltlzlmsk
13 15 l
13 15 z
15 13 l
15 13 z
7 3 j
13 14 zl
15 14 zl
10 14 l
14 10 l
*/


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100050;
const int maxm=200050;
const int maxlen=20000005;
const int maxh=4200050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<22)-1;
const ll base=51646229;
const ll MOD=1222827239;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1+stk[i])*base%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2+stk[i])*base%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1+t[i])*base%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2+t[i])*base%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=1100050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<20)-1;
const ll base=19260817;
const ll MOD=1e9+7;
struct node{
	ll val;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum++;return;
		}
	}
	nd[tot].val=k;nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k){
	int x=(int)(k&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k){
	int x=(int)(k&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val==k){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh2[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh2[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		ll _hsh1=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base%MOD+stk[i])%MOD;
			insert_hsh(_hsh1);
		}
		dfs2(v,u);
		ll _hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh2=(_hsh2*base%MOD+stk[i])%MOD;
			delete_hsh(_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);

		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++)	_hsh1=(_hsh1*base%MOD+t[i])%MOD;
		hsh1[i]=_hsh1;
		for(int i=len_t-1;i>=0;i--)	_hsh2=(_hsh2*base%MOD+t[i])%MOD;
		hsh2[i]=_hsh2;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=100050;
const int maxm=200050;
const int maxlen=10000005;
const int maxh=4200050;
const int maxq=100050;
const int maxt=305;
const ll bitmask=(1<<22)-1;
const ll base1=51646229,base2=19260817;
const ll MOD1=1222827239,MOD2=998244353;
struct node{
	ll val1,val2;
	int sum,nxt;
};
struct edge{
	int nxt,to;
	char ch;
};
int fa[maxn][20];
node nd[maxlen];
edge e[maxm];
vector<int> f1[maxn],f2[maxn],g1[maxn],g2[maxn];
ll hsh1[maxq],hsh2[maxq],hsh3[maxq],hsh4[maxq];
int hsh[maxh];
int head[maxn],dep[maxn];
int res[maxq];
int f[maxt];
char p[maxn],stk[maxn];
char s[maxt],t[maxt];
int n,q,cnt,tot,max_t,now;

void init(){
	memset(hsh,-1,sizeof(hsh));
	memset(head,-1,sizeof(head));
	cnt=tot=max_t=now=0;
}
void add(int u,int v,char ch){
	e[cnt].to=v;e[cnt].ch=ch;e[cnt].nxt=head[u];head[u]=cnt++;
}
void insert_hsh(ll k1,ll k2){
	int x=(int)((k1*k2)&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val1==k1&&nd[i].val2==k2){
			nd[i].sum++;return;
		}
	}
	nd[tot].val1=k1;nd[tot].val2=k2;
	nd[tot].sum=1;
	nd[tot].nxt=hsh[x];hsh[x]=tot++;
}
int find_hsh(ll k1,ll k2){
	int x=(int)((k1*k2)&bitmask);
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val1==k1&&nd[i].val2==k2)	return nd[i].sum;
	}
	return 0;
}
void delete_hsh(ll k1,ll k2){
	int x=(int)((k1*k2)&bitmask);
	int pre=-1;
	for(int i=hsh[x];i!=-1;i=nd[i].nxt){
		if(nd[i].val1==k1&&nd[i].val2==k2){
			nd[i].sum--;
			if(!nd[i].sum){
				if(i==hsh[x])	hsh[x]=nd[i].nxt;
				else	nd[pre].nxt=nd[i].nxt;
			}
			return;
		}
		pre=i;
	}
}
void dfs(int u,int _fa,int _dep){
	fa[u][0]=_fa;dep[u]=_dep;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		p[v]=ch;
		dfs(v,u,_dep+1);
	}
}
void get_fa(){
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i<=n;i++)	fa[i][j]=fa[fa[i][j-1]][j-1];
	}
}
int lca(int a,int b){
	if(dep[a]>dep[b])	swap(a,b);
	int len=dep[b]-dep[a];
	for(int i=0;(1<<i)<=len;i++){
		if((1<<i)&len)	b=fa[b][i];
	}
	if(a!=b){
		for(int i=(int)log2(n);i>=0;i--){
			if(fa[a][i]!=fa[b][i]){
				a=fa[a][i];b=fa[b][i];
			}
		}
		a=fa[a][0];
	}
	return a;
}
void getfail(char *t,int len_t){
	for(int i=1;i<len_t;i++){
		int j=f[i];
		while(j&&t[i]!=t[j])	j=f[j];
		f[i+1]=(t[i]==t[j])?j+1:0;
	}
}
int kmp(char *s,char *t,int len_s,int len_t){
	int j=0,ans=0;
	for(int i=0;i<len_s;i++){
		while(j&&s[i]!=t[j])	j=f[j];
		if(s[i]==t[j])	j++;
		if(j==len_t)	ans++;
	}
	return ans;
}
void cal(int u){
	for(int i=0,sz=f1[u].size();i<sz;i++){
		int id=f1[u][i];
		res[id]+=find_hsh(hsh1[id],hsh2[id]);
	}
	for(int i=0,sz=f2[u].size();i<sz;i++){
		int id=f2[u][i];
		res[id]-=find_hsh(hsh1[id],hsh2[id]);
	}
	for(int i=0,sz=g1[u].size();i<sz;i++){
		int id=g1[u][i];
		res[id]+=find_hsh(hsh3[id],hsh4[id]);
	}
	for(int i=0,sz=g2[u].size();i<sz;i++){
		int id=g2[u][i];
		res[id]-=find_hsh(hsh3[id],hsh4[id]);
	}
}
void dfs2(int u,int _fa){
	cal(u);
	ll _hsh1,_hsh2;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].to;
		if(v==_fa)	continue;
		char ch=e[i].ch;
		stk[++now]=ch;
		_hsh1=0;_hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base1%MOD1+stk[i])%MOD1;
			_hsh2=(_hsh2*base2%MOD2+stk[i])%MOD2;
			insert_hsh(_hsh1,_hsh2);
		}
		dfs2(v,u);
		_hsh1=0;_hsh2=0;
		for(int i=now;i>=1&&now-i+1<=max_t;i--){
			_hsh1=(_hsh1*base1%MOD1+stk[i])%MOD1;
			_hsh2=(_hsh2*base2%MOD2+stk[i])%MOD2;
			delete_hsh(_hsh1,_hsh2);
		}
		now--;
	}
}
void solve(){
	init();
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,s);
		add(u,v,s[0]);add(v,u,s[0]);
	}
	dfs(1,0,0);
	get_fa();
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%d %d %s",&u,&v,t);
		if(u==v)	continue;
		int len_t=strlen(t);
		max_t=max(max_t,len_t);
		int _lca=lca(u,v),u0=u,v0=v;
		int len1=dep[u]-(dep[_lca]+len_t-1);
		int len2=dep[v]-(dep[_lca]+len_t-1);
		if(len1>0){
			for(int j=0;(1<<j)<=len1;j++){
				if((1<<j)&len1)	u0=fa[u0][j];
			}
		}
		if(len2>0){
			for(int j=0;(1<<j)<=len2;j++){
				if((1<<j)&len2)	v0=fa[v0][j];
			}
		}
		f1[u].push_back(i);f2[u0].push_back(i);
		g1[v].push_back(i);g2[v0].push_back(i);
		getfail(t,len_t);
		int len_s=0;
		for(int j=u0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		for(int j=v0;j!=_lca;j=fa[j][0])	s[len_s++]=p[j];
		reverse(s+(dep[u0]-dep[_lca]),s+(dep[u0]-dep[_lca])+(dep[v0]-dep[_lca]));
		res[i]+=kmp(s,t,len_s,len_t);
		ll _hsh1=0,_hsh2=0;
		for(int i=0;i<len_t;i++){
			_hsh1=(_hsh1*base1%MOD1+t[i])%MOD1;
			_hsh2=(_hsh2*base2%MOD2+t[i])%MOD2;
		}
		hsh1[i]=_hsh1;hsh2[i]=_hsh2;
		ll _hsh3=0,_hsh4=0;
		for(int i=len_t-1;i>=0;i--){
			_hsh3=(_hsh3*base1%MOD1+t[i])%MOD1;
			_hsh4=(_hsh4*base2%MOD2+t[i])%MOD2;
		}
		hsh3[i]=_hsh3;hsh4[i]=_hsh4;
	}
	dfs2(1,0);
	for(int i=1;i<=q;i++)	printf("%d\n",res[i]);
}
int main(){
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull mod=(1ull<<61)-1,base=233;
inline ull mult(ull a,ull b)
{
	ull l1=(unsigned int)a,h1=a>>32,l2=(unsigned int)b,h2=b>>32;
	ull l=l1*l2,m=l1*h2+l2*h1,h=h1*h2;
	ull ret=(l&mod)+(l>>61)+(h<<3)+(m>>29)+(m<<35>>3)+1;
	ret=(ret&mod)+(ret>>61);
	ret=(ret&mod)+(ret>>61);
	return ret-1;
}
inline ull add(ull a,ull b)
{
	if((a+=b)>=mod)a-=mod;
	return a;
}
inline ull dec(ull a,ull b)
{
	return add(a,mod-b);
}
ull hs[100010][110],pw[100010];
int n,Q,st[100010][17],dep[100010];
char fc[100010];
inline int lca(int x,int y)
{
	if(dep[x]<dep[y])swap(x,y);
	for(int i=16;i>=0;i--)
		if(dep[st[x][i]]>=dep[y])x=st[x][i];
	if(x==y)return x;
	for(int i=16;i>=0;i--)
		if(st[x][i]!=st[y][i])x=st[x][i],y=st[y][i];
	return st[x][0];
}
inline int jump(int p,int t)
{
	for(int i=16;i>=0;i--)if((t>>i)&1)p=st[p][i];
	return p;
}
int ans[100010];
vector<pair<int,int> > q[100010];
vector<pair<int,char> > v[100010];
map<ull,int> cnt;
string s[200010];
ull vl[200010];
void dfs1(int np,int fath,char ic)
{
	dep[np]=dep[fath]+1;
	st[np][0]=fath;
	fc[np]=ic;
	for(int i=1;i<17;i++)st[np][i]=st[st[np][i-1]][i-1];
	hs[np][0]=0;
	for(int i=1;i<=100&&i<dep[np];i++)hs[np][i]=add(mult(ic-'a'+1,pw[i-1]),hs[fath][i-1]);
	for(auto &x:v[np])
	{
		if(x.first==fath)continue;
		dfs1(x.first,np,x.second);
	}
}
inline ull gethash(const string &s)
{
	ull h=0;
	for(int i=0;i<s.size();i++)
		h=add(mult(h,base),s[i]-'a'+1);
	return h;
}
int cmp(const vector<char> &a,ull key,int len)
{
	if(a.size()>len)return 0;
	ull h=0;
	int ret=0;
	for(int i=0;i<len-1;i++)h=add(mult(h,base),a[i]-'a'+1);
	for(int i=len-1;i<a.size();i++)
	{
		h=add(mult(h,base),a[i]-'a'+1);
		ret+=(h==key);
		h=dec(h,mult(pw[len-1],a[i-len+1]-'a'+1));
	}
	return ret;
}
void dfs2(int np,int fath)
{
	for(int i=1;i<dep[np]&&i<=100;i++)cnt[hs[np][i]]++;
	for(auto &x:q[np])
	{
		if(x.second==1)ans[x.first>>1]+=cnt[vl[x.first]];
		else ans[x.first>>1]-=cnt[vl[x.first]];
	}
	for(auto &x:v[np])
		if(x.first!=fath)dfs2(x.first,np);
	for(int i=1;i<dep[np]&&i<=100;i++)cnt[hs[np][i]]--;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		int a,b;
		char s[5];
		scanf("%d%d%s",&a,&b,s);
		v[a].push_back(make_pair(b,s[0]));
		v[b].push_back(make_pair(a,s[0]));
	}
	pw[0]=1;
	for(int i=1;i<=100;i++)pw[i]=mult(pw[i-1],base);
	dfs1(1,0,'a');
	scanf("%d",&Q);
	for(int i=1,a,b;i<=Q;i++)
	{
		scanf("%d%d",&a,&b);
		cin>>s[i*2];
		int cl=s[i*2].size();
		s[i*2+1]=s[i*2];
		reverse(s[i*2+1].begin(),s[i*2+1].end());
		vl[i*2]=gethash(s[i*2]);
		vl[i*2+1]=gethash(s[i*2+1]);
		int L=lca(a,b);
		vector<char> v1,v2;
		int p;
		if(dep[a]-dep[L]<cl-1)p=a;
		else p=jump(a,dep[a]-dep[L]-(cl-1));
		if(a!=p)
		{
			q[a].push_back(make_pair(i*2,1));
			q[p].push_back(make_pair(i*2,-1));
		}
		while(p!=L)v1.push_back(fc[p]),p=st[p][0];
		if(dep[b]-dep[L]<cl)p=b;
		else p=jump(b,dep[b]-dep[L]-(cl-1));
		if(b!=p)
		{
			q[b].push_back(make_pair(i*2+1,1));
			q[p].push_back(make_pair(i*2+1,-1));
		}
		while(p!=L)v2.push_back(fc[p]),p=st[p][0];
		reverse(v2.begin(),v2.end());
		v1.insert(v1.end(),v2.begin(),v2.end());
		ans[i]=cmp(v1,vl[i<<1],s[i*2].size());
	}
	dfs2(1,0);
	for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull mod=(1ull<<61)-1,base=233;
inline ull mult(ull a,ull b)
{
	ull l1=(unsigned int)a,h1=a>>32,l2=(unsigned int)b,h2=b>>32;
	ull l=l1*l2,m=l1*h2+l2*h1,h=h1*h2;
	ull ret=(l&mod)+(l>>61)+(h<<3)+(m>>29)+(m<<35>>3)+1;
	ret=(ret&mod)+(ret>>61);
	ret=(ret&mod)+(ret>>61);
	return ret-1;
}
inline ull add(ull a,ull b)
{
	if((a+=b)>=mod)a-=mod;
	return a;
}
inline ull dec(ull a,ull b)
{
	return add(a,mod-b);
}
ull hs[100010][110],pw[100010];
int n,Q,st[100010][17],dep[100010];
char fc[100010];
inline int lca(int x,int y)
{
	if(dep[x]<dep[y])swap(x,y);
	for(int i=16;i>=0;i--)
		if(dep[st[x][i]]>=dep[y])x=st[x][i];
	if(x==y)return x;
	for(int i=16;i>=0;i--)
		if(st[x][i]!=st[y][i])x=st[x][i],y=st[y][i];
	return st[x][0];
}
inline int jump(int p,int t)
{
	for(int i=16;i>=0;i--)if((t>>i)&1)p=st[p][i];
	return p;
}
int ans[100010];
vector<pair<int,int> > q[100010];
vector<pair<int,char> > v[100010];
unordered_map<ull,int> cnt;
string s[200010];
ull vl[200010];
int len[200010];
void dfs1(int np,int fath,char ic)
{
	dep[np]=dep[fath]+1;
	st[np][0]=fath;
	fc[np]=ic;
	for(int i=1;i<17;i++)st[np][i]=st[st[np][i-1]][i-1];
	hs[np][0]=0;
	for(int i=1;i<=100&&i<dep[np];i++)hs[np][i]=add(mult(ic-'a'+1,pw[i-1]),hs[fath][i-1]);
	for(auto &x:v[np])
	{
		if(x.first==fath)continue;
		dfs1(x.first,np,x.second);
	}
}
inline ull gethash(const string &s)
{
	ull h=0;
	for(int i=0;i<s.size();i++)
		h=add(mult(h,base),s[i]-'a'+1);
	return h;
}
int cmp(const vector<char> &a,ull key,int len)
{
	if(a.size()<len)return 0;
	ull h=0;
	int ret=0;
	for(int i=0;i<len-1;i++)h=add(mult(h,base),a[i]-'a'+1);
	for(int i=len-1;i<a.size();i++)
	{
		h=add(mult(h,base),a[i]-'a'+1);
		ret+=(h==key);
		h=dec(h,mult(pw[len-1],a[i-len+1]-'a'+1));
	}
	return ret;
}
void dfs2(int np,int fath,int l)
{
	if(dep[np]>l)cnt[hs[np][l]]++;
//	for(int i=1;i<dep[np]&&i<=100;i++)cnt[hs[np][i]]++;
	for(auto &x:q[np])
	{
		if(len[x.first]==l)
		{
			if(x.second==1)ans[x.first>>1]+=cnt[vl[x.first]];
			else ans[x.first>>1]-=cnt[vl[x.first]];
		}
	}
	for(auto &x:v[np])
		if(x.first!=fath)dfs2(x.first,np,l);
	if(dep[np]>l)cnt[hs[np][l]]--;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		int a,b;
		char s[5];
		scanf("%d%d%s",&a,&b,s);
		v[a].push_back(make_pair(b,s[0]));
		v[b].push_back(make_pair(a,s[0]));
	}
	pw[0]=1;
	for(int i=1;i<=100;i++)pw[i]=mult(pw[i-1],base);
	dfs1(1,0,'a');
	scanf("%d",&Q);
	for(int i=1,a,b;i<=Q;i++)
	{
		scanf("%d%d",&a,&b);
		cin>>s[i*2];
		int cl=s[i*2].size();
		s[i*2+1]=s[i*2];
		reverse(s[i*2+1].begin(),s[i*2+1].end());
		vl[i*2]=gethash(s[i*2]);
		vl[i*2+1]=gethash(s[i*2+1]);
		len[i*2]=s[i*2].size();
		len[i*2+1]=s[i*2+1].size();
		int L=lca(a,b);
		vector<char> v1,v2;
		int p;
		if(dep[a]-dep[L]<cl-1)p=a;
		else p=jump(a,dep[a]-dep[L]-(cl-1));
		if(a!=p)
		{
			q[a].push_back(make_pair(i*2,1));
			q[p].push_back(make_pair(i*2,-1));
		}
		while(p!=L)v1.push_back(fc[p]),p=st[p][0];
		if(dep[b]-dep[L]<cl)p=b;
		else p=jump(b,dep[b]-dep[L]-(cl-1));
		if(b!=p)
		{
			q[b].push_back(make_pair(i*2+1,1));
			q[p].push_back(make_pair(i*2+1,-1));
		}
		while(p!=L)v2.push_back(fc[p]),p=st[p][0];
		reverse(v2.begin(),v2.end());
		v1.insert(v1.end(),v2.begin(),v2.end());
		ans[i]=cmp(v1,vl[i<<1],s[i*2].size());
	}
	for(int i=1;i<=100;i++)
	{
		cnt.clear();
		dfs2(1,0,i);
	}
//	dfs2(1,0);
	for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
	return 0;
}