import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

import static java.lang.Math.*;

public class ProblemD_91 implements Runnable{

	final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null;
	BufferedReader in;
	PrintWriter out;
	StringTokenizer tok = new StringTokenizer("");

	void init() throws FileNotFoundException{
		if (ONLINE_JUDGE){
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
		}else{
			in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter("output.txt");
		}
	}

	String readString() throws IOException{
		while(!tok.hasMoreTokens()){
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	int readInt() throws IOException{
		return Integer.parseInt(readString());
	}

	long readLong() throws IOException{
		return Long.parseLong(readString());
	}

	double readDouble() throws IOException{
		return Double.parseDouble(readString());
	}

	public static void main(String[] args){
		new Thread(null, new ProblemD_91(), "", 256 * (1L << 20)).start();
	}

	public void run(){
		try{
			long t1 = System.currentTimeMillis();
			init();
			solve();
			out.close();
			long t2 = System.currentTimeMillis();
			System.err.println("Time = "+(t2-t1));
		}catch (Exception e){
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}

	void solve() throws IOException{
		int n = readInt();
		int k = readInt();
		char[] c = readString().toCharArray();
		for (int i = 0; i < n - 1; i++){
			if (k == 0) break;
			if (c[i] == '4' && c[i+1] == '7'){
				if (i % 2 == 0){
					c[i+1] = '4';
					k--;
					if (i < n - 2 && c[i+2] == '7'){
						if (k % 2 == 1){
							c[i+1] = '7';
						}
						for (int j = 0; j < n; j++){
							out.print(c[j]);
						}
						return;
					}
				}
				else{
					c[i] = '7';
					k--;
					if (i > 0 && c[i-1] == '4'){
						if (k % 2 == 1){
							c[i] = '4';
						}
						for (int j = 0; j < n; j++){
							out.print(c[j]);
						}
						return;
					}
				}
			}
		}
		for (int i = 0; i < n; i++){
			out.print(c[i]);
		}
	}

	static public class Utils {

		private Utils() {}

		public static void swap(int x, int y){
			x ^= y;
			y ^= x;
			x ^= y;
		}

		public static long mergeSort(int[] a) { // ���������� ���-�� �������� � �������� �������
			return mergeSort(a, 0, a.length - 1);
		}

		private static long mergeSort(int[] a, int leftIndex, int rightIndex) {
			final int MAGIC_VALUE = 50;
			if (leftIndex < rightIndex) {
				if (rightIndex - leftIndex <= MAGIC_VALUE) {
					return insertionSort(a, leftIndex, rightIndex);
				} else {
					int middleIndex = (leftIndex + rightIndex) / 2;
					return mergeSort(a, leftIndex, middleIndex) +
					mergeSort(a, middleIndex + 1, rightIndex) +
					merge(a, leftIndex, middleIndex, rightIndex);
				}
			}
			return 0;
		}

		private static long merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
			int length1 = middleIndex - leftIndex + 1;
			int length2 = rightIndex - middleIndex;
			int[] leftArray = new int[length1+1];
			int[] rightArray = new int[length2+1];
			System.arraycopy(a, leftIndex, leftArray, 0, length1);
			System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
			leftArray[length1] = rightArray[length2] = Integer.MAX_VALUE;
			long res = 0;
			for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
				if (leftArray[i] > rightArray[j]){
					a[k] = rightArray[j++];
					res += length1 - i;
				}else a[k] = leftArray[i++];
			}
			return res;
		}

		private static long insertionSort(int[] a, int leftIndex, int rightIndex) {
			long res = 0;
			for (int i = leftIndex + 1; i <= rightIndex; i++) {
				int current = a[i];
				int j = i - 1;
				while (j >= leftIndex && a[j] > current) {
					a[j + 1] = a[j];
					j--;
					res++;
				}
				a[j + 1] = current;
			}
			return res;
		}

		public static int median(int[] a){
			return select(a, 0, a.length - 1, a.length / 2 - 1);
		}

		public static int select(int[] a, int i){
			return select(a, 0, a.length - 1, i);
		}

		private static int select(int[] a, int l, int r, int i){
			if (l == r) return a[l];
			int len = (r - l) / 5 + 1;
			int[] x = new int[len];
			for (int j = 0; j < len; j++){
				int leftIndex = l + j * 5;
				int rightIndex = min(leftIndex + 4, r);
				insertionSort(a, leftIndex, rightIndex);
				x[j] = a[(rightIndex+leftIndex)/2];
			}
			int med = median(x);
			int m = partition(a, l, r, med);
			int k = m - l;
			if (i == k) return a[k+l];
			else return i < k? select(a, l, m - 1, i): select(a, m + 1, r, i - k - 1);
		}

		private static int partition(int[] a, int l, int r, int med){
			int i = l;
			for (int j = l; j <= r; j++){
				if (a[j] < med){
					swap(a[i++], a[j]);
				}
			}
			return i;
		}

		public static int median(int[] x, int[] y){
			return median(x, 0, x.length - 1, y, 0, y.length - 1);
		}

		private static int median(int[] x, int xl, int xr, int[] y, int yl, int yr){ // ����� ������� ���� ��������������� �������� ���������� �����
			if (xl == xr){
				int ind = xl + yl;
				for (; ind < x.length - 1 && yl < yr; yl++, ind++);
				return ind == x.length - 1? min(x[xl], y[yl]): max(x[xl], y[yl]);
			}
			if (yl == yr){
				int ind = xl + yl;
				for (; ind < y.length - 1 && xl < xr; xl++, ind++);
				return ind == y.length - 1? min(x[xl], y[yl]): max(x[xl], y[yl]);
			}
			int mdx = (xl + xr) / 2;
			int mdy = (yl + yr) / 2;
			return x[mdx] < y[mdy]? median(x, mdx, xr, y, yl, mdy): median(x, xl, mdx, y, mdy, yr);
		}

		public static final int[][] step8 = {{-1, 0}, {1, 0}, {0, -1}, {0, 1},
									{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};

		public static final int[][] stepKnight = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
											{1, -2}, {1, 2}, {2, -1}, {2, 1}};

		public static long gcd(long a, long b){
			if (a == 0)	return b;
			return gcd(b % a, a);
		}

		public static long lcm(long a, long b){
			return a / gcd(a, b)*b;
		}

		public static long[] gcdPlus(long a, long b){
			long[] d = new long[3];
			if (a == 0){
				d[0] = b;
				d[1] = 0;
				d[2] = 1;
			}else{
				d = gcdPlus(b % a, a);
				long r = d[1];
				d[1] = d[2] - b/a*d[1];
				d[2] = r;
			}
			return d;
		}

		public static long binpow(long a, int n){
			if (n == 0)	return 1;
			if ((n & 1) == 0){
				long b = binpow(a, n/2);
				return b*b;
			}else return binpow(a, n - 1)*a;
		}

		public static long binpowmod(long a, int n, long m){
			if (m == 1) return 0;
			if (n == 0)	return 1;
			if ((n & 1) == 0){
				long b = binpowmod(a, n/2, m);
				return (b*b) % m;
			}else return binpowmod(a, n - 1, m)*a % m;
		}

		public static long f(long n, int x, int k){ //���-�� ���������� ����� (������� 0), ���������� � ���� ����� �� 0 �� k-1
			if (n == 0)	return 1;
			long b = binpow(10, x - 1);
			long c = n / b;
			return (c < k? c: k)*binpow(k, x - 1) + (c < k? 1: 0)*f(n % b, x - 1, k);
		}

		public static long fib(int n){
			if (n == 0) return 0;
			if ((n & 1) == 0){
				long f1 = fib(n/2 - 1);
				long f2 = fib(n/2 + 1);
				return f2*f2 - f1*f1;
			}else{
				long f1 = fib(n/2);
				long f2 = fib(n/2 + 1);
				return f1*f1 + f2*f2;
			}
		}

		public static BigInteger BigFib(int n){
			if (n == 0) return BigInteger.ZERO;
			if ((n & 1) == 0){
				BigInteger f1 = BigFib(n/2 - 1);
				f1 = f1.multiply(f1);
				BigInteger f2 = BigFib(n/2 + 1);
				f2 = f2.multiply(f2);
				return f2.subtract(f1);
			}else{
				BigInteger f1 = BigFib(n/2);
				f1 = f1.multiply(f1);
				BigInteger f2 = BigFib(n/2 + 1);
				f2 = f2.multiply(f2);
				return f2.add(f1);
			}
		}

		public static long phi(long n){
			int[] p = new int[1];
			Sieve(p, (int)ceil(sqrt(n)) + 2);
			long phi = 1;
			for (int i = 0; i < p.length; i++){
				long x = 1;
				while (n % p[i] == 0){
					n /= p[i];
					x *= p[i];
				}
				phi *= x - x/p[i];
			}
			if (n != 1) phi *= n - 1;
			return phi;
		}

		public static boolean IsProbablyPrimeSqrt(long n){
			if (n == 1)	return false;
			if ((n & 1) == 0) return (n == 2);
			for (int j = 3; j < sqrt(n) + 1; j += 2){
				if (n % j == 0)	return false;
			}
			return true;
		}

		public static void Sieve(int[] p, int n){
			boolean[] b = new boolean[n+1];
			Arrays.fill(b, true);
			b[0] = false;
			b[1] = false;
			long nLong = n;
			int j=0;
			for (int i = 1; i <= n; i++) {
				if (b[i]){
					j++;
					if (((long)i)*i <= nLong) {
						for (int k = i*i; k <= n; k += i) {
							b[k] = false;
						}
					}
				}
			}
			p = new int[j];
			Arrays.fill(p, 0);
			j=0;
			for (int i = 2; i <= n; i++) {
				if (b[i]){
					p[j]=i;
					j++;
				}
			}
		}

		public static class PointD{

			double x, y;

			public PointD(double x, double y){
				this.x = x;
				this.y = y;
			}

			public double d(PointD p){
				return sqrt(d2(p));
			}

			public double d2(PointD p){
				return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);
			}
		}

		public static class Point3D{

			double x, y, z;

			public Point3D(double x, double y, double z){
				this.x = x;
				this.y = y;
				this.z = z;
			}

			public double d(Point3D p){
				return sqrt(d2(p));
			}

			public double d2(Point3D p){
				return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y) + (z - p.z) * (z - p.z);
			}
		}

		public static int[][] Palindromes(String s){ // ���-�� ���� �������� - ����������� � ������
			char[] c = s.toCharArray();
			int n = c.length;
			int[][] d = new int[2][n];
			int l = 0, r = -1;
			for (int i = 0; i < n; i++){
				int j = (i > r? 0: min(d[0][l+r-i+1], r-i+1)) + 1;
				for (; i - j >= 0 && i + j - 1< n && c[i-j] == c[i+j-1]; j++);
				d[0][i] = --j;
				if (i + d[0][i] - 1 > r){
					r = i + d[0][i] - 1;
					l = i - d[0][i];
				}
			}
			l = 0;
			r = -1;
			for (int i = 0; i < n; i++){
				int j = (i > r? 0: min(d[1][l+r-i], r-i)) + 1;
				for (; i - j >= 0 && i + j < n && c[i-j] == c[i+j]; j++);
				d[1][i] = --j;
				if (i + d[1][i] > r){
					r = i + d[1][i];
					l = i - d[1][i];
				}
			}
			return d;
		}

		public static class Permutation {

			private int[] a;
			private int n;

