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