#include using namespace std; typedef long long int ll; typedef pair P; typedef vector VI; typedef vector VVI; #define REP(i,n) for(ll i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() template bool chmax(T &x, const T &y) {return (x bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;}; constexpr ll MOD=998244353; constexpr ll INF=2e18; int main(){ string x, y; cin >> x >> y; int n=x.size(), m=y.size(); VVI xl(n,VI(26,0)), xr(n,VI(26,0)), yl(m,VI(26,0)), yr(m,VI(26,0)); REP(i,n){ if(i>0) xl[i]=xl[i-1]; xl[i][x[i]-'a']++; } for(int i=n-1;i>=0;i--){ if(i0) yl[i]=yl[i-1]; yl[i][y[i]-'a']++; } for(int i=m-1;i>=0;i--){ if(i> q; int l, r; char c; while(q--){ cin >> l >> r >> c; l--, r--; int ans=0; if(l/(n+m)==r/(n+m)){ if(l%(n+m)=n&&r%(n+m)>=n){ int tmp=l/(n+m)+1; for(int i=(1<<29);i>=1;i/=2){ if(tmp%i==0){ if(tmp/i%2==0) ans+=yl[r%(n+m)-n][c-'a']-(l%(n+m)-n==0?0:xl[l%(n+m)-n-1][c-'a']); else ans+=yr[m-1-(r%(n+m)-n)][c-'a']-(l%(n+m)-n==0?0:yr[m-1-(l%(n+m)-n)+1][c-'a']); break; } } cout << ans << endl; continue; } } int xnumr=(r+m+1)/(n+m); int xnuml=(l+n+m-1)/(n+m)+1; int xnum=max(0,xnumr-xnuml+1); ans+=xl[n-1][c-'a']*xnum; if(l%(n+m)>0&&l%(n+m)<=n-1){ xnuml--; if(xnuml%2==1) ans+=xr[l%(n+m)][c-'a']; else ans+=xl[n-1-l%(n+m)][c-'a']; } if(r%(n+m)>=0&&r%(n+m)n&&l%(n+m)<=n+m-1){ ynuml--; for(int i=(1<<29);i>=1;i/=2){ if(ynuml%i==0){ if(ynuml/i%2==0) ans+=yr[(l%(n+m)-n)][c-'a']; else ans+=yl[m-1-(l%(n+m)-n)][c-'a']; break; } } } if(r%(n+m)>=n&&r%(n+m)=1;i/=2){ if(ynumr%i==0){ if(ynumr/i%2==0) ans+=yl[r%(n+m)-n][c-'a']; else ans+=yr[m-1-(r%(n+m)-n)][c-'a']; break; } } } cout << ans << endl; } return 0; }