#include <bits/stdc++.h>
using namespace std;
int n, m;
string s, s2;
vector<int> h[26];
int a[26];
int main() {
  int i, j;
  cin >> n >> s;
  for (i = 0; i < n; i++) h[s[i] - 'a'].push_back(i);
  cin >> m;
  while (m--) {
    for (i = 0; i < 26; i++) a[i] = 0;
    int ans = 0, tmp;
    cin >> s2;
    for (i = 0; i < s2.length(); i++) {
      tmp = s2[i] - 'a';
      ans = max(ans, h[tmp][a[tmp]] + 1);
      a[tmp]++;
    }
    cout << ans << endl;
  }
  return 0;
}
