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