import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;

int cvt (dchar c) {
  return (c.to!int - 97);
}

void main() {
  immutable n = readln.strip.to!int;
  string s = readln.strip;
  auto idx = new int[][26];
  foreach (i; 0 .. n) {
    int o = cvt (s[i]);
    idx[o] ~= i + 1;
  }
  immutable m = readln.strip.to!int;
  debug stderr.writeln (m);
  foreach (qid; 0 .. m) {
    string t = readln.strip;
    int[26] c;
    foreach (ch; t) {
      ++c[cvt (ch)];
    }
    int res;
    foreach (i; 0 .. 26) {
      if (c[i] > 0) {
        res = max (res, idx[i][c[i]-1]);
      }
    }
    writeln (res);
  }
}

