#include <bits/stdc++.h>
using namespace std;
const int xx = 1e5 + 10;
long long n;
string s;
vector<long long> last;
long long ans = 0;
map<pair<int, int>, int> mp;
int a[26];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) {
    a[s[i] - 'a']++;
    mp[pair<int, int>(s[i] - 'a', a[s[i] - 'a'])] = i + 1;
  }
  int t;
  cin >> t;
  string temp;
  while (t--) {
    for (int i = 0; i < 26; i++) a[i] = 0;
    int ans = -1;
    cin >> temp;
    for (auto x : temp) {
      int xx = x - 'a';
      a[xx]++;
    }
    for (int i = 0; i < 26; i++) {
      ans = max(ans, mp[pair<int, int>(i, a[i])]);
    }
    cout << ans << endl;
  }
}
