#include <bits/stdc++.h>
using namespace std;
int main() {
  unordered_map<char, unordered_map<int, int>> m;
  int n;
  cin >> n;
  unordered_map<char, int> u;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (u.count(s[i])) {
      u[s[i]]++;
      m[s[i]][u[s[i]]] = i;
    } else {
      u[s[i]] = 1;
      m[s[i]][u[s[i]]] = i;
    }
  }
  int q;
  cin >> q;
  while (q--) {
    string s1;
    cin >> s1;
    int ans = 0;
    unordered_map<char, int> z;
    for (int i = 0; i < s1.length(); i++) {
      z[s1[i]]++;
      ans = max(ans, m[s1[i]][z[s1[i]]]);
    }
    cout << ans + 1 << endl;
  }
}
