using System; using System.Collections.Generic; using static System.Console; using System.Linq; class yuki320 { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static void Main() { var x = ReadLine(); var y = ReadLine(); var q = NN; var l = new int[q]; var r = new int[q]; var c = new char[q]; for (var i = 0; i < q; ++i) { var cin = ReadLine().Split(); l[i] = int.Parse(cin[0]) - 1; r[i] = int.Parse(cin[1]); c[i] = cin[2][0]; } var rmax = r.Max(); xcum = new int[26][]; ycum = new int[26][]; xrcum = new int[26][]; yrcum = new int[26][]; xl = x.Length; yl = y.Length; for (var i = 0; i < xcum.Length; ++i) { xcum[i] = new int[x.Length + 1]; ycum[i] = new int[y.Length + 1]; xrcum[i] = new int[x.Length + 1]; yrcum[i] = new int[y.Length + 1]; } for (var j = 0; j < x.Length; ++j) { for (var i = 0; i < 26; ++i) { xcum[i][j + 1] = xcum[i][j]; xrcum[i][j + 1] = xrcum[i][j]; } ++xcum[x[j] - 'a'][j + 1]; ++xrcum[x[x.Length - j - 1] - 'a'][j + 1]; } for (var j = 0; j < y.Length; ++j) { for (var i = 0; i < 26; ++i) { ycum[i][j + 1] = ycum[i][j]; yrcum[i][j + 1] = yrcum[i][j]; } ++ycum[y[j] - 'a'][j + 1]; ++yrcum[y[y.Length - j - 1] - 'a'][j + 1]; } lenlist = new List(); lenlist.Add(x.Length); while (lenlist.Last() < rmax) lenlist.Add(lenlist.Last() * 2 + y.Length); var res = new int[q]; for (var i = 0; i < q; ++i) res[i] = DFS(r[i], lenlist.Count, c[i] - 'a', false) - DFS(l[i], lenlist.Count, c[i] - 'a', false); WriteLine(string.Join("\n", res)); } static List lenlist; static int[][] xcum; static int[][] xrcum; static int[][] ycum; static int[][] yrcum; static int xl; static int yl; static int DFS(int cur, int depth, int ch, bool rev) { if (depth == 0) { if (rev) return xrcum[ch][cur]; else return xcum[ch][cur]; } else if (cur < lenlist[depth - 1]) return DFS(cur, depth - 1, ch, false); else if (cur < lenlist[depth - 1] + yl) { var res = xcum[ch].Last() * (int)Math.Pow(2, depth - 1); res += ycum[ch].Last() * ((int)Math.Pow(2, depth - 1) - 1); if (rev) res += yrcum[ch][cur - lenlist[depth - 1]]; else res += ycum[ch][cur - lenlist[depth - 1]]; return res; } else { var res = xcum[ch].Last() * (int)Math.Pow(2, depth - 1); res += ycum[ch].Last() * ((int)Math.Pow(2, depth - 1) - 1); res += yrcum[ch].Last(); res += DFS(cur - lenlist[depth - 1] - yl, depth - 1, ch, true); return res; } } }