#include #include #include #include #include using namespace std; int main() { string X, Y; int Q; cin >> X >> Y >> Q; int LX = X.size(), LY = Y.size(); vector LF = { LX }; while (LF.back() <= 1000000000) { LF.push_back(LF.back() * 2 + LY); } vector > XS(26, vector(LX + 1)); vector > YS(26, vector(LY + 1)); for (int i = 0; i < LX; ++i) { XS[X[i] - 'a'][i + 1]++; } for (int i = 0; i < LY; ++i) { YS[Y[i] - 'a'][i + 1]++; } for (int i = 0; i < 26; ++i) { for (int j = 0; j < LX; ++j) { XS[i][j + 1] += XS[i][j]; } for (int j = 0; j < LY; ++j) { YS[i][j + 1] += YS[i][j]; } } vector > CF(LF.size(), vector(26)); for (int i = 0; i < 26; ++i) { CF[0][i] = XS[i][LX]; } for (int i = 1; i < int(LF.size()); ++i) { for (int j = 0; j < 26; ++j) { CF[i][j] = CF[i - 1][j] * 2 + YS[j][LY]; } } for (int i = 0; i < Q; ++i) { int L, R; string C; cin >> L >> R >> C; --L; int id = C[0] - 'a'; function calc = [&](int layer, long long l, long long r) { if (L <= l && r <= R) { return CF[layer][id]; } if (R <= l || r <= L) { return 0LL; } if (layer == 0) { long long pl = max(l, (long long)(L)), pr = min(r, (long long)(R)); return (l < r ? XS[id][pr - l] - XS[id][pl - l] : 0LL); } long long mp1 = l + LF[layer - 1]; long long mp2 = r - LF[layer - 1]; long long res1 = calc(layer - 1, l, mp1); long long res2 = calc(layer - 1, mp2, r); long long res3 = 0; long long cl = max(mp1, (long long)(L)), cr = min(mp2, (long long)(R)); if (cl < cr) { res3 = YS[id][cr - mp1] - YS[id][cl - mp1]; } return res1 + res2 + res3; }; long long ans = calc(int(LF.size()) - 1, 0, LF.back()); cout << ans << endl; } return 0; }