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