			public Permutation(int n){
				this.n = n;
				a = new int[n];
				for (int i = 0; i < n; i++){
					a[i] = i;
				}
			}

			public boolean nextPermutation(){ //������� � do{}while(nextPermutation(a));
				int i = n - 1;
				for (i = n - 2; i >= 0 && a[i] < a[i+1]; i--);
				if (i == -1) return false;
				int jMin = i + 1;
				for (int j = n - 1; j > i; j--){
					if (a[i] < a[j] && a[j] < a[jMin]){
						jMin = j;
					}
				}
				swap(i, jMin);
				for (int j = 1; j <= (n - i)/2; j++){
					swap(i + j, n - j);
				}
				return true;
			}

			public int get(int i){
				return a[i];
			}

			private void swap(int i, int j){
				a[i] ^= a[j];
				a[j] ^= a[i];
				a[i] ^= a[j];
			}
		}

		public static class Fraction implements Comparable<Fraction>, Cloneable{

			public final Fraction FRACTION_ZERO = new Fraction();
			public final Fraction FRACTION_ONE = new Fraction(1);
			public long numerator = 0;
			public long denominator = 1;

			public Fraction(){
				numerator = 0;
				denominator = 1;
			}

			public Fraction(long numerator){
				this.numerator = numerator;
				denominator = 1;
			}

			public Fraction(long numerator, long denominator){
				this.numerator = numerator;
				this.denominator = denominator;
				Cancellation();
			}

			public Fraction(double numerator, double denominator, int accuracy){
				this.numerator = (long)(numerator*pow(10,accuracy));
				this.denominator = (long)(denominator*pow(10,accuracy));
				Cancellation();
			}

			public Fraction(String s){
				if (s.charAt(0) == '-'){
					denominator = -1;
					s = s.substring(1);
				}
				if (s.indexOf("/") != -1){
					denominator *= Integer.parseInt(s.substring(s.indexOf("/") + 1));
				}
				if (s.indexOf(" ") != -1){
					numerator = Integer.parseInt(s.substring(0, s.indexOf(" "))) * abs(denominator) + Integer.parseInt(s.substring(s.indexOf(" ") + 1, s.indexOf("/")));
				}else{
					if (s.indexOf("/") != -1){
						numerator = Integer.parseInt(s.substring(0, s.indexOf("/")));
					}else{
						numerator = Integer.parseInt(s)*abs(denominator);
					}
				}
				this.Cancellation();
			}

			private void Cancellation(){
				long g = gcd(abs(numerator), abs(denominator));
				numerator /= g;
				denominator /= g;
				if (denominator < 0){
					numerator *= -1;
					denominator *= -1;
				}
			}

			public String toString(){
				String s = "";
				if (numerator == 0){
					return "0";
				}
				if (numerator < 0){
					s += "-";
				}
				if (abs(numerator) >= denominator){
					s += Long.toString(abs(numerator) / denominator) + " ";
				}
				if (abs(numerator) % denominator != 0){
					s += Long.toString(abs(numerator) % denominator);
				}else{
					s = s.substring(0, s.length()-1);
				}
				if (denominator != 1){
					s += "/" + Long.toString(denominator);
				}
				return s;
			}

			public Fraction add(Fraction f){
				Fraction fResult = new Fraction();
				fResult.denominator = lcm(denominator, f.denominator);
				fResult.numerator = numerator * fResult.denominator / denominator + f.numerator * fResult.denominator / f.denominator;
				fResult.Cancellation();
				return fResult;
			}

			public Fraction subtract(Fraction f){
				Fraction fResult = new Fraction();
				fResult.denominator = lcm(denominator, f.denominator);
				fResult.numerator = numerator * fResult.denominator / denominator - f.numerator * fResult.denominator / f.denominator;
				fResult.Cancellation();
				return fResult;
			}

			public Fraction multiply(Fraction f){
				Fraction fResult = new Fraction();
				fResult.numerator = numerator * f.numerator;
				fResult.denominator = denominator * f.denominator;
				fResult.Cancellation();
				return fResult;
			}

			public Fraction divide(Fraction f){
				Fraction fResult = new Fraction();
				fResult.numerator = numerator * f.denominator;
				fResult.denominator = denominator * f.numerator;
				fResult.Cancellation();
				return fResult;
			}

			@Override
			public int compareTo(Fraction f){
				long g = gcd(denominator, f.denominator);
				long res = numerator * (f.denominator / g) - f.numerator * (denominator / g);
				if (res < 0){
					return -1;
				}
				if (res > 0){
					return 1;
				}
				return 0;
			}

			public Fraction clone(){
				Fraction fResult = new Fraction(numerator, denominator);
				return fResult;
			}

			public Fraction floor(){
				Fraction fResult = this.clone();
				fResult.numerator = (fResult.numerator / fResult.denominator) * fResult.denominator;
				return fResult;
			}

			public Fraction ceil(){
				Fraction fResult = this.clone();
				fResult.numerator = (fResult.numerator/fResult.denominator + 1) * fResult.denominator;
				return fResult;
			}

			public Fraction binpow(int n){
				if (n==0) return FRACTION_ONE;
				if ((n&1)==0){
					Fraction f=this.binpow(n/2);
					return f.multiply(f);
				}else return binpow(n-1).multiply(this);
			}
		}

		public static class FenwickTree_1{	//One-dimensional array

			private int n;
			private long[] a;
			private long[] tree;

			public FenwickTree_1(int n){
				this.n = n;
				tree = new long[n];
				a = new long[n];
			}

			public long sum(int xl, int xr){
				return sum(xr) - sum(xl);
			}

			private long sum(int x){
				long result = 0;
				for (int i = x; i >= 0; i = (i & (i + 1)) - 1){
					result += tree[i];
				}
				return result;
			}

			public void update(int x, long new_value){
				long delta = new_value - a[x];
				a[x] = new_value;
				for (int i = x; i < n; i = (i | (i + 1))){
					tree[i] += delta;
				}
			}
		}

		public static class FenwickTree_2{	//Two-dimensional array

			private int n, m;
			private long[][] a;
			private long[][] tree;

			public FenwickTree_2(int n, int m){
				this.n = n;
				this.m = m;
				tree = new long[n][m];
				a = new long[n][m];
			}

			public long sum(int xl, int yl, int xr, int yr){
				return sum(xr, yr) - sum(xl - 1, yr) - sum(xr, yl - 1) + sum(xl - 1, yl - 1);
			}

			private long sum(int x, int y){
				long result = 0;
				for (int i = x; i >= 0; i = (i & (i + 1)) - 1){
					for (int j = y; j >= 0; j = (j & (j + 1)) - 1){
						result+=tree[i][j];
					}
				}
				return result;
			}

			public void update(int x, int y, long new_value){
				long delta = new_value - a[x][y];
				a[x][y] = new_value;
				for (int i = x; i < n; i = (i | (i + 1))){
					for (int j = y; j < m; j = (j | (j + 1))){
						tree[i][j] += delta;
					}
				}
			}
		}

		public static class FenwickTree_3{	//Three-dimensional array

			private int n, m, l;
			private long[][][] a;
			private long[][][] tree;

			public FenwickTree_3(int n, int m, int l){
				this.n = n;
				this.m = m;
				this.l = l;
				tree = new long[n][m][l];
				a = new long[n][m][l];
			}

			public long sum(int xl, int yl, int zl, int xr, int yr, int zr){
				return sum(xr, yr, zr) - sum(xl - 1, yr, zr)
				+ sum(xl - 1, yr, zl - 1) - sum(xr, yr, zl - 1)
				- sum(xr, yl - 1, zr) + sum(xl - 1, yl - 1, zr)
				- sum(xl - 1, yl - 1, zl - 1) + sum(xr, yl - 1, zl - 1);
			}

			private long sum(int x, int y, int z){
				long result = 0;
				for (int i = x; i >= 0; i = (i & (i + 1)) - 1){
					for (int j = y; j >= 0; j = (j & (j + 1)) - 1){
						for (int k = z; k >= 0; k = (k & (k + 1)) - 1){
							result += tree[i][j][k];
						}
					}
				}
				return result;
			}

			public void update(int x, int y, int z, long new_value){
				long delta = new_value - a[x][y][z];
				a[x][y][z] = new_value;
				for (int i = x; i < n; i = (i | (i + 1))){
					for (int j = y; j < m; j = (j | (j + 1))){
						for (int k = z; k < l; k = (k | (k + 1))){
							tree[i][j][k] += delta;
						}
					}
				}
			}
		}

		static public class YoungTable{

			private long[] table;
			private int n;
			private int countOfElements;

			public YoungTable(int nmax) {
				n = (int) floor(sqrt(nmax)) + 1;
				table = new long[n*n];
				Arrays.fill(table, Long.MAX_VALUE);
				countOfElements = 0;
			}

			public YoungTable(long[] a){
				n = (int) floor(sqrt(a.length)) + 1;
				table = new long[n*n];
				Arrays.fill(table, Long.MAX_VALUE);
				countOfElements = 0;
				for (int i = 0; i < a.length; i++){
					put(a[i]);
				}
			}

			int size(){
				return countOfElements;
			}

			void put(long v) throws ArrayIndexOutOfBoundsException{
				if (countOfElements == table.length) throw new ArrayIndexOutOfBoundsException();
				table[countOfElements] = v;
				YoungTablefyUp(countOfElements++);
			}

			private void YoungTablefyUp(int ind){
				int maxInd = ind;
				if (ind >= n && table[maxInd] < table[ind-n]) maxInd = ind - n;
				if (ind % n > 0 && table[maxInd] < table[ind-1]) maxInd = ind - 1;
				if (maxInd != ind){
					swap(maxInd, ind);
					YoungTablefyUp(maxInd);
				}
			}

			private void swap(int i, int j){
				table[i] ^= table[j];
				table[j] ^= table[i];
				table[i] ^= table[j];
			}

			boolean contains(long v){
				int i = n - 1;
				int j = 0;
				int ind = i * n + j;
				boolean exit = true;
				do{
					exit = true;
					while (i > 0 && table[ind] > v){
						i--;
						ind -= n;
						exit = false;
					}
					while (j < n - 1 && table[ind] < v){
						j++;
						ind++;
						exit = false;
					}
				}while (!exit);
				return table[ind] == v;
			}

			long peek() throws ArrayIndexOutOfBoundsException{
				if (countOfElements == 0) throw new ArrayIndexOutOfBoundsException();
				return table[0];
			}

			long poll() throws ArrayIndexOutOfBoundsException{
				if (countOfElements == 0) throw new ArrayIndexOutOfBoundsException();
				long res = table[0];
				table[0] = table[--countOfElements];
				table[countOfElements] = Long.MAX_VALUE;
				YoungTablefyDown(0);
				return res;
			}

			private void YoungTablefyDown(int ind){
				int minInd = ind;
				if (ind % n < n - 1 && table[minInd] > table[ind+1]) minInd = ind + 1;
				if (ind / n < n - 1 && table[minInd] > table[ind+n]) minInd = ind + n;
				if (minInd != ind){
					swap(minInd, ind);
					YoungTablefyDown(minInd);
				}
			}

			void sort(long[] a){
				YoungTable yt = new YoungTable(a);
				for (int i = 0; i < a.length; i++){
					a[i] = yt.poll();
				}
			}
		}
	}
}


import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;

public class Solution121B {

        final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null;
        BufferedReader in;
        PrintWriter out;
        StringTokenizer tok = new StringTokenizer("");

