#include <bits/stdc++.h>
using namespace std;
char T[1510];
vector<int> Q[26];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q, m;
  char c;
  cin >> n;
  cin.get();
  cin.getline(T, 1510);
  for (int i = 0; i < n; i++) {
    Q[T[i] - 'a'].push_back(i);
  }
  cin >> q;
  for (int t = 0; t < q; t++) {
    cin >> m >> c;
    int No = c - 'a';
    int len = Q[No].size(), cnt = m, L = 0, R = 1, Out = 0;
    for (int i = 0; i < len; i++) {
      while (i > L) L = i, cnt += Q[No][L] - Q[No][L - 1] - 1;
      while (R < len && cnt >= Q[No][R] - Q[No][R - 1] - 1)
        cnt -= Q[No][R] - Q[No][R - 1] - 1, R++;
      int H = cnt;
      if (H > n - (Q[No][R - 1] - Q[No][L] + 1))
        H = n - (Q[No][R - 1] - Q[No][L] + 1);
      if (Q[No][R - 1] - Q[No][L] + 1 + H > Out)
        Out = Q[No][R - 1] - Q[No][L] + 1 + H;
    }
    if (m > Out) Out = m;
    cout << Out << '\n';
  }
}
