#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int n, s[N][26], p[N][26], sum[26];
char str[N];
int main() {
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 26; j++)
      if (j == str[i] - 'a')
        s[i][j] = s[i - 1][j] + 1;
      else
        s[i][j] = s[i - 1][j];
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 26; j++) p[i][j] = n + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 26; j++) p[s[i][j]][j] = min(p[s[i][j]][j], i);
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", str + 1);
    for (int i = 0; i < 26; i++) sum[i] = 0;
    int len = strlen(str + 1), ans = 0;
    for (int i = 1; i <= len; i++) sum[str[i] - 'a']++;
    for (int i = 0; i < 26; i++) ans = max(ans, p[sum[i]][i]);
    printf("%d\n", ans);
  }
}