        void init() throws FileNotFoundException{
                if (ONLINE_JUDGE){
                        in = new BufferedReader(new InputStreamReader(System.in));
                        out = new PrintWriter(System.out);
                }else{
                        in = new BufferedReader(new FileReader("input.txt"));
                        out = new PrintWriter("output.txt");
                }
        }

        String readString() throws IOException{
                while(!tok.hasMoreTokens()){
                        tok = new StringTokenizer(in.readLine());
                }
                return tok.nextToken();
        }

        int readInt() throws IOException{
                return Integer.parseInt(readString());
        }

        long readLong() throws IOException{
                return Long.parseLong(readString());
        }

        double readDouble() throws IOException{
                return Double.parseDouble(readString());
        }

        public static void main(String[] args){
                new Solution121B().run();
        }

        public void run(){
                try{
                        long t1 = System.currentTimeMillis();
                        init();
                        solve();
                        out.close();
                        long t2 = System.currentTimeMillis();
                        System.err.println("Time = "+(t2-t1));
                }catch (Exception e){
                        e.printStackTrace(System.err);
                        System.exit(-1);
                }
        }

        void solve() throws IOException{
        	int n = readInt();
        	int k = readInt();
        	int[] d = new int[n];
        	char[] s = readString().toCharArray();
        	for(int i = 0; i < n; i++){
        		d[i] = (s[i] - '0');
        	}
        	for(int i = 0; i < n-1; i++){
        		if(k == 0){
        			break;
        		}
        		if(i < n - 2){
        			if(d[i] == 4 && (d[i+1] == 7 || d[i+1] == 4) && d[i+2] == 7 && i%2 == 0){
        				if(k%2 != 0){
        					if(d[i+1] == 4) d[i+1] =7;
        					else d[i+1] = 4;
        				}
        				for(int j = 0; j < n; j++){
        					out.print(d[j]);
        				}
        				return;
        			}
        		}
        		if(d[i] == 4 && d[i+1] == 7){
        			if(i%2 == 0) d[i+1] = 4;
        			else d[i] = 7;
        			k--;
        		}
        	}

        	for(int j = 0; j < n; j++){
				out.print(d[j]);
			}
        	return;

        	/*int first = -1;
        	for(int i = 0; i < n-2; i++){
        		if(d[i] == 4 && d[i+1] == 4 && d[i+2] == 7 && i % 2 == 0){
        			first = i;
        		}
        		if(d[i] == 4 && d[i+1] == 7 && d[i+2] == 7 && i % 2 == 0){
        			first = i;
        		}
        	}
        	int j = 0;
        	int countOper = 0;
        	while((j < first || (j < n-1 && first == -1)) && countOper < k){
        		if(d[j] == 4 && d[j+1] == 7){
        			if(j % 2 == 0){
        				d[j] = d[j+1] = 4;
        			}
        			else d[j] = d[j+1] = 7;
        			countOper++;
        		}
        		j++;
        	}
        	if(first == -1 || countOper >= k){
        		for(int i = 0; i < n; i++){
        			out.print(d[i]);
        		}
        		return;
        	}
        	if((k - countOper)%2== 0){
        		for(int i = 0; i < n; i++){
        			out.print(d[i]);
        		}
        		return;
        	}
        	if(d[first+1] == 4){
        		d[first + 1] = 7;
       		}
       		else {
       			d[first+1] = 4;
       		}
        	int u = 0;

        	for(int i = 0; i < n; i++){
        		out.print(d[i]);
        	}
        	*/
        }

        int ModExp(int a, int n, int mod){
        	int res = 1;
        	while (n!=0)
        		if ((n & 1) != 0) {
        			res = (res*a)%mod;
        			--n;
        		}
        		else {
        			a = (a*a)%mod;
        			n >>= 1;
        		}
        	return res;
        }

        static class Utils {

                private Utils() {}

                public static void mergeSort(int[] a) {
                        mergeSort(a, 0, a.length - 1);
                }

                private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
                        final int MAGIC_VALUE = 50;
                        if (leftIndex < rightIndex) {
                                if (rightIndex - leftIndex <= MAGIC_VALUE) {
                                        insertionSort(a, leftIndex, rightIndex);
                                } else {
                                        int middleIndex = (leftIndex + rightIndex) / 2;
                                        mergeSort(a, leftIndex, middleIndex);
                                        mergeSort(a, middleIndex + 1, rightIndex);
                                        merge(a, leftIndex, middleIndex, rightIndex);
                                }
                        }
                }

                private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
                        int length1 = middleIndex - leftIndex + 1;
                        int length2 = rightIndex - middleIndex;
                        int[] leftArray = new int[length1];
                        int[] rightArray = new int[length2];
                        System.arraycopy(a, leftIndex, leftArray, 0, length1);
                        System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
                        for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
                                if (i == length1) {
                                        a[k] = rightArray[j++];
                                } else if (j == length2) {
                                        a[k] = leftArray[i++];
                                } else {
                                        a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
                                }
                        }
                }

                private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
                        for (int i = leftIndex + 1; i <= rightIndex; i++) {
                                int current = a[i];
                                int j = i - 1;
                                while (j >= leftIndex && a[j] > current) {
                                        a[j + 1] = a[j];
                                        j--;
                                }
                                a[j + 1] = current;
                        }
                }
        }

        boolean isPrime(int a){
        	for(int i = 2; i <= sqrt(a); i++)
        		if(a % i == 0) return false;
        	return true;
        }

        static double distance(long x1, long y1, long x2, long y2){
        	return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        }

        static long gcd(long a, long b){
        	if(min(a,b) == 0) return max(a,b);
        	return gcd(max(a, b) % min(a,b), min(a,b));
        }

        static long lcm(long a, long b){
        	return a * b /gcd(a, b);
        }
}


/**
 * Created by IntelliJ IDEA.
 * User: piyushd
 * Date: 3/26/11
 * Time: 10:53 PM
 * To change this template use File | Settings | File Templates.
 */

public class TaskB {

    void run() {
        int n = nextInt(), k = nextInt();
        String s = next();

        int[] a = new int[n];
        for(int i = 0; i < n; i++) a[i] = s.charAt(i) - '0';

        for(int i = 0; i < n - 1 && k > 0; i++) {
            if(a[i] == 4 && a[i + 1] == 7) {
                if(i > 0) {
                    if(a[i - 1] == 4 && i % 2 == 1) {
                        if(k % 2 == 1) {
                            a[i] = a[i + 1] = 7;
                        }
                        k = 0;
                    }
                    else {
                        a[i] = a[i + 1] = 7;
                        k--;
                    }
                }
                else {
                    a[i] = a[i + 1] = 4;
                    k--;
                }
            }
        }

        for(int i = 0; i < n; i++)
            System.out.print(a[i]);
        System.out.println();
    }

    int nextInt() {
        try {
            int c = System.in.read();
            if (c == -1) return c;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = System.in.read();
                if (c == -1) return c;
            }
            if (c == '-') return -nextInt();
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    long nextLong() {
        try {
            int c = System.in.read();
            if (c == -1) return -1;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = System.in.read();
                if (c == -1) return -1;
            }
            if (c == '-') return -nextLong();
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String next() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (Character.isWhitespace(c))
                c = System.in.read();
            do {
                res.append((char) c);
            } while (!Character.isWhitespace(c = System.in.read()));
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }

    String nextLine() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (c == '\r' || c == '\n')
                c = System.in.read();
            do {
                res.append((char) c);
                c = System.in.read();
            } while (c != '\r' && c != '\n');
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        new TaskB().run();
    }
}


/**
 * Created by IntelliJ IDEA.
 * User: piyushd
 * Date: 3/26/11
 * Time: 10:53 PM
 * To change this template use File | Settings | File Templates.
 */

public class TaskB {

    void run() {
        int n = nextInt(), k = nextInt();
        String s = next();

        int[] a = new int[n];
        for(int i = 0; i < n; i++) a[i] = s.charAt(i) - '0';

        for(int i = 0; i < n - 1 && k > 0; i++) {
            if(a[i] == 4 && a[i + 1] == 7) {
                if(i > 0) {
                    if(a[i - 1] == 4 && i % 2 == 1) {
                        if(k % 2 == 1) {
                            a[i] = a[i + 1] = 7;
                        }
                        k = 0;
                    }
                    else {
                        if(i % 2 == 0) a[i] = a[i + 1] = 4;
                        else a[i] = a[i + 1] = 7;
                        k--;
                    }
                }
                else {
                    a[i] = a[i + 1] = 4;
                    k--;
                }
            }
        }

        for(int i = 0; i < n; i++)
            System.out.print(a[i]);
        System.out.println();
    }

    int nextInt() {
        try {
            int c = System.in.read();
            if (c == -1) return c;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = System.in.read();
                if (c == -1) return c;
            }
            if (c == '-') return -nextInt();
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    long nextLong() {
        try {
            int c = System.in.read();
            if (c == -1) return -1;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = System.in.read();
                if (c == -1) return -1;
            }
            if (c == '-') return -nextLong();
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String next() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (Character.isWhitespace(c))
                c = System.in.read();
            do {
                res.append((char) c);
            } while (!Character.isWhitespace(c = System.in.read()));
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }

    String nextLine() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (c == '\r' || c == '\n')
                c = System.in.read();
            do {
                res.append((char) c);
                c = System.in.read();
            } while (c != '\r' && c != '\n');
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        new TaskB().run();
    }
}


/**
 * Created by IntelliJ IDEA.
 * User: piyushd
 * Date: 3/26/11
 * Time: 10:53 PM
 * To change this template use File | Settings | File Templates.
 */

public class TaskB {

    void run() {
        int n = nextInt(), k = nextInt();
        String s = next();

        int[] a = new int[n];
        for(int i = 0; i < n; i++) a[i] = s.charAt(i) - '0';

        for(int i = 0; i < n - 1 && k > 0; i++) {
            if(a[i] == 4 && a[i + 1] == 7) {
                if(i > 0) {
                    if(a[i - 1] == 4 && i % 2 == 1) {
                        if(k % 2 == 1) {
                            a[i] = a[i + 1] = 7;
                        }
                        k = 0;
                    }
                    else {
                        a[i] = a[i + 1] = 4;
                        k--;
                    }
                }
                else {
                    a[i] = a[i + 1] = 4;
                    k--;
                }
            }
        }

        for(int i = 0; i < n; i++)
            System.out.print(a[i]);
        System.out.println();
    }

    int nextInt() {
        try {
            int c = System.in.read();
            if (c == -1) return c;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = System.in.read();
                if (c == -1) return c;
            }
            if (c == '-') return -nextInt();
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    long nextLong() {
        try {
            int c = System.in.read();
            if (c == -1) return -1;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = System.in.read();
                if (c == -1) return -1;
            }
            if (c == '-') return -nextLong();
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String next() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (Character.isWhitespace(c))
                c = System.in.read();
            do {
                res.append((char) c);
            } while (!Character.isWhitespace(c = System.in.read()));
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }

    String nextLine() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (c == '\r' || c == '\n')
                c = System.in.read();
            do {
                res.append((char) c);
                c = System.in.read();
            } while (c != '\r' && c != '\n');
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        new TaskB().run();
    }
}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Ivan Pryvalov (ivan.pryvalov@gmail.com)
 */
public class Codeforces_Round_91_Div1_B implements Runnable{

