#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int N = 1510;
int DP[27][N][N];
int ans[27][N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  memset(DP, 0, sizeof(DP));
  memset(ans, 0, sizeof(ans));
  DP[s[0] - 'a'][0][0] = 1;
  for (int i = (int)1; i < (int)N; i++) DP[s[0] - 'a'][0][i] = 1;
  for (char ch = 'a'; ch <= 'z'; ch++) {
    if (s[0] == ch) continue;
    for (int i = (int)1; i < (int)N; i++) DP[ch - 'a'][0][i] = 1;
  }
  for (int i = (int)1; i < (int)(int)(s.size()); i++) {
    for (char ch = 'a'; ch <= 'z'; ch++) {
      if (s[i] == ch) {
        int len = 0;
        for (int k = i; k >= 0; k--) {
          if (s[k] != ch) break;
          len++;
        }
        DP[ch - 'a'][i][0] = len;
      }
      for (int j = (int)1; j < (int)N; j++) {
        if (ch == s[i])
          DP[ch - 'a'][i][j] = 1 + DP[ch - 'a'][i - 1][j];
        else
          DP[ch - 'a'][i][j] = 1 + DP[ch - 'a'][i - 1][j - 1];
      }
    }
  }
  for (char ch = 'a'; ch <= 'z'; ch++) {
    for (int j = 0; j < N; j++) {
      for (int i = (int)0; i < (int)(int)(s.size()); i++)
        ans[ch - 'a'][j] = max(ans[ch - 'a'][j], DP[ch - 'a'][i][j]);
    }
  }
  int q;
  cin >> q;
  for (int i = (int)0; i < (int)q; i++) {
    int m;
    char ch;
    cin >> m >> ch;
    cout << ans[ch - 'a'][m] << endl;
  }
  return 0;
}
