#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897932384626;
long long MOD = 1000000007;
const long double EPS = 1e-9;
const int N = 3e5 + 7;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline unsigned int setbits(unsigned int v) {
  unsigned int c;
  for (c = 0; v; ++c) v &= (v - 1);
  return c;
}
inline bool is_power_of_two(unsigned int v) { return (v && !(v & (v - 1))); }
inline void scan(int& a) { scanf("%d", &a); }
inline void scan(long long& a) { scanf("%lld", &a); }
inline void scan(long double& a) { scanf("%Lf", &a); }
inline void scan(string& a) { cin >> a; }
inline void scan(pair<int, int>& a) { scanf("%d %d", &a.first, &a.second); }
template <typename T>
inline void scanarr(const vector<T>& arr) {
  for (int i = 0; i < arr.size(); ++i) scan(arr[i]);
}
template <typename T>
inline void scanarr2d(const vector<vector<T>>& arr) {
  for (int i = 0; i < arr.size(); ++i)
    for (int j = 0; j < arr[i].size(); ++j) cin >> arr[i][j];
}
inline void print(const int& v) { printf("%d\n", v); }
inline void print(const long long& v) { printf("%lld\n", v); }
inline void print(const long double& v) { printf("%Lf\n", v); }
inline void print(const string& v) { printf("%s\n", v.c_str()); }
inline void print(const pair<int, int>& v) {
  printf("%d %d\n", v.first, v.second);
}
template <typename T>
inline void printarr(const vector<T>& arr) {
  for (int i = 0; i < arr.size(); ++i) print(arr[i]);
}
template <typename T>
inline void printarr2d(const vector<vector<T>>& arr) {
  for (int i = 0; i < arr.size(); ++i) {
    for (int j = 0; j < arr[i].size(); ++j) cout << arr[i] << " ";
    cout << '\n';
  }
}
bool is_palin(const string& s) {
  int i, j;
  for (i = 0, j = s.length() - 1; i <= j; ++i, --j)
    if (s[i] != s[j]) return false;
  return true;
}
long long power(long long base, unsigned long long exp, long long mod = MOD) {
  long long res = 1;
  base %= mod;
  while (exp) {
    if (exp & 1) res = (res * base) % mod;
    exp >>= 1;
    base = (base * base) % mod;
  }
  return res;
}
long long modinv(long long a, long long mod = MOD) {
  if (mod == 1) return 0;
  long long x = 1, y = 0, m = mod;
  while (a > 1) {
    long long q = a / m, t = m;
    m = a % m;
    a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += mod;
  return x;
}
inline unsigned int iabs(int x) {
  unsigned int y = x;
  return (y > 0) ? y : -y;
}
inline long long add(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod + b % mod) % mod;
  return res;
}
inline long long sub(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod - b % mod + mod) % mod;
  return res;
}
inline long long mult(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod * b % mod) % mod;
  return res;
}
inline long long divi(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod * modinv(b, mod)) % mod;
  return res;
}
void openf() {}
vector<vector<vector<int>>> dp(1500,
                               vector<vector<int>>(1501, vector<int>(2, 0)));
vector<vector<int>> ans(1501, vector<int>(26, 0));
int main() {
  openf();
  int m, n, q, msf = 1;
  char c;
  string s;
  cin >> n >> s >> q;
  for (int k = 0; k < 26; ++k) {
    for (int i = 0; i < 1500; ++i) {
      for (int j = 0; j < 1501; ++j) {
        for (int w = 0; w < 2; ++w) {
          dp[i][j][w] = 0;
        }
      }
    }
    if ((s[0] - 'a') == k) {
      dp[0][0][1] = 1;
    } else {
      for (int j = 1; j < 1501; ++j) {
        dp[0][j][1] = 1;
      }
    }
    int mcl = 0;
    if (s[0] - 'a' == k) {
      ++mcl;
    }
    for (int i = 1; i < n; ++i) {
      for (int j = 1; j < 1501; ++j) {
        if ((s[i] - 'a') == k) {
          int ti = i - mcl - 1;
          if (ti >= 0) {
            dp[i][j][1] = 1 + mcl + dp[ti][j][1];
          } else {
            dp[i][j][1] = 1 + mcl;
          }
          dp[i][j][0] = dp[i - 1][j][0];
        } else {
          dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
          int ti = i - mcl - 1;
          if (ti >= 0) {
            dp[i][j][1] = 1 + mcl + dp[ti][j - 1][1];
          } else {
            dp[i][j][1] = 1 + mcl;
          }
        }
      }
      if (s[i] - 'a' == k) {
        ++mcl;
      } else {
        mcl = 0;
      }
    }
    for (int i = 0; i < 1501; ++i) {
      ans[i][k] = max(dp[n - 1][i][0], dp[n - 1][i][1]);
    }
  }
  while (q--) {
    cin >> m >> c;
    cout << ans[m][c - 'a'] << endl;
  }
  return 0;
}