	static class Data{
		int i, dir;
		public Data(int i, int dir) {
			this.i = i;
			this.dir = dir;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + dir;
			result = prime * result + i;
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Data other = (Data) obj;
			if (dir != other.dir)
				return false;
			if (i != other.i)
				return false;
			return true;
		}
	}

	private void solve() throws IOException {
		int n = scanner.nextInt(); //10^5
		int k = scanner.nextInt(); //10^9;
		String s = scanner.nextToken();
		char[] c = s.toCharArray();

		LinkedList<Data> queue = new LinkedList<Data>();
		queue.add(new Data(0,-1));

		int left = k;
		while (left>0){
			Data d = queue.peek();
			if (queue.size() > 3){
				queue.removeLast();
				if (queue.get(0).equals(queue.get(2))){
					int cLeft = left/2;
					left -= cLeft*2;
				}
			}

			if (left>0){
				int cur = Math.max(d.i-1, 0);
				while (cur<n-1){
					if (c[cur]=='4' && c[cur+1]=='7'){
						if (cur%2==0){
							c[cur+1]='4';
						}else{
							c[cur] = '7';
						}
						queue.addFirst(new Data(cur, cur%2));
						left--;
						break;
					}else{
						cur++;
					}
				}
			}
		}

		out.println(new String(c));
	}

	/////////////////////////////////////////////////
	final int BUF_SIZE = 1024 * 1024 * 8;//important to read long-string tokens properly
	final int INPUT_BUFFER_SIZE = 1024 * 1024 * 8 ;
	final int BUF_SIZE_INPUT = 1024;

	final int BUF_SIZE_OUT = 1024;

	boolean inputFromFile = false;
	String filenamePrefix = "A-small-attempt0";
	String inSuffix = ".in";
	String outSuffix = ".out";

	//InputStream bis;
	//OutputStream bos;
	PrintStream out;
	ByteScanner scanner;
	ByteWriter writer;

