#include #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; const int INF = 1001001001; string x, y; int q; ll rc(vector &sz, vector &alpsum, vector &alp, vector &rvalp, vector &syt, vector &syf, int lv, int r, bool mode, bool lst = false) { // if(q == 0) cerr << lv << " " << r << endl; // rep(i, alp.size()) cerr << alp.at(i) << " "; // cerr << endl; if(sz.at(lv) <= r) return alpsum.at(lv); if(lv == 0) { // cerr << lst << endl; if(r >= x.size()) r = x.size() - 1; if(!lst) return alp.at(r); else return rvalp.at(r); } ll bsz = sz.at(lv-1); if(r <= bsz) { return rc(sz, alpsum, alp, rvalp, syt, syf, lv-1, r, false); } bsz += y.size(); if(r <= bsz) { int d = r - sz.at(lv-1); ll res = 0; if(!mode) res = syt.at(d); else res = syf.at(d); return rc(sz, alpsum, alp, rvalp, syt, syf, lv-1, r, false) + res; } ll res = rc(sz, alpsum, alp, rvalp, syt, syf, lv-1, r, false) + syt.back(); return rc(sz, alpsum, alp, rvalp, syt, syf, lv-1, r - bsz, true, true) + res; } ll fc(vector &sz, vector &alpsum, vector &alp, vector &rvalp, vector &syt, vector &syf, int r) { return rc(sz, alpsum, alp, rvalp, syt, syf, sz.size()-1, r, false); } void init(vector> &syt, vector> &syf, string y) { auto ry = y; reverse(ALL(ry)); rep(i, y.size()) { int now = y.at(i) - 'a'; rep(j, 26) { syt.at(j).at(i+1) += syt.at(j).at(i); if(j == now) syt.at(j).at(i+1)++; } now = ry.at(i) - 'a'; rep(j, 26) { syf.at(j).at(i+1) += syf.at(j).at(i); if(j == now) syf.at(j).at(i+1)++; } } } int main() { cin >> x >> y; vector sz(1, x.size()); while(1) { ll p = sz.back(); sz.push_back(p+p+y.size()); if(sz.back() > INF) break; } vector syt(26, vector(y.size()+1, 0)); vector syf(26, vector(y.size()+1, 0)); init(syt, syf, y); vector sxt(26, vector(x.size()+1, 0)); vector sxf(26, vector(x.size()+1, 0)); init(sxt, sxf, x); vector alpsum(26, vector(sz.size(), 0)); rep(i, 26) alpsum.at(i).at(0) = sxt.at(i).back(); rep2(i, 1, alpsum.size()) { rep(j, 26) { alpsum.at(j).at(i) = alpsum.at(j).at(i-1) * 2 + syt.at(j).back(); } } cin >> q; while(q--) { int l, r; char c; cin >> l >> r >> c; int idx = c - 'a'; ll ans = fc(sz, alpsum.at(idx), sxt.at(idx), sxf.at(idx), syt.at(idx), syf.at(idx), r); if(l != 0) ans -= fc(sz, alpsum.at(idx), sxt.at(idx), sxf.at(idx), syt.at(idx), syf.at(idx), l-1); cout << ans << endl; } return 0; }