#include <bits/stdc++.h>
using namespace std;
int n;
int cnt[26];
int num[26][200003];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char c;
    cin >> c;
    num[c - 'a'][++cnt[c - 'a']] = i;
  }
  int q;
  cin >> q;
  while (q--) {
    memset(cnt, 0, sizeof cnt);
    string s;
    cin >> s;
    int ans = 0;
    for (char c : s) ans = max(ans, num[c - 'a'][++cnt[c - 'a']]);
    cout << ans << "\n";
  }
}