	@Override
	public void run() {
		try{
			InputStream bis = null;
			OutputStream bos = null;
			//PrintStream out = null;
			if (inputFromFile){
				File baseFile = new File(getClass().getResource("/").getFile());
				bis = new BufferedInputStream(
						new FileInputStream(new File(
								baseFile, filenamePrefix+inSuffix)),
								INPUT_BUFFER_SIZE);
				bos = new BufferedOutputStream(
						new FileOutputStream(
								new File(baseFile, filenamePrefix+outSuffix)));
				out = new PrintStream(bos);
			}else{
				bis = new BufferedInputStream(System.in, INPUT_BUFFER_SIZE);
				bos = new BufferedOutputStream(System.out);
				out = new PrintStream(bos);
			}
			scanner = new ByteScanner(bis, BUF_SIZE_INPUT, BUF_SIZE);
			writer = new ByteWriter(bos, BUF_SIZE_OUT);

			solve();
			out.flush();
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public interface Constants{
		final static byte ZERO = '0';//48 or 0x30
		final static byte NINE = '9';
		final static byte SPACEBAR = ' '; //32 or 0x20
		final static byte MINUS = '-'; //45 or 0x2d

		final static char FLOAT_POINT = '.';
	}

	public static class EofException extends IOException{
	}

	public static class ByteWriter implements Constants {

		int bufSize = 1024;
		byte[] byteBuf = new byte[bufSize];
		OutputStream os;

		public ByteWriter(OutputStream os, int bufSize){
			this.os = os;
			this.bufSize = bufSize;
		}

		public void writeInt(int num) throws IOException{
	         int byteWriteOffset = byteBuf.length;
	         if (num==0){
	        	 byteBuf[--byteWriteOffset] = ZERO;
	         }else{
	        	 int numAbs = Math.abs(num);
	        	 while (numAbs>0){
	        		 byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
	        		 numAbs /= 10;
	        	 }
	        	 if (num<0)
	        		 byteBuf[--byteWriteOffset] = MINUS;
	         }
	         os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
		}

		/**
		 * Please ensure ar.length <= byteBuf.length!
		 *
		 * @param ar
		 * @throws IOException
		 */
		public void writeByteAr(byte[] ar) throws IOException{
			for (int i = 0; i < ar.length; i++) {
				byteBuf[i] = ar[i];
			}
			os.write(byteBuf,0,ar.length);
		}

		public void writeSpaceBar() throws IOException{
			byteBuf[0] = SPACEBAR;
			os.write(byteBuf,0,1);
		}

	}

	public static class ByteScanner implements Constants{

		InputStream is;

		public ByteScanner(InputStream is, int bufSizeInput, int bufSize){
			this.is = is;
			this.bufSizeInput = bufSizeInput;
			this.bufSize = bufSize;

			byteBufInput = new byte[this.bufSizeInput];
			byteBuf = new byte[this.bufSize];
		}

		public ByteScanner(byte[] data){
			byteBufInput = data;
			bufSizeInput = data.length;
			bufSize = data.length;
			byteBuf = new byte[bufSize];
			byteRead = data.length;
			bytePos = 0;
		}

		private int bufSizeInput;
		private int bufSize;

		byte[] byteBufInput;
		byte by=-1;
		int byteRead=-1;
		int bytePos=-1;

		byte[] byteBuf;
		int totalBytes;

		boolean eofMet = false;

		private byte nextByte() throws IOException{

			if (bytePos<0 || bytePos>=byteRead){
				byteRead = is==null? -1: is.read(byteBufInput);
				bytePos=0;
				if (byteRead<0){
					byteBufInput[bytePos]=-1;//!!!
					if (eofMet)
						throw new EofException();
					eofMet = true;
				}
			}
			return byteBufInput[bytePos++];
		}

		/**
		 * Returns next meaningful character as a byte.

		 *
		 * @return
		 * @throws IOException
		 */
		public byte nextChar() throws IOException{
			while ((by=nextByte())<=0x20);
			return by;
		}

		/**
		 * Returns next meaningful character OR space as a byte.

		 *
		 * @return
		 * @throws IOException
		 */
		public byte nextCharOrSpacebar() throws IOException{
			while ((by=nextByte())<0x20);
			return by;
		}

	    /**
	     * Reads line.
	     *
	     * @return
	     * @throws IOException
	     */
	    public String nextLine() throws IOException {
            readToken((byte)0x20);
            return new String(byteBuf,0,totalBytes);
	    }

	    public byte[] nextLineAsArray() throws IOException {
            readToken((byte)0x20);
            byte[] out = new byte[totalBytes];
            System.arraycopy(byteBuf, 0, out, 0, totalBytes);
            return out;
	    }

	    /**
	     * Reads token. Spacebar is separator char.
	     *
	     * @return
	     * @throws IOException
	     */
	    public String nextToken() throws IOException {
            readToken((byte)0x21);
            return new String(byteBuf,0,totalBytes);
	    }

	    /**
	     * Spacebar is included as separator char
	     *
	     * @throws IOException
	     */
	    private void readToken() throws IOException {
            readToken((byte)0x21);
	    }

	    private void readToken(byte acceptFrom) throws IOException {
            totalBytes = 0;
            while ((by=nextByte())<acceptFrom);
            byteBuf[totalBytes++] = by;
            while ((by=nextByte())>=acceptFrom){
                byteBuf[totalBytes++] = by;
            }
	    }

	    public int nextInt() throws IOException{
			readToken();
			int num=0, i=0;
			boolean sign=false;
			if (byteBuf[i]==MINUS){
				sign = true;
				i++;
			}
			for (; i<totalBytes; i++){
				num*=10;
				num+=byteBuf[i]-ZERO;
			}
			return sign?-num:num;
		}

		public long nextLong() throws IOException{
			readToken();
			long num=0;
			int i=0;
			boolean sign=false;
			if (byteBuf[i]==MINUS){
				sign = true;
				i++;
			}
			for (; i<totalBytes; i++){
				num*=10;
				num+=byteBuf[i]-ZERO;
			}
			return sign?-num:num;
		}

		/*
		//TODO test Unix/Windows formats
		public void toNextLine() throws IOException{
			while ((ch=nextChar())!='\n');
		}
		*/

		public double nextDouble() throws IOException{
			readToken();
			char[] token = new char[totalBytes];
			for (int i = 0; i < totalBytes; i++) {
				token[i] = (char)byteBuf[i];
			}
			return Double.parseDouble(new String(token));
		}

	}

	public static void main(String[] args) {
		new Codeforces_Round_91_Div1_B().run();
	}

}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Ivan Pryvalov (ivan.pryvalov@gmail.com)
 */
public class Codeforces_Round_91_Div1_B implements Runnable{

	static class Data{
		int i, dir;
		public Data(int i, int dir) {
			this.i = i;
			this.dir = dir;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + dir;
			result = prime * result + i;
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Data other = (Data) obj;
			if (dir != other.dir)
				return false;
			if (i != other.i)
				return false;
			return true;
		}
	}

	private void solve() throws IOException {
		int n = scanner.nextInt(); //10^5
		int k = scanner.nextInt(); //10^9;
		String s = scanner.nextToken();
		char[] c = s.toCharArray();

		LinkedList<Data> queue = new LinkedList<Data>();
		queue.add(new Data(0,-1));

		int left = k;
		while (left>0){
			Data d = queue.peek();
			if (queue.size() > 3){
				queue.removeLast();
				if (queue.get(0).equals(queue.get(2))){
					int cLeft = left/2;
					left -= cLeft*2;
				}
			}

			if (left>0){
				int cur = Math.max(d.i-1, 0);
				while (cur<n-1){
					if (c[cur]=='4' && c[cur+1]=='7'){
						if (cur%2==0){
							c[cur+1]='4';
						}else{
							c[cur] = '7';
						}
						queue.addFirst(new Data(cur, cur%2));
						left--;
						break;
					}else{
						cur++;
					}
				}
				if (cur>=n-1)
					break;
			}
		}

		out.println(new String(c));
	}

	/////////////////////////////////////////////////
	final int BUF_SIZE = 1024 * 1024 * 8;//important to read long-string tokens properly
	final int INPUT_BUFFER_SIZE = 1024 * 1024 * 8 ;
	final int BUF_SIZE_INPUT = 1024;

	final int BUF_SIZE_OUT = 1024;

	boolean inputFromFile = false;
	String filenamePrefix = "A-small-attempt0";
	String inSuffix = ".in";
	String outSuffix = ".out";

	//InputStream bis;
	//OutputStream bos;
	PrintStream out;
	ByteScanner scanner;
	ByteWriter writer;

	@Override
	public void run() {
		try{
			InputStream bis = null;
			OutputStream bos = null;
			//PrintStream out = null;
			if (inputFromFile){
				File baseFile = new File(getClass().getResource("/").getFile());
				bis = new BufferedInputStream(
						new FileInputStream(new File(
								baseFile, filenamePrefix+inSuffix)),
								INPUT_BUFFER_SIZE);
				bos = new BufferedOutputStream(
						new FileOutputStream(
								new File(baseFile, filenamePrefix+outSuffix)));
				out = new PrintStream(bos);
			}else{
				bis = new BufferedInputStream(System.in, INPUT_BUFFER_SIZE);
				bos = new BufferedOutputStream(System.out);
				out = new PrintStream(bos);
			}
			scanner = new ByteScanner(bis, BUF_SIZE_INPUT, BUF_SIZE);
			writer = new ByteWriter(bos, BUF_SIZE_OUT);

			solve();
			out.flush();
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public interface Constants{
		final static byte ZERO = '0';//48 or 0x30
		final static byte NINE = '9';
		final static byte SPACEBAR = ' '; //32 or 0x20
		final static byte MINUS = '-'; //45 or 0x2d

		final static char FLOAT_POINT = '.';
	}

	public static class EofException extends IOException{
	}

	public static class ByteWriter implements Constants {

		int bufSize = 1024;
		byte[] byteBuf = new byte[bufSize];
		OutputStream os;

		public ByteWriter(OutputStream os, int bufSize){
			this.os = os;
			this.bufSize = bufSize;
		}

		public void writeInt(int num) throws IOException{
	         int byteWriteOffset = byteBuf.length;
	         if (num==0){
	        	 byteBuf[--byteWriteOffset] = ZERO;
	         }else{
	        	 int numAbs = Math.abs(num);
	        	 while (numAbs>0){
	        		 byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
	        		 numAbs /= 10;
	        	 }
	        	 if (num<0)
	        		 byteBuf[--byteWriteOffset] = MINUS;
	         }
	         os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
		}

		/**
		 * Please ensure ar.length <= byteBuf.length!
		 *
		 * @param ar
		 * @throws IOException
		 */
		public void writeByteAr(byte[] ar) throws IOException{
			for (int i = 0; i < ar.length; i++) {
				byteBuf[i] = ar[i];
			}
			os.write(byteBuf,0,ar.length);
		}

		public void writeSpaceBar() throws IOException{
			byteBuf[0] = SPACEBAR;
			os.write(byteBuf,0,1);
		}

	}

	public static class ByteScanner implements Constants{

		InputStream is;

		public ByteScanner(InputStream is, int bufSizeInput, int bufSize){
			this.is = is;
			this.bufSizeInput = bufSizeInput;
			this.bufSize = bufSize;

			byteBufInput = new byte[this.bufSizeInput];
			byteBuf = new byte[this.bufSize];
		}

		public ByteScanner(byte[] data){
			byteBufInput = data;
			bufSizeInput = data.length;
			bufSize = data.length;
			byteBuf = new byte[bufSize];
			byteRead = data.length;
			bytePos = 0;
		}

		private int bufSizeInput;
		private int bufSize;

		byte[] byteBufInput;
		byte by=-1;
		int byteRead=-1;
		int bytePos=-1;

		byte[] byteBuf;
		int totalBytes;

		boolean eofMet = false;

		private byte nextByte() throws IOException{

			if (bytePos<0 || bytePos>=byteRead){
				byteRead = is==null? -1: is.read(byteBufInput);
				bytePos=0;
				if (byteRead<0){
					byteBufInput[bytePos]=-1;//!!!
					if (eofMet)
						throw new EofException();
					eofMet = true;
				}
			}
			return byteBufInput[bytePos++];
		}

		/**
		 * Returns next meaningful character as a byte.

		 *
		 * @return
		 * @throws IOException
		 */
		public byte nextChar() throws IOException{
			while ((by=nextByte())<=0x20);
			return by;
		}

		/**
		 * Returns next meaningful character OR space as a byte.

		 *
		 * @return
		 * @throws IOException
		 */
		public byte nextCharOrSpacebar() throws IOException{
			while ((by=nextByte())<0x20);
			return by;
		}

	    /**
	     * Reads line.
	     *
	     * @return
	     * @throws IOException
	     */
	    public String nextLine() throws IOException {
            readToken((byte)0x20);
            return new String(byteBuf,0,totalBytes);
	    }

	    public byte[] nextLineAsArray() throws IOException {
            readToken((byte)0x20);
            byte[] out = new byte[totalBytes];
            System.arraycopy(byteBuf, 0, out, 0, totalBytes);
            return out;
	    }

	    /**
	     * Reads token. Spacebar is separator char.
	     *
	     * @return
	     * @throws IOException
	     */
	    public String nextToken() throws IOException {
            readToken((byte)0x21);
            return new String(byteBuf,0,totalBytes);
	    }

	    /**
	     * Spacebar is included as separator char
	     *
	     * @throws IOException
	     */
	    private void readToken() throws IOException {
            readToken((byte)0x21);
	    }

	    private void readToken(byte acceptFrom) throws IOException {
            totalBytes = 0;
            while ((by=nextByte())<acceptFrom);
            byteBuf[totalBytes++] = by;
            while ((by=nextByte())>=acceptFrom){
                byteBuf[totalBytes++] = by;
            }
	    }

	    public int nextInt() throws IOException{
			readToken();
			int num=0, i=0;
			boolean sign=false;
			if (byteBuf[i]==MINUS){
				sign = true;
				i++;
			}
			for (; i<totalBytes; i++){
				num*=10;
				num+=byteBuf[i]-ZERO;
			}
			return sign?-num:num;
		}

		public long nextLong() throws IOException{
			readToken();
			long num=0;
			int i=0;
			boolean sign=false;
			if (byteBuf[i]==MINUS){
				sign = true;
				i++;
			}
			for (; i<totalBytes; i++){
				num*=10;
				num+=byteBuf[i]-ZERO;
			}
			return sign?-num:num;
		}

		/*
		//TODO test Unix/Windows formats
		public void toNextLine() throws IOException{
			while ((ch=nextChar())!='\n');
		}
		*/

		public double nextDouble() throws IOException{
			readToken();
			char[] token = new char[totalBytes];
			for (int i = 0; i < totalBytes; i++) {
				token[i] = (char)byteBuf[i];
			}
			return Double.parseDouble(new String(token));
		}

	}

	public static void main(String[] args) {
		new Codeforces_Round_91_Div1_B().run();
	}

}


#include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define fr(i,a,b) for(int i=a;i>=b;i--)
//typedef long long ll;
typedef long long ll;
#define vt vector
#define all(v)  v.begin(),v.end()
typedef vt<long long> vii;
const ll M=998244353;
 ll a[200005][25];
 ll n;
 string s1,s;
 bool check(ll x)
 {
     if(x<s1.size())
        return false;
     vii v1(26,0),v2(26,0);
     f(i,0,x)
     v1[s[i]-'a']++;
     f(i,0,s1.size())
     v2[s1[i]-'a']++;
     ll flag=0;
     f(i,0,26)
     {
         if(v1[i]<v2[i])
            return false;
     }
     return true;
 }
 int main()
{
    cin>>n;
    cin>>s;
    ll m;
    cin>>m;
    f(i,0,m)
    {
        cin>>s1;
     ll l=s1.size(),h=n;
     ll ans;
     while(l<=h)
     {
         ll mid=(l+h)/2;
         if(mid<s1.size())
            break;
         if(check(mid))
         {
             ans=mid;
             h=mid-1;
         }
         else
            l=mid+1;
     }
     cout<<ans<<endl;
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define fr(i,a,b) for(int i=a;i>=b;i--)
//typedef long long ll;
typedef long long ll;
#define vt vector
#define all(v)  v.begin(),v.end()
typedef vt<long long> vii;
const ll M=998244353;
 ll a[200005][25];
 ll n;
 string s1,s;
 bool check(ll x,vector<ll> v2)
 {
     if(x<s1.size())
        return false;
     vii v1(26,0);
     f(i,0,x)
     v1[s[i]-'a']++;
     ll flag=0;
     f(i,0,26)
     {
         if(v1[i]<v2[i])
            return false;
     }
     return true;
 }
 int main()
{
    cin>>n;
    cin>>s;
    ll m;
    cin>>m;
    f(i,0,m)
    {
        cin>>s1;
     ll l=s1.size(),h=n;
     ll ans;
    vector<ll> v1(26,0);
      f(i,0,l)
     v1[s1[i]-'a']++;

     while(l<=h)
     {
         ll mid=(l+h)/2;
         if(mid<s1.size())
            break;
         if(check(mid,v1))
         {
             ans=mid;
             h=mid-1;
         }
         else
            l=mid+1;
     }
     cout<<ans<<endl;
    }
  return 0;
}


// Problem : B. Letters Shop
// Contest : Codeforces - Educational Codeforces Round 67 (Rated for Div. 2)
// URL : https://codeforces.com/problemset/problem/1187/B
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define hell1 1000000007
#define hell2 1000000009
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define ll long long
#define ld long double
#define no_of_digits(n) floor(log10(n)) + 1
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define see(x) cout << #x << "=" << x << endl;
#define see2(x, y) cout << #x << "=" << x << "\t" << #y << "=" << y << endl;
#define see3(x, y, z) cout << #x << "=" << x << "\t" << #y << "=" << y << "\t" << #z << "=" << z << endl;
#define INF 1e9+5
#define LL_MAX 1e18+5

using namespace std;

const ld PI = acos((ld)-1);

ll modmult(ll a,ll b, ll p=hell1){   return ((a%p)*(b%p))%p; }
ll modmultbig(ll a,ll b,ll mod=hell1){   if(a==0 or b==0) return 0;if(a==1 or b==1) return (a*b)%mod;ll cur=modmultbig(a,b/2,mod);cur=(2*cur)%mod;if(b%2) cur=(cur+a)%mod;return cur;}
ll modadd(ll a, ll b, ll p=hell1){ if(a>=p) a%=p; if(b>=p) b%=p; if(a+b>p) return (a + b)%p; else return a+b;}
ll modsub(ll a, ll b, ll p=hell1){return (a%p - b%p + p)%p;}
ll fpow(ll n, ll k, ll p = hell1) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = modmult(r,n,p); n = modmult(n,n,p);} return r;}
ll inv(ll a, ll p = hell1) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = hell1){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}

int isPowerof2(ll x) { return (x && !(x & (x-1))); }

ll log2(ll x) { ll res = 0; while (x >>= 1) res++; return res; }

bool isPerfectSquare(long double x) {long double sr = sqrt(x); return ((sr - floor(sr)) == 0); }

bool boundary(ll i,ll j,ll n, ll m){return (i>=0 && i<n && j>=0 &&  j<m);} //matrix boundary checking

ll ceil(const ll &a, const ll &b){return (a - 1)/b + 1;}
int lcm(int a, int b){return (a*b)/__gcd(a, b);}

string alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

// cout << fixed for removing e notation

bool comp2(const pair<int, int>&a, const pair<int, int>&b)
{
    if(a.second < b.second)
        return true;
    else if(a.second == b.second)
    {
        if(a.fi > b.fi) return true;
        else return false;
    }
    else return false;
}

bool prime[10000005];
void SieveOfEratosthenes(int n)
{
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = 0;
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
    }
}

int main()
{
	int n;
	cin >> n;

	string s;
	cin >> s;
	ll cnt[26][n];

	ms(cnt, 0);

	for(int i=0;i<n;i++)
	{
		cnt[s[i]-'a'][i]++;
	}

	// for(int i=0;i<n;i++)
	// {
		// for(int j=0;j<26;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	for(int i=0;i<26;i++)
	{
		for(int j=1;j<n;j++)
			cnt[i][j] += cnt[i][j-1];
	}

	// cout << "\n\n\n";
//
	// for(int i=0;i<26;i++)
	// {
		// for(int j=0;j<n;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	ll q;
	cin >> q;

	while(q--)
	{
		string t;
		cin >> t;

		ll req[26] = {0};
		for(auto x: t) req[x-'a']++;

		vector<int>idx;

		for(int i=0;i<26;i++)
		{
			auto itr = lower_bound(cnt[i], cnt[i]+n, req[i]) - cnt[i];
			// see(itr);
			idx.pb(itr+1);
		}

		cout << *max_element(all(idx)) << "\n";

		// int pos = -1;
		// for(int i=0;i<n;i++)
		// {
			// int ok = 1;
			// pos = i;
			// for(int j=0;j<26;j++)
			// {
				// if(cnt[i][j] < req[j])
				// {
					// ok = 0;
					// break;
				// }
			// }
//
			// if(ok)
			// {
				// cout << pos+1 << "\n";
				// break;
			// }
		// }

	}

}


// Problem : B. Letters Shop
// Contest : Codeforces - Educational Codeforces Round 67 (Rated for Div. 2)
// URL : https://codeforces.com/problemset/problem/1187/B
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define hell1 1000000007
#define hell2 1000000009
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define ll long long
#define ld long double
#define no_of_digits(n) floor(log10(n)) + 1
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define see(x) cout << #x << "=" << x << endl;
#define see2(x, y) cout << #x << "=" << x << "\t" << #y << "=" << y << endl;
#define see3(x, y, z) cout << #x << "=" << x << "\t" << #y << "=" << y << "\t" << #z << "=" << z << endl;
#define INF 1e9+5
#define LL_MAX 1e18+5

using namespace std;

const ld PI = acos((ld)-1);

ll modmult(ll a,ll b, ll p=hell1){   return ((a%p)*(b%p))%p; }
ll modmultbig(ll a,ll b,ll mod=hell1){   if(a==0 or b==0) return 0;if(a==1 or b==1) return (a*b)%mod;ll cur=modmultbig(a,b/2,mod);cur=(2*cur)%mod;if(b%2) cur=(cur+a)%mod;return cur;}
ll modadd(ll a, ll b, ll p=hell1){ if(a>=p) a%=p; if(b>=p) b%=p; if(a+b>p) return (a + b)%p; else return a+b;}
ll modsub(ll a, ll b, ll p=hell1){return (a%p - b%p + p)%p;}
ll fpow(ll n, ll k, ll p = hell1) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = modmult(r,n,p); n = modmult(n,n,p);} return r;}
ll inv(ll a, ll p = hell1) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = hell1){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}

