#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> v(26);
int bun(string s, char c, int m) {
  int i, n = s.size(), j, k = 0, t = 0;
  i = 0;
  j = 0;
  for (i = 0; i < n; i++) {
    if (s[i] != c) k++;
    if (k > m) {
      while (j < i && k > m) {
        if (s[j] != c) k--;
        j++;
      }
    }
    t = max(t, i - j + 1);
  }
  return t;
}
void fun(string s) {
  int i, n = s.size(), j, k;
  int b[26];
  for (i = 0; i < 26; i++) b[i] = 0;
  for (i = 0; i < n; i++) b[s[i] - 'a'] = 1;
  for (i = 0; i < 26; i++) {
    for (j = 1; j <= n; j++) {
      if (b[i] == 0) {
        v[i].push_back(j);
        continue;
        ;
      }
      if (j == n) {
        v[i].push_back(n);
        break;
      }
      v[i].push_back(bun(s, i + 'a', j));
    }
  }
}
int main() {
  int n, i, q, m;
  cin >> n;
  string s;
  cin >> s;
  fun(s);
  cin >> q;
  char c;
  for (i = 0; i < q; i++) {
    cin >> m >> c;
    cout << v[c - 'a'][m - 1] << endl;
  }
}
