#include <bits/stdc++.h>
using namespace std;
int get_min_prefix_size(const vector<vector<int> >& v, const string& name) {
  vector<int> letters_count(26, 0);
  for (char c : name) {
    letters_count[c - 'a']++;
  }
  int ans = 0;
  for (int i = 0; i < 26; ++i) {
    int count_ = letters_count[i];
    if (count_ == 0) continue;
    ans = max(ans, v[i][count_ - 1]);
  }
  return ans + 1;
}
int main() {
  int n;
  cin >> n;
  vector<vector<int> > letters_to_positions(26);
  for (int i = 0; i < n; ++i) {
    char c;
    cin >> c;
    letters_to_positions[c - 'a'].push_back(i);
  }
  int m;
  cin >> m;
  vector<int> ans;
  for (int i = 0; i < m; ++i) {
    string s;
    cin >> s;
    int a = get_min_prefix_size(letters_to_positions, s);
    ans.push_back(a);
  }
  for (int a : ans) {
    cout << a << endl;
  }
  return 0;
}