int isPowerof2(ll x) { return (x && !(x & (x-1))); }

ll log2(ll x) { ll res = 0; while (x >>= 1) res++; return res; }

bool isPerfectSquare(long double x) {long double sr = sqrt(x); return ((sr - floor(sr)) == 0); }

bool boundary(ll i,ll j,ll n, ll m){return (i>=0 && i<n && j>=0 &&  j<m);} //matrix boundary checking

ll ceil(const ll &a, const ll &b){return (a - 1)/b + 1;}
int lcm(int a, int b){return (a*b)/__gcd(a, b);}

string alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

// cout << fixed for removing e notation

bool comp2(const pair<int, int>&a, const pair<int, int>&b)
{
    if(a.second < b.second)
        return true;
    else if(a.second == b.second)
    {
        if(a.fi > b.fi) return true;
        else return false;
    }
    else return false;
}

bool prime[10000005];
void SieveOfEratosthenes(int n)
{
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = 0;
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
    }
}

int main()
{
	int n;
	cin >> n;

	string s;
	cin >> s;
	ll cnt[26][n];

	ms(cnt, 0);

	for(int i=0;i<n;i++)
		cnt[s[i]-'a'][i]++;

	for(int i=0;i<26;i++)
	{
		for(int j=1;j<n;j++)
			cnt[i][j] += cnt[i][j-1];
	}

	ll q;
	cin >> q;

	while(q--)
	{
		string t;
		cin >> t;

		ll req[26] = {0};
		for(auto x: t) req[x-'a']++;

		vector<int>idx;

		for(int i=0;i<26;i++)
		{
			auto itr = lower_bound(cnt[i], cnt[i]+n, req[i]) - cnt[i];
			idx.pb(itr+1);
		}

		cout << *max_element(all(idx)) << "\n";
	}
}


// Problem : B. Letters Shop
// Contest : Codeforces - Educational Codeforces Round 67 (Rated for Div. 2)
// URL : https://codeforces.com/problemset/problem/1187/B
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define hell1 1000000007
#define hell2 1000000009
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define ll long long
#define ld long double
#define no_of_digits(n) floor(log10(n)) + 1
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define see(x) cout << #x << "=" << x << endl;
#define see2(x, y) cout << #x << "=" << x << "\t" << #y << "=" << y << endl;
#define see3(x, y, z) cout << #x << "=" << x << "\t" << #y << "=" << y << "\t" << #z << "=" << z << endl;
#define INF 1e9+5
#define LL_MAX 1e18+5

using namespace std;

const ld PI = acos((ld)-1);

ll modmult(ll a,ll b, ll p=hell1){   return ((a%p)*(b%p))%p; }
ll modmultbig(ll a,ll b,ll mod=hell1){   if(a==0 or b==0) return 0;if(a==1 or b==1) return (a*b)%mod;ll cur=modmultbig(a,b/2,mod);cur=(2*cur)%mod;if(b%2) cur=(cur+a)%mod;return cur;}
ll modadd(ll a, ll b, ll p=hell1){ if(a>=p) a%=p; if(b>=p) b%=p; if(a+b>p) return (a + b)%p; else return a+b;}
ll modsub(ll a, ll b, ll p=hell1){return (a%p - b%p + p)%p;}
ll fpow(ll n, ll k, ll p = hell1) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = modmult(r,n,p); n = modmult(n,n,p);} return r;}
ll inv(ll a, ll p = hell1) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = hell1){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}

int isPowerof2(ll x) { return (x && !(x & (x-1))); }

ll log2(ll x) { ll res = 0; while (x >>= 1) res++; return res; }

bool isPerfectSquare(long double x) {long double sr = sqrt(x); return ((sr - floor(sr)) == 0); }

bool boundary(ll i,ll j,ll n, ll m){return (i>=0 && i<n && j>=0 &&  j<m);} //matrix boundary checking

ll ceil(const ll &a, const ll &b){return (a - 1)/b + 1;}
int lcm(int a, int b){return (a*b)/__gcd(a, b);}

string alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

// cout << fixed for removing e notation

bool comp2(const pair<int, int>&a, const pair<int, int>&b)
{
    if(a.second < b.second)
        return true;
    else if(a.second == b.second)
    {
        if(a.fi > b.fi) return true;
        else return false;
    }
    else return false;
}

bool prime[10000005];
void SieveOfEratosthenes(int n)
{
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = 0;
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
    }
}

int main()
{
	int n;
	cin >> n;

	string s;
	cin >> s;
	ll cnt[n][26];

	ms(cnt, 0);

	for(int i=0;i<n;i++)
	{
		cnt[i][s[i]-'a']++;
	}

	// for(int i=0;i<n;i++)
	// {
		// for(int j=0;j<26;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	for(int i=1;i<n;i++)
	{
		for(int j=0;j<26;j++)
			cnt[i][j] += cnt[i-1][j];
	}

	// cout << "\n\n\n";
//
	// for(int i=0;i<n;i++)
	// {
		// for(int j=0;j<26;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	ll q;
	cin >> q;

	while(q--)
	{
		string t;
		cin >> t;

		ll req[26] = {0};
		for(auto x: t) req[x-'a']++;

		int pos = -1;
		for(int i=0;i<n;i++)
		{
			int ok = 1;
			pos = i;
			for(int j=0;j<26;j++)
			{
				if(cnt[i][j] < req[j])
				{
					ok = 0;
					break;
				}
			}

			if(ok)
			{
				cout << pos+1 << "\n";
				break;
			}
		}
	}

}


// Problem : B. Letters Shop
// Contest : Codeforces - Educational Codeforces Round 67 (Rated for Div. 2)
// URL : https://codeforces.com/problemset/problem/1187/B
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define hell1 1000000007
#define hell2 1000000009
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define ll long long
#define ld long double
#define no_of_digits(n) floor(log10(n)) + 1
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define see(x) cout << #x << "=" << x << endl;
#define see2(x, y) cout << #x << "=" << x << "\t" << #y << "=" << y << endl;
#define see3(x, y, z) cout << #x << "=" << x << "\t" << #y << "=" << y << "\t" << #z << "=" << z << endl;
#define INF 1e9+5
#define LL_MAX 1e18+5

using namespace std;

const ld PI = acos((ld)-1);

ll modmult(ll a,ll b, ll p=hell1){   return ((a%p)*(b%p))%p; }
ll modmultbig(ll a,ll b,ll mod=hell1){   if(a==0 or b==0) return 0;if(a==1 or b==1) return (a*b)%mod;ll cur=modmultbig(a,b/2,mod);cur=(2*cur)%mod;if(b%2) cur=(cur+a)%mod;return cur;}
ll modadd(ll a, ll b, ll p=hell1){ if(a>=p) a%=p; if(b>=p) b%=p; if(a+b>p) return (a + b)%p; else return a+b;}
ll modsub(ll a, ll b, ll p=hell1){return (a%p - b%p + p)%p;}
ll fpow(ll n, ll k, ll p = hell1) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = modmult(r,n,p); n = modmult(n,n,p);} return r;}
ll inv(ll a, ll p = hell1) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = hell1){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}

int isPowerof2(ll x) { return (x && !(x & (x-1))); }

ll log2(ll x) { ll res = 0; while (x >>= 1) res++; return res; }

bool isPerfectSquare(long double x) {long double sr = sqrt(x); return ((sr - floor(sr)) == 0); }

bool boundary(ll i,ll j,ll n, ll m){return (i>=0 && i<n && j>=0 &&  j<m);} //matrix boundary checking

ll ceil(const ll &a, const ll &b){return (a - 1)/b + 1;}
int lcm(int a, int b){return (a*b)/__gcd(a, b);}

string alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

// cout << fixed for removing e notation

bool comp2(const pair<int, int>&a, const pair<int, int>&b)
{
    if(a.second < b.second)
        return true;
    else if(a.second == b.second)
    {
        if(a.fi > b.fi) return true;
        else return false;
    }
    else return false;
}

bool prime[10000005];
void SieveOfEratosthenes(int n)
{
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = 0;
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
    }
}

int main()
{
	int n;
	cin >> n;

	string s;
	cin >> s;
	ll cnt[n][26];

	ms(cnt, 0);

	for(int i=0;i<n;i++)
	{
		cnt[i][s[i]-'a']++;
	}

	// for(int i=0;i<n;i++)
	// {
		// for(int j=0;j<26;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	for(int i=1;i<n;i++)
	{
		for(int j=0;j<26;j++)
			cnt[i][j] += cnt[i-1][j];
	}

	// cout << "\n\n\n";
//
	// for(int i=0;i<n;i++)
	// {
		// for(int j=0;j<26;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	ll q;
	cin >> q;

	while(q--)
	{
		string t;
		cin >> t;

		ll req[26] = {0};
		for(auto x: t) req[x-'a']++;

		sort(all(t));
		auto itr = unique(all(t));
		t.resize(distance(begin(t), itr));

		int pos = -1;
		for(int i=0;i<n;i++)
		{
			int ok = 1;
			pos = i;
			for(int j=0;j<26;j++)
			{
				if(cnt[i][j] < req[j])
				{
					ok = 0;
					break;
				}
			}

			if(ok)
			{
				cout << pos+1 << "\n";
				break;
			}
		}
	}

}


