#include <bits/stdc++.h>
using namespace std;
int dp[1502][26], DP[1502][26], tmp[1502][26], a[1502];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
  int n, i, j, k;
  cin >> n;
  string s;
  cin >> s;
  for (i = 1; i <= n; i++) {
    a[i] = s[i - 1] - 'a';
  }
  for (i = 1; i <= n; i++) {
    tmp[0][a[i]] = 1 + dp[0][a[i]];
    for (j = 1; j <= i; j++) {
      for (k = 0; k < 26; k++) {
        if (a[i] == k) {
          tmp[j][k] = dp[j][k] + 1;
        } else {
          tmp[j][k] = dp[j - 1][k] + 1;
        }
        DP[j][k] = max(DP[j][k], tmp[j][k]);
      }
    }
    DP[0][a[i]] = max(DP[0][a[i]], tmp[0][a[i]]);
    dp[0][a[i]] = tmp[0][a[i]];
    tmp[0][a[i]] = 0;
    for (k = 0; k < 26; k++) {
      if (k != a[i]) dp[0][k] = 0;
    }
    for (j = 1; j <= i; j++) {
      for (k = 0; k < 26; k++) {
        dp[j][k] = tmp[j][k];
        tmp[j][k] = 0;
      }
    }
  }
  for (j = 1; j <= n; j++) {
    for (k = 0; k < 26; k++) {
      DP[j][k] = max(DP[j - 1][k], DP[j][k]);
    }
  }
  int q, m;
  char ch;
  cin >> q;
  while (q--) {
    cin >> m >> ch;
    cout << DP[m][ch - 'a'] << endl;
  }
  return 0;
}
