#include <bits/stdc++.h>
using namespace std;
int n, q, num, dp[30][1505];
vector<int> pl[30];
char ch;
string nm;
int main(void) {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> nm >> q;
  for (int i = 0; i < int((int)(nm).size()); i++) {
    for (int j = 0; j < int(26); j++) {
      if (nm[i] == j + 'a')
        continue;
      else
        pl[j].push_back(i);
    }
  }
  for (int i = 0; i < int(26); i++) {
    dp[i][0] = 0;
    int tmp = 1;
    for (int j = 1; j < (int)(pl[i]).size(); ++j) {
      if (nm[j] != nm[j - 1]) {
        dp[i][0] = max(dp[i][0], tmp);
        tmp = 1;
      } else {
        ++tmp;
      }
    }
    dp[i][0] = max(dp[i][0], tmp);
    for (int j = 1; j <= n; ++j) {
      if (j >= (int)(pl[i]).size())
        dp[i][j] = n;
      else {
        int L = 0, R = j - 1;
        while (R <= (int)(pl[i]).size() - 1) {
          int goL = pl[i][L], goR = pl[i][R];
          while (goL >= 1 && nm[goL - 1] == i + 'a') --goL;
          while (goR < n - 1 && nm[goR + 1] == i + 'a') ++goR;
          dp[i][j] = max(dp[i][j], goR - goL + 1);
          ++L, ++R;
        }
      }
    }
  }
  while (q--) {
    cin >> num >> ch;
    cout << dp[ch - 'a'][num] << "\n";
  }
}