// Problem : B. Letters Shop
// Contest : Codeforces - Educational Codeforces Round 67 (Rated for Div. 2)
// URL : https://codeforces.com/problemset/problem/1187/B
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define hell1 1000000007
#define hell2 1000000009
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define ll long long
#define ld long double
#define no_of_digits(n) floor(log10(n)) + 1
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define see(x) cout << #x << "=" << x << endl;
#define see2(x, y) cout << #x << "=" << x << "\t" << #y << "=" << y << endl;
#define see3(x, y, z) cout << #x << "=" << x << "\t" << #y << "=" << y << "\t" << #z << "=" << z << endl;
#define INF 1e9+5
#define LL_MAX 1e18+5

using namespace std;

const ld PI = acos((ld)-1);

ll modmult(ll a,ll b, ll p=hell1){   return ((a%p)*(b%p))%p; }
ll modmultbig(ll a,ll b,ll mod=hell1){   if(a==0 or b==0) return 0;if(a==1 or b==1) return (a*b)%mod;ll cur=modmultbig(a,b/2,mod);cur=(2*cur)%mod;if(b%2) cur=(cur+a)%mod;return cur;}
ll modadd(ll a, ll b, ll p=hell1){ if(a>=p) a%=p; if(b>=p) b%=p; if(a+b>p) return (a + b)%p; else return a+b;}
ll modsub(ll a, ll b, ll p=hell1){return (a%p - b%p + p)%p;}
ll fpow(ll n, ll k, ll p = hell1) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = modmult(r,n,p); n = modmult(n,n,p);} return r;}
ll inv(ll a, ll p = hell1) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = hell1){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}

int isPowerof2(ll x) { return (x && !(x & (x-1))); }

ll log2(ll x) { ll res = 0; while (x >>= 1) res++; return res; }

bool isPerfectSquare(long double x) {long double sr = sqrt(x); return ((sr - floor(sr)) == 0); }

bool boundary(ll i,ll j,ll n, ll m){return (i>=0 && i<n && j>=0 &&  j<m);} //matrix boundary checking

ll ceil(const ll &a, const ll &b){return (a - 1)/b + 1;}
int lcm(int a, int b){return (a*b)/__gcd(a, b);}

string alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

// cout << fixed for removing e notation

bool comp2(const pair<int, int>&a, const pair<int, int>&b)
{
    if(a.second < b.second)
        return true;
    else if(a.second == b.second)
    {
        if(a.fi > b.fi) return true;
        else return false;
    }
    else return false;
}

bool prime[10000005];
void SieveOfEratosthenes(int n)
{
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = 0;
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
    }
}

