#include <bits/stdc++.h>
using namespace std;
int arr[26];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  string s;
  cin >> s;
  map<pair<int, int>, int> m_1;
  for (int i = 0; i < n; i++) {
    int x = s[i] - 'a';
    arr[x]++;
    m_1[{x, arr[x]}] = i;
  }
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    string fd;
    cin >> fd;
    int arr_2[26];
    for (int j = 0; j < 26; j++) arr_2[j] = 0;
    int res = INT_MIN;
    for (long long j = 0; j < fd.size(); j++) {
      int x = fd[j] - 'a';
      arr_2[x]++;
      res = max(res, m_1[{x, arr_2[x]}]);
    }
    cout << res + 1 << "\n";
  }
  return 0;
}
