#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<int> > v(26);
  for (int i = 0; i < n; i++) {
    v[s[i] - 'a'].push_back(i + 1);
  }
  int m;
  cin >> m;
  while (m--) {
    string s1;
    cin >> s1;
    int max = 0;
    int arr[26] = {0};
    for (int i = 0; i < s1.size(); i++) {
      int t = s1[i] - 'a';
      int l = v[t][arr[t]];
      if (l > max) max = l;
      arr[t]++;
    }
    cout << max << endl;
  }
}