int main()
{
	int n;
	cin >> n;

	string s;
	cin >> s;
	ll cnt[n][26];

	ms(cnt, 0);

	for(int i=0;i<n;i++)
	{
		cnt[i][s[i]-'a']++;
	}

	// for(int i=0;i<n;i++)
	// {
		// for(int j=0;j<26;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	for(int i=1;i<n;i++)
	{
		for(int j=0;j<26;j++)
			cnt[i][j] += cnt[i-1][j];
	}

	// cout << "\n\n\n";
//
	// for(int i=0;i<n;i++)
	// {
		// for(int j=0;j<26;j++) cout << cnt[i][j] << " ";
		// cout << "\n";
	// }

	ll q;
	cin >> q;

	while(q--)
	{
		string t;
		cin >> t;

		sort(all(t));
		auto itr = unique(all(t));
		t.resize(distance(begin(t), itr));

		ll req[26] = {0};
		for(auto x: t) req[x-'a']++;

		int pos = -1;
		for(int i=0;i<n;i++)
		{
			int ok = 1;
			pos = i;
			for(int j=0;j<26;j++)
			{
				if(cnt[i][j] < req[j])
				{
					ok = 0;
					break;
				}
			}

			if(ok)
			{
				cout << pos+1 << "\n";
				break;
			}
		}
	}

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m,i;
	std::vector<ll>v[27];
	string s;
	cin >> n;
	cin >> s;
	for(i=0;i<n;i++)
	{
		v[s[i]-'a'].push_back(i+1);
	}
	cin >> m;
	for(i=0;i<m;i++)
	{
		ll count[27]={0},x,ans=0,j=0;
		string a;
		cin >> a;
		for(j=0;j<a.size();j++)
		{
			ll w=a[j]-'a';
			x=v[w][count[w]++];
			ans=max(x,ans);

		}
		cout<< ans<<"\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m,i;
	std::vector<ll>v[27];
	string s;
	cin >> n;
	cin >> s;
	for(i=0;i<n;i++)
	{
		v[s[i]-'a'].push_back(i+1);
	}
	cin >> m;
	for(i=0;i<m;i++)
	{
		ll count[27]={0},x,ans=0,j=0;
		string a;
		cin >> a;
		for(j=0;j<a.size();j++)
		{
			x=v[a[j]-'a'][count[a[j]-'a']++];
			ans=max(x,ans);

		}
		cout<< ans<<"\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m,i;
	std::vector<ll>v[27];
	string s;
	cin >> n;
	cin >> s;
	for(i=0;i<n;i++)
	{
		v[s[i]-'a'].push_back(i+1);
	}
	cin >> m;
	for(i=0;i<m;i++)
	{
		ll count[27]={0},x,ans=0,j=0;
		string a;
		cin >> a;
		for(j=0;j<a.size();j++)
		{
			ll w=a[j]-'a';
			x=v[w][count[w]];
			w++;
			ans=max(x,ans);

		}
		cout<< ans<<"\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m,i;
	map<char,ll>pos;
	//map<char,ll>::iterator itr;
	string s;
	cin >> n;
	cin >> s;
	ll r=0;
	for(i=0;i<n;i++)
	{
		if(pos[s[i]]==0)
			pos[s[i]]=i+1;
	}
	cin >> m;
	for(i=0;i<m;i++)
	{
		ll l,count[n]={0},j;
		string a;
		set<char>c;
		set<char>::iterator it;
		cin >> a;
		l=a.length();
		for(j=0;j<l;j++)
		{
			c.insert(a[j]);
		}
		j=0;
		ll max=0;
		for(it=c.begin();it!=c.end();++it)
		{
				count[j]=pos[*it];
				//cout<<pos[*it];
				if(count[j]>max)
					max=count[j];
				j++;

		}
		cout<< max<<"\n";

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m,i;
	deque<ll>pos[27];
	string s;
	cin >> n;
	cin >> s;
	for(i=0;i<n;i++)
		pos[s[i]-'a'].push_back(i+1);
	//cout << pos[0].front();
	cin >> m;
	while(m--)
	{
		string a;
		cin >> a;
		deque<ll>posi[27];
		for(i=0;i<n;i++)
			posi[s[i]-'a'].push_back(i+1);
		ll count=0;
		sort(a.begin(),a.end());
		for(i=0;i<a.length();i++)
		{
			ll x=posi[a[i]-'a'].front();
			//cout<<x<<" ";
			posi[a[i]-'a'].pop_front();
			if(count<x)
				count=x;
		}
		cout<< count<<"\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,m,i;
	deque<ll>pos[27];
	string s;
	cin >> n;
	cin >> s;
	for(i=0;i<n;i++)
		pos[s[i]-'a'].push_back(i+1);
	//cout << pos[0].front();
	cin >> m;
	while(m--)
	{
		string a;
		cin >> a;
		deque<ll>posi[27];
	//	for(i=0;i<n;i++)
	//		posi[s[i]-'a'].push_back(i+1);
		for(i=0;i<27;i++)
			posi[i]=pos[i];
		ll count=0;
		sort(a.begin(),a.end());
		for(i=0;i<a.length();i++)
		{
			ll x=posi[a[i]-'a'].front();
			//cout<<x<<" ";
			posi[a[i]-'a'].pop_front();
			if(count<x)
				count=x;
		}
		cout<< count<<"\n";
	}
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define ll long long
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define all(a) a.begin(), a.end()
#define PI 3.141592653589793238462643383279502884197169399375105820974

typedef vector <pair <int ,int > > vpii;
typedef vector <pair <ll ,ll > > vpll;
typedef vector <pair <double ,double > > vpdd;
typedef vector<int> vi;
typedef vector <ll> vl;
typedef vector <char> vc;
typedef vector <double> vd;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
double f(double x, double y, double x1, double y1){
  return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
}

const int inf = INT_MAX;
int main() {
 //freopen("input.txt", "r", stdin);
 //freopen("output.txt", "w", stdout);
 ios_base::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
 int n;
 cin >> n;
 string s;
 cin >> s;
 unordered_map <char, vector<int>> b;
 for(int i = 0; i < s.size(); i++) {
     b[s[i]].push_back(i);
 }
 int m;
 cin >> m;
 for(int i = 0; i < m; i++) {
   string buf;
   cin >> buf;
   int max = -1;
   unordered_map<char, vector<int>> tmp = b;
   for(int j = 0; j < buf.size(); j++){
        bool ch = true;
        if (tmp.count(buf[j]) != 0) {
            for(int k = 0; k < tmp[buf[j]].size(); k++) {
                if (tmp[buf[j]][k] != inf) {
                    ch = false;
                    max = std::max(max, tmp[buf[j]][k]);
                    tmp[buf[j]][k] = inf;
                    break;
                }
            }
        }
        if (ch) {
            cout << -1 << endl;
            break;
        }
    }
    cout << max + 1 << endl;
    }
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define ll long long
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define all(a) a.begin(), a.end()
#define PI 3.141592653589793238462643383279502884197169399375105820974

typedef vector <pair <int ,int > > vpii;
typedef vector <pair <ll ,ll > > vpll;
typedef vector <pair <double ,double > > vpdd;
typedef vector<int> vi;
typedef vector <ll> vl;
typedef vector <char> vc;
typedef vector <double> vd;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
double f(double x, double y, double x1, double y1){
  return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
}

const int inf = INT_MAX;
int main() {
 //freopen("input.txt", "r", stdin);
 //freopen("output.txt", "w", stdout);
 ios_base::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
 int n;
 cin >> n;
 string s;
 cin >> s;
 map <char, vector<int>> b;
 for(int i = 0; i < s.size(); i++) {
     b[s[i]].push_back(i);
 }
 int m;
 cin >> m;
 for(int i = 0; i < m; i++) {
   string buf;
   cin >> buf;
   int max = -1;
   map<char, vector<int>> tmp = b;
   for(int j = 0; j < buf.size(); j++){
        bool ch = true;
        if (tmp.count(buf[j]) != 0) {
            for(int k = 0; k < tmp[buf[j]].size(); k++) {
                if (tmp[buf[j]][k] != inf) {
                    ch = false;
                    max = std::max(max, tmp[buf[j]][k]);
                    tmp[buf[j]][k] = inf;
                    break;
                }
            }
        }
        if (ch) {
            cout << -1 << endl;
            break;
        }
    }
    cout << max + 1 << endl;
    }
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

//const int inf = INT_MAX;

int main() {
  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    string s;
    cin >> s;
    map<char, vector<int>> b;
    for (int i = 0; i < s.size(); i++) {
        b[s[i]].push_back(i);
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        string buf;
        cin >> buf;
        int max = -1;
        std::map<char, int> shift;
        for(char c = 'a'; c <= 'z'; c++) {
            shift[c] = 0;
        }
        for (char c : buf) {
            bool ch = true;
            if (b.count(c) != 0) {
                if (shift[c] >= (int)b[c].size()) {
                    // hiy
                } else {
                    ch = false;
                    max = std::max(max, b[c][shift[c]]);
                    shift[c]++;
                }
            }
            if (ch) {
                cout << -1 << "\n";
                break;
            }
        }
        cout << max + 1 << endl;
    }
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

const int inf = INT_MAX;

int get_ind(char c) {
    return (int)((int)c - (int)'a');
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    string s;
    cin >> s;
    map<char, vector<int>> b;
    for (int i = 0; i < s.size(); i++) {
        b[s[i]].push_back(i);
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        string buf;
        cin >> buf;
        int max = -1;
        std::vector<int> shift(26, 0);
        for (char c : buf) {
            bool ch = true;
            if (b.count(c) != 0) {
                if (shift[get_ind(c)] >= (int)b[c].size()) {
                    // nothing
                } else {
                    ch = false;
                    max = std::max(max, b[c][shift[get_ind(c)]]);
                    shift[get_ind(c)]++;
                }
            }
            if (ch) {
                max = -1;
                break;
            }
        }
        if (max == -1) {
            cout << max << endl;
        } else {
            cout << max + 1 << endl;
        }
    }
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

const int inf = INT_MAX;

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    string s;
    cin >> s;
    map<char, vector<int>> b;
    for (int i = 0; i < s.size(); i++) {
        b[s[i]].push_back(i);
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        string buf;
        cin >> buf;
        int max = -1;
        std::map<char, int> shift;
        for(char c = 'a'; c <= 'z'; c++) {
            shift[c] = 0;
        }
        for (char c : buf) {
            bool ch = true;
            if (b.count(c) != 0) {
                if (shift[c] >= (int)b[c].size()) {
                    //
                } else {
                    ch = false;
                    max = std::max(max, b[c][shift[c]]);
                    shift[c]++;
                }
            }
            if (ch) {
                max = -1;
                break;
            }
        }
        if (max == -1) {
            cout << max << endl;
        } else {
            cout << max + 1 << endl;
        }
    }
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define ll long long
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define all(a) a.begin(), a.end()
#define PI 3.141592653589793238462643383279502884197169399375105820974

typedef vector <pair <int ,int > > vpii;
typedef vector <pair <ll ,ll > > vpll;
typedef vector <pair <double ,double > > vpdd;
typedef vector<int> vi;
typedef vector <ll> vl;
typedef vector <char> vc;
typedef vector <double> vd;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
double f(double x, double y, double x1, double y1){
  return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
}

const int inf = INT_MAX;
int main() {
 //freopen("input.txt", "r", stdin);
 //freopen("output.txt", "w", stdout);
 ios_base::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
 int n;
 cin >> n;
 string s;
 cin >> s;
 unordered_map <char, vector<int>> b;
 for(int i = 0; i < s.size(); i++) {
     b[s[i]].push_back(i);
 }
 int m;
 cin >> m;
 for(int i = 0; i < m; i++) {
   string buf;
   cin >> buf;
   int max = -1;
   unordered_map<char, vector<int>> tmp = b;
   for(int j = 0; j < buf.size(); j++){
        bool ch = true;
        if (tmp.count(buf[j]) != 0) {
            for(int k = 0; k < tmp[buf[j]].size(); k++) {
                if (tmp[buf[j]][k] != inf) {
                    ch = false;
                    max = std::max(max, tmp[buf[j]][k]);
                    tmp[buf[j]][k] = inf;
                    break;
                }
            }
        }
        if (ch) {
            cout << -1 << endl;
            break;
        }
    }
    cout << max + 1 << '\n';
    }
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define ll long long
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define all(a) a.begin(), a.end()
#define PI 3.141592653589793238462643383279502884197169399375105820974

typedef vector <pair <int ,int > > vpii;
typedef vector <pair <ll ,ll > > vpll;
typedef vector <pair <double ,double > > vpdd;
typedef vector<int> vi;
typedef vector <ll> vl;
typedef vector <char> vc;
typedef vector <double> vd;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
double f(double x, double y, double x1, double y1){
  return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
}
int main() {
 //freopen("input.txt", "r", stdin);
 //freopen("output.txt", "w", stdout);
 ios_base::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
 int n;
 cin >> n;
 string s;
 cin >> s;
 map <char, int> b;
 for(int i = 0; i < s.size();i++){
   if(b.find(s[i]) == b.end()){
     b[s[i]] = i + 1;
   }
 }
 int m;
 cin >> m;
 for(int i = 0; i < m; i++){
   string buf;
   cin >> buf;
   int max = -1;
   for(int j = 0; j < buf.size(); j++){
     if(b[buf[j]] > max){
     max = b[buf[j]];
   }
   }
   cout << max << endl;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int  n;
    cin>>n;
    string s;
    cin>>s;
    vector<int>pos[130];
    int h;
    for(int i=0;i<n;i++)
    {
        h=(int)s[i];
        pos[h].push_back(i+1);
    }
    int m;
    cin>>m;
    while(m--)
    {
        string t;
        cin>>t;
        int cnt[130]={0},k;
        for(int i=0;i<t.size();i++)
        {
          k=(int)t[i];
          cnt[k]++;
        }
        int ans=-1;
        for(int i=97;i<=122;i++)
        {
            if(cnt[i]>0)
            {
                ans=max(ans,pos[i][cnt[i]-1]);
            }
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define end "\n"
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        int x,m=INT_MIN,flag=1;
        string d;
        cin>>d;
        int c[130]={0},k,in,q;
        int z[sizeof(c)]={0};

        for(int i=0;i<d.size();i++)
        {
            k=(int)d[i];
            c[k]++;
            q=(int)s[i];
            if(c[q]>0)c[q]--;
          if(memcmp(c,z,sizeof(c))==0){flag=0;cout<<i+1<<endl;break;}

        }
        if(flag==0)continue;
        else
        {
            for(int i=d.size();i<n;i++)
            {
              k=(int)s[i];
              if(c[k]>0)c[k]--;
              if(memcmp(c,z,sizeof(c))==0){in=i;break;}
            }
        cout<<"helloo"<<in+1<<endl;

        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        string d;
        int k;
        cin>>d;
        if(d.size()==n){cout<<n<<endl;continue;}
        int f[130]={0},g[130]={0};
        for(int i=0;i<d.size();i++)
        {
           k=int(d[i]);
           f[k]++;
        }
        int flag=1,in;
        for(int i=0;i<d.size();i++)
        {
            k=int(s[i]);
            g[k]++;
            flag=1;
            for(int j=0;j<d.size();j++)
            {
              int q=(int)d[j];
              if(g[q]<f[q]){flag=0;break;}
            }
            if(flag==1){in=i;break;}

        }
        if(flag==0)
        {
            for(int i=d.size();i<n;i++)
            {
                k=(int)s[i];
                g[k]++;
                flag=1;
                for(int j=0;j<d.size();j++)
                {
                int q=(int)d[j];
                if(g[q]<f[q]){flag=0;break;}
                }
            if(flag==1){in=i;break;}

            }
            cout<<in+1<<endl;

        }
        else cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        string d;
        int k;
        cin>>d;
        set<char>x;
        for(int i=0;i<d.size();i++)
        {
            x.insert(d[i]);
        }
        int f[130]={0},g[130]={0};
        for(int i=0;i<d.size();i++)
        {
           k=int(d[i]);
           f[k]++;
        }
        int flag=1,in;
        set<char>::iterator j;
        for(int i=0;i<n;i++)
        {
            k=int(s[i]);
            g[k]++;
            flag=1;
            for(j=x.begin();j!=x.end();j++)
            {
              int q=(int)*j;
              if(g[q]<f[q]){flag=0;break;}
            }
            if(flag==1){in=i;break;}

        }
        cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #define int long long int
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    for(int b=0;b<t;b++)
    {
        int x,m=INT_MIN;
        string d;
        cin>>d;
        int c[130]={0},k,in;
        for(int i=0;i<d.size();i++)
        {
            k=(int)d[i];
            c[k]++;
        }
        int z[sizeof(c)]={0};

        for(int i=0;i<n;i++)
        {
             k=(int)s[i];
             if(c[k]>0)c[k]--;
           if(memcmp(c,z,sizeof(c))==0){in=i;break;}
        }
        cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        int x,m=INT_MIN;
        string d;
        cin>>d;
        int c[130]={0},k,in;
        for(int i=0;i<d.size();i++)
        {
            k=(int)d[i];
            c[k]++;
        }
        int z[sizeof(c)]={0};

        for(int i=0;i<n;i++)
        {
             k=(int)s[i];
             if(c[k]>0)c[k]--;
           if(memcmp(c,z,sizeof(c))==0){in=i;break;}
        }
        cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        string d;
        int k;
        cin>>d;
        int f[130]={0},g[130]={0};
        for(int i=0;i<d.size();i++)
        {
           k=int(d[i]);
           f[k]++;
        }
        int flag=1,in;
        for(int i=0;i<d.size();i++)
        {
            k=int(s[i]);
            g[k]++;
            flag=1;
            for(int j=0;j<d.size();j++)
            {
              int q=(int)d[j];
              if(g[q]<f[q]){flag=0;break;}
            }
            if(flag==1){in=i;break;}

        }
        if(flag==0)
        {
            for(int i=d.size();i<n;i++)
            {
                k=(int)s[i];
                g[k]++;
                flag=1;
                for(int j=0;j<d.size();j++)
                {
                int q=(int)d[j];
                if(g[q]<f[q]){flag=0;break;}
                }
            if(flag==1){in=i;break;}

            }
            cout<<in+1<<endl;

        }
        else cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        int x,m=INT_MIN;
        string d;
        cin>>d;
        int c[130]={0},k,in;
        for(int i=0;i<d.size();i++)
        {
            k=(int)d[i];
            c[k]++;
        }
        int z[sizeof(c)]={0};

        for(int i=0;i<n;i++)
        {
             k=(int)s[i];
             if(c[k]>0)c[k]--;
           if(memcmp(c,z,sizeof(c))==0){in=i;break;}
        }
        cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #define int long long int
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        int x,m=INT_MIN;
        string d;
        cin>>d;
        int c[130]={0},k,in;
        for(int i=0;i<d.size();i++)
        {
            k=(int)d[i];
            c[k]++;
        }
        int z[sizeof(c)]={0};

        for(int i=0;i<n;i++)
        {
             k=(int)s[i];
             if(c[k]>0)c[k]--;
           if(memcmp(c,z,sizeof(c))==0){in=i;break;}
        }
        cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define endl "\n"
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        string d;
        int k;
        cin>>d;
        if(d.size()==n){cout<<n<<endl;continue;}
        int f[130]={0},g[130]={0};
        for(int i=0;i<d.size();i++)
        {
           k=int(d[i]);
           f[k]++;
        }
        for(int i=0;i<d.size();i++)
        {
            k=int(s[i]);
            g[k]++;
        }
        string z;
        int flag=1;

        for(int i=0;i<d.size();i++)
        {
            k=int(d[i]);
            if(g[k]<f[k]){flag=0;z+=d[i];}
        }
        if(flag==1){cout<<d.size()<<endl;continue;}
            flag=1;
            int in=d.size();

            for(int i=d.size();i<n;i++)
            {
                k=(int)s[i];
                g[k]++;
                flag=1;
                for(int j=0;j<z.size();j++)
                {
                int q=(int)z[j];
                if(g[q]<f[q]){flag=0;break;}
                }
            if(flag==1){in=i;break;}

            }
            cout<<in+1<<endl;

    }

    return 0;
}