#include #include using namespace std; int n, m, sx[100009][26], sy[100009][26]; int ps(int xc, int yc, int r, int p) { if (r < 1) return 0; if (!yc) return sx[r][p]; int txc = xc / 2, tyc = (yc - 1) / 2; if (txc * n + tyc * m >= r) return ps(txc, tyc, r, p); else if (txc * n + (tyc + 1) * m >= r) return sx[n][p] * txc + sy[m][p] * tyc + sy[r - (txc * n + tyc * m)][p]; else return sx[n][p] * xc + sy[m][p] * yc - ps(txc, tyc, xc * n + yc * m - r, p); } int main() { ios::sync_with_stdio(false); cin.tie(0); string x, y; cin >> x >> y; n = x.length(); m = y.length(); x = "." + x; y = "." + y; for (int i = 1; i <= n; i++) { for (int j = 0; j < 26; j++) sx[i][j] = sx[i - 1][j]; sx[i][(int)(x[i] - 'a')]++; } for (int i = 1; i <= m; i++) { for (int j = 0; j < 26; j++) sy[i][j] = sy[i - 1][j]; sy[i][(int)(y[i] - 'a')]++; } int q; cin >> q; for (int i = 0; i < q; i++) { int l, r; char p; cin >> l >> r >> p; int xc = 1, yc = 0; while (xc * n + yc * m < r) { xc *= 2; yc = yc * 2 + 1; } cout << ps(xc, yc, r, (int)(p - 'a')) - ps(xc, yc, l - 1, (int)(p - 'a')) << '\n'; } return 0; }