#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  char ch = getchar();
  int pd = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') pd = -pd;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= pd;
}
inline void write(const int &x) {
  char ggg[10001];
  int s = 0;
  int tmp = x;
  if (tmp == 0) {
    putchar('0');
    return;
  }
  if (tmp < 0) {
    tmp = -tmp;
    putchar('-');
  }
  while (tmp > 0) {
    ggg[s++] = tmp % 10 + '0';
    tmp /= 10;
  }
  while (s > 0) {
    putchar(ggg[--s]);
  }
}
int n, m, mp[30][200010];
char s[200010];
int cnt[30];
int main() {
  read(n);
  scanf("%s", s + 1);
  for (register int i = 1; i <= n; ++i) {
    for (register int j = 0; j < 26; ++j) {
      mp[j][i] = mp[j][i - 1];
    }
    ++mp[s[i] - 'a'][i];
  }
  read(m);
  while (m--) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    for (register int i = 1; i <= len; ++i) {
      ++cnt[s[i] - 'a'];
    }
    int ans = 0;
    for (register int i = 0; i < 26; ++i) {
      int now = lower_bound(mp[i], mp[i] + n + 1, cnt[i]) - mp[i];
      ans = (ans > now ? ans : now);
      cnt[i] = 0;
    }
    write(ans);
    puts("");
  }
}
