#include <bits/stdc++.h>
using namespace std;
vector<long long> v[26];
int main() {
  long long n, m, c[26], b, an = 0;
  cin >> n;
  string s;
  char ch;
  for (long long i = 0; i < n; i++) {
    cin >> ch;
    v[ch - 97].push_back(i);
  }
  cin >> m;
  for (long long i = 0; i < m; i++) {
    cin >> s;
    an = 0;
    memset(c, 0, sizeof(c));
    for (long long j = 0; j < s.length(); j++) {
      b = s[j] - 97;
      an = max(an, v[b][c[b]]);
      c[b]++;
    }
    cout << an + 1 << endl;
  }
}
