#include <bits/stdc++.h>
using namespace std;
int ans[1505][27];
int co[1505][27];
char s[1505], ss[2];
inline void scan_d(int &ret) {
  char c;
  ret = 0;
  while ((c = getchar()) < '0' || c > '9')
    ;
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + (c - '0'), c = getchar();
  }
}
void Out(int a) {
  if (a > 9) Out(a / 10);
  putchar(a % 10 + '0');
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i) {
    int k = s[i] - 'a';
    for (int j = 0; j < 26; ++j) co[i][j] = co[i - 1][j];
    ++co[i][k];
    for (int j = 0; j < i; ++j) {
      int len = i - j;
      for (int f = 0; f < 26; ++f) {
        int z = len - co[i][f] + co[j][f];
        if (len > ans[z][f]) ans[z][f] = len;
      }
    }
  }
  int m;
  scanf("%d", &m);
  int z;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 26; ++j)
      if (ans[i][j] < ans[i - 1][j]) ans[i][j] = ans[i - 1][j];
  }
  while (m--) {
    scan_d(z);
    scanf("%s", ss);
    Out(ans[z][ss[0] - 'a']);
    putchar('\n');
  }
  return 0;
}
