#include <bits/stdc++.h>
using namespace std;
map<pair<char, long long>, long long> mp;
long long freq[1000];
int main() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    freq[s[i] - 0]++;
    mp[make_pair(s[i], freq[s[i] - 0])] = i + 1;
  }
  long long ans;
  long long m;
  cin >> m;
  while (m--) {
    ans = 0;
    memset(freq, 0, sizeof freq);
    string z;
    cin >> z;
    for (int i = 0; i < z.size(); i++) {
      freq[z[i] - 0]++;
      ans = max(ans, mp[make_pair(z[i], freq[z[i] - 0])]);
    }
    cout << ans << endl;
  }
  return 0;
}
