import java.awt.Point;
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

public class R108_D2_B {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        // BufferedReader in = new BufferedReader(new
        // InputStreamReader(System.in));
        int n = in.readInt();
        int m = in.readInt();
        int xc = in.readInt();
        int yc = in.readInt();
        int k = in.readInt();
        Point[] inp = new Point[k];
        for (int i = 0; i < k; i++) {
            inp[i] = new Point(in.readInt(), in.readInt());
        }
        long c = 0;
        for (int i = 0; i < k; i++) {
            int dx = inp[i].x;
            int dy = inp[i].y;
            int rightx = n - xc;
            int leftx = xc - 1;
            int upy = m - yc;
            int downy = yc - 1;
            int maxX = -1;
            if (dx > 0)
                maxX = rightx / dx;
            else if (dx < 0)
                maxX = leftx / (-1 * dx);
            int maxY = -1;
            if (dy > 0)
                maxY = upy / dy;
            else if (dy < 0)
                maxY = downy / (-1 * dy);
            int steps = 0;
            if (maxX >= 0 && maxY >= 0)
                steps = Math.min(maxX, maxY);
            else if (maxX >= 0)
                steps = maxX;
            else
                steps = maxY;
            c += steps;
            if (maxX >= 0)
                xc += (steps * dx);
            if (maxY >= 0)
                yc += (steps * dy);
        }
        System.out.println(c);

    }

    static class InputReader4 {
        private InputStream stream;
        private byte[] buf = new byte[1000];
        private int curChar, numChars;

        public InputReader4(InputStream stream) {
            this.stream = stream;
        }

        private int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private String readLine0() {
            StringBuffer buf = new StringBuffer();
            int c = read();
            while (c != '\n' && c != -1) {
                buf.appendCodePoint(c);
                c = read();
            }
            return buf.toString();
        }

        public String readLine() {
            String s = readLine0();
            while (s.trim().length() == 0)
                s = readLine0();
            return s;
        }

        public String readLine(boolean ignoreEmptyLines) {
            if (ignoreEmptyLines)
                return readLine();
            else
                return readLine0();
        }

        public char readCharacter() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            return (char) c;
        }

        public double readDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, readInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, readInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }
    }

}


import java.awt.Point;
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

public class R108_D2_B {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        // BufferedReader in = new BufferedReader(new
        // InputStreamReader(System.in));
        int n = in.readInt();
        int m = in.readInt();
        int xc = in.readInt();
        int yc = in.readInt();
        int k = in.readInt();
        Point[] inp = new Point[k];
        for (int i = 0; i < k; i++) {
            inp[i] = new Point(in.readInt(), in.readInt());
        }
        long c = 0;
        for (int i = 0; i < k; i++) {
            int dx = inp[i].x;
            int dy = inp[i].y;
            int rightx = n - xc;
            int leftx = xc - 1;
            int upy = m - yc;
            int downy = yc - 1;
            int maxX = -1;
            if (dx > 0)
                maxX = rightx / dx;
            else if (dx < 0)
                maxX = leftx / (-1 * dx);
            int maxY = -1;
            if (dy > 0)
                maxY = upy / dy;
            else if (dy < 0)
                maxY = downy / (-1 * dy);
            int steps = 0;
            if (maxX >= 0 && maxY >= 0)
                steps = Math.min(maxX, maxY);
            else if (maxX >= 0)
                steps = maxX;
            else
                steps = maxY;
            c += steps;
            if (maxX >= 0)
                xc += (steps * dx);
            if (maxY >= 0)
                yc += (steps * dy);
        }
        System.out.println(c);

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1000];
        private int curChar, numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private String readLine0() {
            StringBuffer buf = new StringBuffer();
            int c = read();
            while (c != '\n' && c != -1) {
                buf.appendCodePoint(c);
                c = read();
            }
            return buf.toString();
        }

        public String readLine() {
            String s = readLine0();
            while (s.trim().length() == 0)
                s = readLine0();
            return s;
        }

        public String readLine(boolean ignoreEmptyLines) {
            if (ignoreEmptyLines)
                return readLine();
            else
                return readLine0();
        }

        public char readCharacter() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            return (char) c;
        }

        public double readDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, readInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, readInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }
    }

}


import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;

public class B {

	private void processInput() throws IOException {
		Scanner in = new Scanner(System.in);

		int n = in.nextInt();
		int m = in.nextInt();
		int x = in.nextInt();
		int y = in.nextInt();
		int k = in.nextInt();
		int[][] a = new int[k][2];
		for (int i = 0; i < k; i++) {
			a[i][0] = in.nextInt();
			a[i][1] = in.nextInt();
		}

		long res = go(n, m, x, y, k, a);

		System.out.printf(Locale.ENGLISH, "%d\n", res);

		in.close();
	}

	private long go(int n, int m, int x, int y, int k, int[][] a) {

		long res = 0;

		for (int i = 0; i < k; i++) {

			int sx = Integer.MAX_VALUE;
			int sy = Integer.MAX_VALUE;
			if (a[i][0] < 0) {
				sx = Math.min(sx, Math.abs((x - 1) / a[i][0]));
			} else if (a[i][0] > 0) {
				sx = Math.min(sx, Math.abs((n - x) / a[i][0]));
			}
			if (a[i][1] < 0) {
				sy = Math.min(sy, Math.abs((y - 1) / a[i][1]));
			} else if (a[i][1] > 0) {
				sy = Math.min(sy, Math.abs((m - y) / a[i][1]));
			}
			int s = Math.min(sx, sy);
			x += a[i][0] * s;
			y += a[i][1] * s;
			res += s;
		}

		return res;
	}

	public static void main(String[] args) throws Exception {
		B a = new B();
		a.processInput();
	}
}


import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;

public class B {

	private void processInput() throws IOException {
		Scanner in = new Scanner(System.in);

		int n = in.nextInt();
		int m = in.nextInt();
		int x = in.nextInt();
		int y = in.nextInt();
		int k = in.nextInt();
		int[][] a = new int[k][2];
		for (int i = 0; i < k; i++) {
			a[i][0] = in.nextInt();
			a[i][1] = in.nextInt();
		}

		long res = go(n, m, x, y, k, a);

		System.out.printf(Locale.ENGLISH, "%d\n", res);

		in.close();
	}

	private long go(int n, int m, int x, int y, int k, int[][] a) {

		long res = 0;

		for (int i = 0; i < k; i++) {

			int sx = Integer.MAX_VALUE;
			int sy = Integer.MAX_VALUE;
			if (a[i][0] < 0) {
				sx = Math.min(sx, Math.abs((x - 1) / a[i][0]));
			} else if (a[i][0] > 0) {
				sx = Math.min(sx, Math.abs((n - x) / a[i][0]));
			}
			if (a[i][1] < 0) {
				sy = Math.min(sy, Math.abs((y - 1) / a[i][1]));
			} else if (a[i][1] > 0) {
				sy = Math.min(sy, Math.abs((n - y) / a[i][1]));
			}
			int s = Math.min(sx, sy);
			x += a[i][0] * s;
			y += a[i][1] * s;
			res += s;
		}

		return res;
	}

	public static void main(String[] args) throws Exception {
		B a = new B();
		a.processInput();
	}
}


import java.io.*;
import java.util.*;

public class Main {
    public void solve( ) throws Throwable {
        long r = in.nextInt( ), c = in.nextInt( );
        long x = in.nextInt( ), y = in.nextInt( );
        long steps = in.nextInt( );
        long ans = 0;
        for ( int i = 0; i < steps; ++i ) {
            long dx = in.nextInt( ), dy = in.nextInt( ), xs = 0, ys = 0;
            if ( dx == 0 ) {
                 if ( dy < 0 ) {
                     ys = ( y - 1 ) / dy;
                 } else {
                     ys = ( c - y ) / dy;
                 }
            } else if ( dy == 0 ) {
                if ( dx < 0 ) {
                    xs = ( x - 1 ) / dx;
                }
                else {
                    xs = ( r - x ) / dx;
                }
            } else {
                if ( dx > 0 && dy > 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( c - y ) / dy;

                } else if ( dx < 0 && dy > 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( c - y ) / dy;
                } else if ( dx > 0 && dy < 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( y - 1 ) / dy;
                } else if ( dx < 0 && dy < 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( y - 1 ) / dy;
                }
            }
            xs = Math.abs( xs );
            ys = Math.abs( ys );

            long jump = 0;
            if ( dx == 0 ) {
                jump = ys;
            } else if ( dy == 0 ) {
                jump = xs;
            } else {
                jump = Math.min( xs, ys );
            }

            if ( jump == 0 ) {
                continue;
            }

            long newx = x + dx * jump, newy = y + dy * jump;
            //debug( xs, ys, newx, newy, jump );
            if ( newx > 0 && newx <= r && newy > 0 && newy <= c ) {
                ans += jump;
                x = newx;
                y = newy;
            }

        }
        out.println( ans );
    }

    public void run( ) {
        in = new FastScanner( System.in );
        out = new PrintWriter( new PrintStream( System.out ), true );

        try {
            solve( );
            out.close( );
            System.exit( 0 );
        } catch( Throwable e ) {
            e.printStackTrace( );
            System.exit( -1 );
        }
    }

    public void debug( Object...os ) {
        System.err.println( Arrays.deepToString( os ) );
    }

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

    private FastScanner in;
    private PrintWriter out;

    private static class FastScanner {
        private int charsRead;
        private int currentRead;
        private byte buffer[ ] = new byte[ 0x1000 ];
        private InputStream reader;

        public FastScanner( InputStream in ) {
            reader = in;
        }

        public int read( ) {
            if ( charsRead == -1 ) {
                throw  new InputMismatchException( );
            }

            if ( currentRead >= charsRead ) {
                currentRead = 0;
                try {
                    charsRead = reader.read( buffer );

                } catch( IOException e ) {
                    throw new InputMismatchException(  );
                }

                if ( charsRead <= 0 ) {
                    return -1;
                }
            }

            return buffer[ currentRead++ ];
        }

        public int nextInt( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public long nextLong( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            long ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public boolean isWhitespace( int c ) {
            return  c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
        }

        public String next( ) {
            int c = read( );
            StringBuffer ans = new StringBuffer( );

            while ( isWhitespace( c ) && c != -1 ) {
                c  = read( );
            }

            if ( c == -1 ) {
                return null;
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public String nextLine( ) {
            String ans = nextLine0( );

            while ( ans.trim( ).length( ) == 0 ) {
                ans = nextLine0( );
            }

            return ans;
        }

        private String nextLine0( ) {
            int c = read( );
            if ( c == -1 ) {
                return null;
            }
            StringBuffer ans = new StringBuffer( );
            while ( c != '\n' && c != '\r' && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public double nextDouble( ) {
            int c = read( );
            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( c == '-' ) {
                c = read( );
            }
            double ans = 0;
            while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }

                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
                throw new InputMismatchException( );
            }

            double pow10 = 1.0;

            if ( c == '.' ) {
                c = read( );
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                pow10 *= 10.0;

                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            return ans * sign / pow10;
        }
    }

}


import java.io.*;
import java.util.*;

public class Main {
    public void solve( ) throws Throwable {
        int r = in.nextInt( ), c = in.nextInt( );
        int x = in.nextInt( ), y = in.nextInt( );
        int steps = in.nextInt( );
        int ans = 0;
        for ( int i = 0; i < steps; ++i ) {
            int dx = in.nextInt( ), dy = in.nextInt( ), xs = 0, ys = 0;
            if ( dx == 0 ) {
                 if ( dy < 0 ) {
                     ys = ( y - 1 ) / dy;
                 } else {
                     ys = ( c - y ) / dy;
                 }
            } else if ( dy == 0 ) {
                if ( dx < 0 ) {
                    xs = ( x - 1 ) / dx;
                }
                else {
                    xs = ( r - x ) / dx;
                }
            } else {
                if ( dx > 0 && dy > 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( c - y ) / dy;

                } else if ( dx < 0 && dy > 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( c - y ) / dy;
                } else if ( dx > 0 && dy < 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( y - 1 ) / dy;
                } else if ( dx < 0 && dy < 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( y - 1 ) / dy;
                }
            }
            xs = Math.abs( xs );
            ys = Math.abs( ys );

            int jump = 0;
            if ( xs == 0 ) {
                jump = ys;
            } else if ( ys == 0 ) {
                jump = xs;
            } else {
                jump = Math.min( xs, ys );
            }

            if ( jump == 0 ) {
                continue;
            }

            int newx = x + dx * jump, newy = y + dy * jump;
            //debug( xs, ys, newx, newy );
            if ( newx > 0 && newx <= r && newy > 0 && newy <= c ) {
                if ( xs == 0 ) {
                    ans += ys;

                } else if ( ys == 0 ) {
                    ans += xs;
                } else {
                    ans += Math.min( xs, ys );
                }
                x = newx;
                y = newy;
            }

        }
        out.println( ans );
    }

    public void run( ) {
        in = new FastScanner( System.in );
        out = new PrintWriter( new PrintStream( System.out ), true );

        try {
            solve( );
            out.close( );
            System.exit( 0 );
        } catch( Throwable e ) {
            e.printStackTrace( );
            System.exit( -1 );
        }
    }

    public void debug( Object...os ) {
        System.err.println( Arrays.deepToString( os ) );
    }

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

    private FastScanner in;
    private PrintWriter out;

    private static class FastScanner {
        private int charsRead;
        private int currentRead;
        private byte buffer[ ] = new byte[ 0x1000 ];
        private InputStream reader;

        public FastScanner( InputStream in ) {
            reader = in;
        }

        public int read( ) {
            if ( charsRead == -1 ) {
                throw  new InputMismatchException( );
            }

            if ( currentRead >= charsRead ) {
                currentRead = 0;
                try {
                    charsRead = reader.read( buffer );

                } catch( IOException e ) {
                    throw new InputMismatchException(  );
                }

                if ( charsRead <= 0 ) {
                    return -1;
                }
            }

            return buffer[ currentRead++ ];
        }

        public int nextInt( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public long nextLong( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            long ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public boolean isWhitespace( int c ) {
            return  c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
        }

        public String next( ) {
            int c = read( );
            StringBuffer ans = new StringBuffer( );

            while ( isWhitespace( c ) && c != -1 ) {
                c  = read( );
            }

            if ( c == -1 ) {
                return null;
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public String nextLine( ) {
            String ans = nextLine0( );

            while ( ans.trim( ).length( ) == 0 ) {
                ans = nextLine0( );
            }

            return ans;
        }

        private String nextLine0( ) {
            int c = read( );
            if ( c == -1 ) {
                return null;
            }
            StringBuffer ans = new StringBuffer( );
            while ( c != '\n' && c != '\r' && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public double nextDouble( ) {
            int c = read( );
            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( c == '-' ) {
                c = read( );
            }
            double ans = 0;
            while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }

                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
                throw new InputMismatchException( );
            }

            double pow10 = 1.0;

            if ( c == '.' ) {
                c = read( );
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                pow10 *= 10.0;

                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            return ans * sign / pow10;
        }
    }

}


import java.io.*;
import java.util.*;

public class Main {
    public void solve( ) throws Throwable {
        int r = in.nextInt( ), c = in.nextInt( );
        int x = in.nextInt( ), y = in.nextInt( );
        int steps = in.nextInt( );
        int ans = 0;
        for ( int i = 0; i < steps; ++i ) {
            int dx = in.nextInt( ), dy = in.nextInt( ), xs = 0, ys = 0;
            if ( dx == 0 ) {
                 if ( dy < 0 ) {
                     ys = ( y - 1 ) / dy;
                 } else {
                     ys = ( c - y ) / dy;
                 }
            } else if ( dy == 0 ) {
                if ( dx < 0 ) {
                    xs = ( x - 1 ) / dx;
                }
                else {
                    xs = ( r - x ) / dx;
                }
            } else {
                if ( dx > 0 && dy > 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( c - y ) / dy;

                } else if ( dx < 0 && dy > 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( c - y ) / dy;
                } else if ( dx > 0 && dy < 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( y - 1 ) / dy;
                } else if ( dx < 0 && dy < 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( y - 1 ) / dy;
                }
            }
            xs = Math.abs( xs );
            ys = Math.abs( ys );

            int jump = 0;
            if ( dx == 0 ) {
                jump = ys;
            } else if ( dy == 0 ) {
                jump = xs;
            } else {
                jump = Math.min( xs, ys );
            }

            if ( jump == 0 ) {
                continue;
            }

            int newx = x + dx * jump, newy = y + dy * jump;
            //debug( xs, ys, newx, newy );
            if ( newx > 0 && newx <= r && newy > 0 && newy <= c ) {
                ans += jump;
                x = newx;
                y = newy;
            }

        }
        out.println( ans );
    }

    public void run( ) {
        in = new FastScanner( System.in );
        out = new PrintWriter( new PrintStream( System.out ), true );

        try {
            solve( );
            out.close( );
            System.exit( 0 );
        } catch( Throwable e ) {
            e.printStackTrace( );
            System.exit( -1 );
        }
    }

    public void debug( Object...os ) {
        System.err.println( Arrays.deepToString( os ) );
    }

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

    private FastScanner in;
    private PrintWriter out;

    private static class FastScanner {
        private int charsRead;
        private int currentRead;
        private byte buffer[ ] = new byte[ 0x1000 ];
        private InputStream reader;

        public FastScanner( InputStream in ) {
            reader = in;
        }

        public int read( ) {
            if ( charsRead == -1 ) {
                throw  new InputMismatchException( );
            }

            if ( currentRead >= charsRead ) {
                currentRead = 0;
                try {
                    charsRead = reader.read( buffer );

                } catch( IOException e ) {
                    throw new InputMismatchException(  );
                }

                if ( charsRead <= 0 ) {
                    return -1;
                }
            }

            return buffer[ currentRead++ ];
        }

        public int nextInt( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public long nextLong( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            long ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public boolean isWhitespace( int c ) {
            return  c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
        }

        public String next( ) {
            int c = read( );
            StringBuffer ans = new StringBuffer( );

            while ( isWhitespace( c ) && c != -1 ) {
                c  = read( );
            }

            if ( c == -1 ) {
                return null;
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public String nextLine( ) {
            String ans = nextLine0( );

            while ( ans.trim( ).length( ) == 0 ) {
                ans = nextLine0( );
            }

            return ans;
        }

        private String nextLine0( ) {
            int c = read( );
            if ( c == -1 ) {
                return null;
            }
            StringBuffer ans = new StringBuffer( );
            while ( c != '\n' && c != '\r' && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public double nextDouble( ) {
            int c = read( );
            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( c == '-' ) {
                c = read( );
            }
            double ans = 0;
            while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }

                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
                throw new InputMismatchException( );
            }

            double pow10 = 1.0;

            if ( c == '.' ) {
                c = read( );
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                pow10 *= 10.0;

                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            return ans * sign / pow10;
        }
    }

}


import java.io.*;
import java.util.*;

public class Main {
    public void solve( ) throws Throwable {
        int r = in.nextInt( ), c = in.nextInt( );
        int x = in.nextInt( ), y = in.nextInt( );
        int steps = in.nextInt( );
        int ans = 0;
        for ( int i = 0; i < steps; ++i ) {
            int dx = in.nextInt( ), dy = in.nextInt( ), xs = 0, ys = 0;
            if ( dx == 0 ) {
                 if ( dy < 0 ) {
                     ys = ( y - 1 ) / dy;
                 } else {
                     ys = ( c - y ) / dy;
                 }
            } else if ( dy == 0 ) {
                if ( dx < 0 ) {
                    xs = ( x - 1 ) / dx;
                }
                else {
                    xs = ( r - x ) / dx;
                }
            } else {
                if ( dx > 0 && dy > 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( c - y ) / dy;

                } else if ( dx < 0 && dy > 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( c - y ) / dy;
                } else if ( dx > 0 && dy < 0 ) {
                    xs = ( r - x ) / dx;
                    ys = ( y - 1 ) / dy;
                } else if ( dx < 0 && dy < 0 ) {
                    xs = ( x - 1 ) / dx;
                    ys = ( y - 1 ) / dy;
                }
            }
            xs = Math.abs( xs );
            ys = Math.abs( ys );

            int jump = 0;
            if ( dx == 0 ) {
                jump = ys;
            } else if ( dy == 0 ) {
                jump = xs;
            } else {
                jump = Math.min( xs, ys );
            }

            if ( jump == 0 ) {
                continue;
            }

            int newx = x + dx * jump, newy = y + dy * jump;
            //debug( xs, ys, newx, newy );
            if ( newx > 0 && newx <= r && newy > 0 && newy <= c ) {
                if ( xs == 0 ) {
                    ans += ys;

                } else if ( ys == 0 ) {
                    ans += xs;
                } else {
                    ans += Math.min( xs, ys );
                }
                x = newx;
                y = newy;
            }

        }
        out.println( ans );
    }

    public void run( ) {
        in = new FastScanner( System.in );
        out = new PrintWriter( new PrintStream( System.out ), true );

        try {
            solve( );
            out.close( );
            System.exit( 0 );
        } catch( Throwable e ) {
            e.printStackTrace( );
            System.exit( -1 );
        }
    }

    public void debug( Object...os ) {
        System.err.println( Arrays.deepToString( os ) );
    }

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

    private FastScanner in;
    private PrintWriter out;

    private static class FastScanner {
        private int charsRead;
        private int currentRead;
        private byte buffer[ ] = new byte[ 0x1000 ];
        private InputStream reader;

        public FastScanner( InputStream in ) {
            reader = in;
        }

        public int read( ) {
            if ( charsRead == -1 ) {
                throw  new InputMismatchException( );
            }

            if ( currentRead >= charsRead ) {
                currentRead = 0;
                try {
                    charsRead = reader.read( buffer );

                } catch( IOException e ) {
                    throw new InputMismatchException(  );
                }

                if ( charsRead <= 0 ) {
                    return -1;
                }
            }

            return buffer[ currentRead++ ];
        }

        public int nextInt( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public long nextLong( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            long ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public boolean isWhitespace( int c ) {
            return  c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
        }

        public String next( ) {
            int c = read( );
            StringBuffer ans = new StringBuffer( );

            while ( isWhitespace( c ) && c != -1 ) {
                c  = read( );
            }

            if ( c == -1 ) {
                return null;
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public String nextLine( ) {
            String ans = nextLine0( );

            while ( ans.trim( ).length( ) == 0 ) {
                ans = nextLine0( );
            }

            return ans;
        }

        private String nextLine0( ) {
            int c = read( );
            if ( c == -1 ) {
                return null;
            }
            StringBuffer ans = new StringBuffer( );
            while ( c != '\n' && c != '\r' && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

        public double nextDouble( ) {
            int c = read( );
            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( c == '-' ) {
                c = read( );
            }
            double ans = 0;
            while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }

                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
                throw new InputMismatchException( );
            }

            double pow10 = 1.0;

            if ( c == '.' ) {
                c = read( );
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                pow10 *= 10.0;

                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10.0 + num;
                c = read( );
            }

            return ans * sign / pow10;
        }
    }

}


import java.io.PrintStream;
import java.util.Scanner;

public class Main {

	public static void main( String[] args ) {
    	new CodeForces_172A().doit();
    }
}
class CodeForces_172A {

	Scanner sc;
	PrintStream ps;

	public void doit() {
		sc = new Scanner( System.in );
		ps = System.out;
		int n = sc.nextInt();
		int m = sc.nextInt();
		int cx = sc.nextInt();
		int cy = sc.nextInt();
		int k = sc.nextInt();
		long ans = 0;
		for ( int i = 0; i < k; i++ ) {
			int dx = sc.nextInt();
			int dy = sc.nextInt();
			int d1 = dx > 0 ? (n - cx) / dx : ((dx < 0) ? (cx - 1) / (-dx) : 0);
			int d2 = dy > 0 ? (m - cy) / dy : ((dy < 0) ? ( cy - 1) / (-dy) : 0);
			int d = 0;
			if ( dx == 0 && dy != 0 ) d = d2;
			else if ( dx != 0 && dy == 0 ) d = d1;
			else d = Math.min( d1, d2 );
			ans += d;
			cx += d * dx;
			cy += d * dy;
		}
		ps.println( ans );
	}
}


import java.io.PrintStream;
import java.util.Scanner;

public class Main {

	public static void main( String[] args ) {
    	new CodeForces_172A().doit();
    }
}
class CodeForces_172A {

	Scanner sc;
	PrintStream ps;

	public void doit() {
		sc = new Scanner( System.in );
		ps = System.out;
		int n = sc.nextInt();
		int m = sc.nextInt();
		int cx = sc.nextInt();
		int cy = sc.nextInt();
		int k = sc.nextInt();
		long ans = 0;
		for ( int i = 0; i < k; i++ ) {
			int dx = sc.nextInt();
			int dy = sc.nextInt();
			int d1 = dx > 0 ? (n - cx) / dx : ((dx < 0) ? (cx - 1) / (-dx) : 0);
			int d2 = dy > 0 ? (m - cy) / dy : ((dy < 0) ? ( cy - 1) / (-dy) : 0);
			int d = 0;
			if ( dx == 0 && dy != 0 ) d = d2;
			else if ( dx != 0 && dy == 0 ) d = d1;
			else d = Math.min( d1, d2 );
			ans += d;
			cx += (dx > 0 ? d : -d) * dx;
			cy += (dy > 0 ? d : -d) * dy;
		}
		ps.println( ans );
	}
}


import java.io.*;
import java.util.*;
import java.math.*;

public class Main
{
	public static void main ( String[] args )
	{
		Scanner in = new Scanner (System.in);
		PrintWriter out = new PrintWriter (System.out);
		int n=in.nextInt(),m=in.nextInt();
		int x=in.nextInt(),y=in.nextInt();
		int k=in.nextInt();
		long cnt=0;
		int INF=1000*1000*1000;
		for (int i=0;i<k;++i)
		{
			int dx=in.nextInt(),dy=in.nextInt();
			int step=INF;
			if (dx!=0) step=Math.min(step,Math.abs(dx>0 ? (n-x)/dx : (x-1)/dx));
			if (dy!=0) step=Math.min(step,Math.abs(dy>0 ? (m-y)/dy : (y-1)/dy));
			cnt+=step;
			x+=dx*step;
			y+=dy*step;
		}
		out.print(cnt);
		out.flush();
	}
}


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

public class Main {
   public static void main(String[] args) throws IOException
   {
	   (new Main()).solve();
   }
   public void Main() {
   }

   void solve()  throws IOException {
	  //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	   Scanner in = new Scanner(System.in);
	  PrintWriter out = new PrintWriter(System.out);
     // Scanner in = new Scanner(new FileReader("input.txt"));
	//  PrintWriter out = new PrintWriter("output.txt");
	int n  = in.nextInt();
	int m = in.nextInt();
	int xc = in.nextInt();
	int yc = in.nextInt();
	int k = in.nextInt();
	long count = 0;

	for (int i = 0; i < k; i++) {
		int xd = in.nextInt();
		int yd = in.nextInt();
		int countx, county;
		if (xd > 0) {
			countx = (n - xc) / xd;
		} else if (xd < 0) {
			countx = (xc - 1) / (-xd);
		} else {
			countx = n - 1;
		}
		if (yd > 0) {
			county = (m - yc) / yd;
		} else if (yd < 0) {
			county = (yc - 1) / (-yd);
		} else {
			county = m - 1;
		}
		int count1 = Math.min(countx, county);
		count += count1;
		xc += (count1 * xd);
		yc += (count1 * yd);
	}

	out.print(count);

	 out.close();
   }

};


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

public class Main {
   public static void main(String[] args) throws IOException
   {
	   (new Main()).solve();
   }
   public void Main() {
   }

   void solve()  throws IOException {
	  //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	   Scanner in = new Scanner(System.in);
	  PrintWriter out = new PrintWriter(System.out);
     // Scanner in = new Scanner(new FileReader("input.txt"));
	//  PrintWriter out = new PrintWriter("output.txt");
	int n  = in.nextInt();
	int m = in.nextInt();
	int xc = in.nextInt();
	int yc = in.nextInt();
	int k = in.nextInt();
	long count = 0;

	for (int i = 0; i < k; i++) {
		int xd = in.nextInt();
		int yd = in.nextInt();
		int countx, county;
		if (xd > 0) {
			countx = (n - xc) / xd;
		} else if (xd < 0) {
			countx = (xc - 1) / (-xd);
		} else {
			countx = Integer.MAX_VALUE;
		}
		if (yd > 0) {
			county = (m - yc) / yd;
		} else if (yd < 0) {
			county = (yc - 1) / (-yd);
		} else {
			county = Integer.MAX_VALUE;
		}
		int count1 = Math.min(countx, county);
		count += count1;
		xc += (count1 * xd);
		yc += (count1 * yd);
	}

	out.print(count);

	 out.close();
   }

};


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.Math.*;

@SuppressWarnings("unused")
public class round108B {
    static BufferedReader br = new BufferedReader(new InputStreamReader(
            System.in));
    static StringTokenizer st = new StringTokenizer("");

    static int nextInt() throws Exception {
        return Integer.parseInt(next());
    }

    static String next() throws Exception {
        while (true) {
            if (st.hasMoreTokens()) {
                return st.nextToken();
            }
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            st = new StringTokenizer(s);
        }
    }

    public static void main(String[] args) throws Exception {
        int n = nextInt();
        int m = nextInt();
        int xc = nextInt();
        int yc = nextInt();
        int k = nextInt();
        int[] dx = new int[k];
        int[] dy = new int[k];
        for (int i = 0; i < k; ++i) {
            dx[i] = nextInt();
            dy[i] = nextInt();
        }
        long ans = 0;
        for (int i = 0; i < k; ++i) {
            if (dx[i] == 0 && dy[i] == 0)
                continue;
            int stepsH = 0;
            int stepsV = 0;
            if (dx[i] == 0) {
                if (dy[i] < 0) {
                    int left = yc - 1;
                    stepsH = abs(dy[i]) == 0 ? 0 : (left / abs(dy[i]));
                } else {
                    int right = m - yc;
                    stepsH = abs(dy[i]) == 0 ? 0 : (right / abs(dy[i]));
                }
                ans += stepsH;
                xc += (dx[i] * stepsH);
                yc += (dy[i] * stepsH);
                continue;
            }
            if (dy[i] == 0) {
                if (dx[i] < 0) {
                    int up = xc - 1;
                    stepsV = abs(dx[i]) == 0 ? 0 : (up / abs(dx[i]));
                } else {
                    int down = n - xc;
                    stepsV = abs(dx[i]) == 0 ? 0 : (down / abs(dx[i]));
                }
                ans += stepsV;
                xc += (dx[i] * stepsV);
                yc += (dy[i] * stepsV);
                continue;
            }
            if (dx[i] < 0) {
                int up = xc - 1;
                stepsV = abs(dx[i]) == 0 ? 0 : (up / abs(dx[i]));
            } else {
                int down = n - xc;
                stepsV = abs(dx[i]) == 0 ? 0 : (down / abs(dx[i]));
            }
            if (dy[i] < 0) {
                int left = yc - 1;
                stepsH = abs(dy[i]) == 0 ? 0 : (left / abs(dy[i]));
            } else {
                int right = m - yc;
                stepsH = abs(dy[i]) == 0 ? 0 : (right / abs(dy[i]));
            }
            if (stepsV < stepsH) {
                ans += stepsV;
                xc += (dx[i] * stepsV);
                yc += (dy[i] * stepsV);
            } else {
                if (stepsH < stepsV) {
                    ans += stepsH;
                    xc += (dx[i] * stepsH);
                    yc += (dy[i] * stepsH);
                }
            }
        }
        System.out.println(ans);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.Math.*;

@SuppressWarnings("unused")
public class round108B {
    static BufferedReader br = new BufferedReader(new InputStreamReader(
            System.in));
    static StringTokenizer st = new StringTokenizer("");

    static int nextInt() throws Exception {
        return Integer.parseInt(next());
    }

    static String next() throws Exception {
        while (true) {
            if (st.hasMoreTokens()) {
                return st.nextToken();
            }
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            st = new StringTokenizer(s);
        }
    }

    public static void main(String[] args) throws Exception {
        int n = nextInt();
        int m = nextInt();
        int xc = nextInt();
        int yc = nextInt();
        int k = nextInt();
        int[] dx = new int[k];
        int[] dy = new int[k];
        for (int i = 0; i < k; ++i) {
            dx[i] = nextInt();
            dy[i] = nextInt();
        }
        long ans = 0;
        for (int i = 0; i < k; ++i) {
            if (dx[i] == 0 && dy[i] == 0)
                continue;
            int stepsH = 0;
            int stepsV = 0;
            if (dx[i] == 0) {
                if (dy[i] < 0) {
                    int left = yc - 1;
                    stepsH = (left / abs(dy[i]));
                } else {
                    int right = m - yc;
                    stepsH = (right / abs(dy[i]));
                }
                ans += stepsH;
                xc += (dx[i] * stepsH);
                yc += (dy[i] * stepsH);
                continue;
            }
            if (dy[i] == 0) {
                if (dx[i] < 0) {
                    int up = xc - 1;
                    stepsV = (up / abs(dx[i]));
                } else {
                    int down = n - xc;
                    stepsV = (down / abs(dx[i]));
                }
                ans += stepsV;
                xc += (dx[i] * stepsV);
                yc += (dy[i] * stepsV);
                continue;
            }
            if (dx[i] < 0) {
                int up = xc - 1;
                stepsV = (up / abs(dx[i]));
            } else {
                int down = n - xc;
                stepsV = (down / abs(dx[i]));
            }
            if (dy[i] < 0) {
                int left = yc - 1;
                stepsH = (left / abs(dy[i]));
            } else {
                int right = m - yc;
                stepsH = (right / abs(dy[i]));
            }
            if (stepsV < stepsH) {
                ans += stepsV;
                xc += (dx[i] * stepsV);
                yc += (dy[i] * stepsV);
            } else {
                ans += stepsH;
                xc += (dx[i] * stepsH);
                yc += (dy[i] * stepsH);
            }
        }
        System.out.println(ans);
    }
}


#include<bits/stdc++.h>

using namespace std ;

#define IOS ios::sync_with_stdio(0); cin.tie(0), cout.tie(0)
#define ll long long int

#define fr first
#define se second

#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) (int)v.size()

#define pb push_back
#define mp make_pair

#define PI 3.141592653589793238

ll n, m, xc, yc, k,ans;

bool valid(ll x, ll y){return x >= 1 && x <= n && y >= 1 && y <= m;}

ll bin(ll x, ll y, ll x1, ll y1){
  ll l = 0, r = (1ll << 32);
  while(r-l > 1){
    ll mid = (r+l) >> 1;
    ll xx = x + x1*mid;
    ll yy = y + y1*mid;
    if(valid(xx, yy)) l = mid; else r = mid;
  }
  return l;
}

int main(){
  IOS;
  cin >> n >> m >> xc >> yc >> k;
  while(k--){
    int x, yy;
    cin >> x >> yy;
    ll t = bin(xc, yc, x , yy);
    xc += t * x;
    yc += t * yy;
    ans += t;
  }
  cout << ans;
  return 0;
}


#include<bits/stdc++.h>

using namespace std ;

#define IOS ios::sync_with_stdio(0); cin.tie(0), cout.tie(0)
#define ll long long int

#define fr first
#define se second

#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) (int)v.size()

#define pb push_back
#define mp make_pair

#define PI 3.141592653589793238

ll n, m, xc, yc, k,ans;

bool valid(ll x, ll y){return x > 0 && x <= n && y > 0 && y <= m;}

int main(){
  IOS;
  cin >> n >> m >> xc >> yc >> k;
  for(int i = 0 ; i < k ; i++){
    ll x, y, st;
    cin >> x >> y;
    if(xc < yc){
      if(x != 0)
        st = (n - xc)/x;
      else
        st = (m-yc)/y;
    }
    else{
      if(y != 0)
        st = (m - yc)/y;
      else
        st = (n - xc)/x;
    }
    if(st < 0)
      st = 0;
    ans += st;
    xc += (st*x);
    yc += (st*y);
  }
  cout << ans;
  return 0;
}


//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
//~ #pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (1e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

int odp[maxn];

int sil[maxn], odw[maxn];
VPII zap[maxn];
int dp[maxn];
LL dwapiec[maxn];

LL pot(LL a, LL w)
{
  if(w == 0)  return 1;
  LL b = pot(a, w / 2);
  b = (b * b) % mod;
  if(w & 1) b = b * a % mod;
  return b;
}

LL net(LL n, LL k)
{
  if(n < 0 || n < k || k < 0)
    return 0;
  return sil[n] * (LL)odw[n - k] % mod * (LL)odw[k] % mod;
}

int main()
{
	_upgrade
  sil[0] = odw[0] = 1;
  dwapiec[0] = 1;
  for(int i = 1;i < maxn;i++)
  {
    dwapiec[i] = (dwapiec[i - 1] * 25LL) % mod;
    sil[i] = (sil[i - 1] * (LL) i) % mod;
    odw[i] = pot(sil[i], mod - 2);
  }
  int m;
  cin>>m;
  string s;
  cin>>s;
  for(int i = 1;i <= m;i++)
  {
    int t;
    cin>>t;
    if(t == 1)
    {
      cin>>s;
      odp[i] = -1;
    }
    else
    {
      int n;
      cin>>n;
      zap[SZ(s)].pb({i, n});
    }
  }
  for(int a = 1;a < maxn;a++)
    if(SZ(zap[a]) > 0)
    {
      for(int j = 0;j < a;j++)
        dp[j] = 0LL;
      dp[a] = 1LL;
      for(int j = a + 1;j < maxn;j++)
      {
        // if(j <= 2 && a == 1)
        //   dbg(dp[j - 1] * 26LL, net((j - 1), a - 1), dwapiec[j - a]);
        dp[j] = (dp[j - 1] * 26LL + net(j - 1, a - 1) * dwapiec[j - a]) % mod;
        dp[j] = ((dp[j] % mod) + mod) % mod;
      }
      for(auto s : zap[a])
        odp[s.fi] = dp[s.se];
    }
  for(int i = 1;i <= m;i++)
    if(odp[i] != -1)
      cout<<odp[i]<<"\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e6+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q;
int k,n,type;
int p[maxn],invp[maxn],_26[maxn],_25[maxn],inv_26[maxn];
int ans[maxn],now_ans[maxn];
string s;
set<int> appear;
vector<pair<pii,int> > v;

int ppow(int a,int b)
{
    if (b==0) return 1;
    int ret=ppow(a,b/2);
    ret=(ret*ret)%mod;
    if (b&1) ret=ret*a%mod;
    return ret;
}

inline int H(int N,int R)
{
    return p[N+R-1]*invp[N-1]%mod*invp[R]%mod;
}

void cal(int K)
{
    int sum=0;
    for (int i=0;i<=100000-K;i++)
    {
        sum=(sum+H(K,i)*_25[i]%mod*inv_26[K+i]%mod)%mod;
        now_ans[i+K]=(sum*_26[i+K])%mod;
    }
}

int32_t main()
{
    IOS
    p[0]=invp[0]=_26[0]=_25[0]=inv_26[0]=1;
    for (int i=1;i<=1000000;i++)
    {
        p[i]=p[i-1]*i%mod;
        invp[i]=ppow(p[i],mod-2);
        _26[i]=_26[i-1]*26%mod;
        inv_26[i]=ppow(_26[i],mod-2);
        _25[i]=_25[i-1]*25%mod;
    }
    memset(ans,-1,sizeof(ans));

    cin>>q>>s;
    k=s.length();
    for (int i=1;i<=q;i++)
    {
        cin>>type;
        if (type==1)
        {
            cin>>s;
            k=s.length();
            if (appear.find(k)==appear.end()) appear.insert(k);
        }
        else
        {
            cin>>n;
            v.pb(mkp(mkp(k,n),i));
        }
    }

    int last=-1;
    for (auto i:v)
    {
        if (i.F.F!=last)
        {
            last=i.F.F;
            cal(last);
        }
        ans[i.S]=now_ans[i.F.S];
    }
    for (int i=1;i<=q;i++)
    {
        if (ans[i]!=-1) cout<<ans[i]<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e6+7;
const ll mod=1e9+7;
#define pii pair<int,int>
#define int ll
#define mkp make_pair
#define pb push_back
#define F first
#define S second

int q;
int k,n,type;
int p[maxn],invp[maxn],_26[maxn],_25[maxn],inv_26[maxn];
int ans[maxn],now_ans[maxn];
string s;
set<int> appear;
vector<pair<pii,int> > v;

int ppow(int a,int b)
{
    if (b==0) return 1;
    int ret=ppow(a,b/2);
    ret=(ret*ret)%mod;
    if (b&1) ret=ret*a%mod;
    return ret;
}

inline int H(int N,int R)
{
    return p[N+R-1]*invp[N-1]%mod*invp[R]%mod;
}

void cal(int K)
{
    int sum=0;
    for (int i=0;i<=100000-K;i++)
    {
        sum=(sum+H(K,i)*_25[i]%mod*inv_26[K+i]%mod)%mod;
        now_ans[i+K]=(sum*_26[i+K])%mod;
    }
}

int32_t main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    p[0]=invp[0]=_26[0]=_25[0]=inv_26[0]=1;
    for (int i=1;i<=1000000;i++)
    {
        p[i]=p[i-1]*i%mod;
        invp[i]=ppow(p[i],mod-2);
        _26[i]=_26[i-1]*26%mod;
        inv_26[i]=ppow(_26[i],mod-2);
        _25[i]=_25[i-1]*25%mod;
    }
    memset(ans,-1,sizeof(ans));

    cin>>q>>s;
    k=s.length();
    for (int i=1;i<=q;i++)
    {
        cin>>type;
        if (type==1)
        {
            cin>>s;
            k=s.length();
            if (appear.find(k)==appear.end()) appear.insert(k);
        }
        else
        {
            cin>>n;
            v.pb(mkp(mkp(k,n),i));
        }
    }

    int last=-1;
    for (auto i:v)
    {
        if (i.F.F!=last)
        {
            last=i.F.F;
            cal(last);
        }
        if (i.F.F>i.F.S) ans[i.S]=0;
        else ans[i.S]=now_ans[i.F.S];
    }
    for (int i=1;i<=q;i++)
    {
        if (ans[i]!=-1) cout<<ans[i]<<'\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e6+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q;
int k,n,type;
int p[maxn],invp[maxn],_26[maxn],_25[maxn],inv_26[maxn];
int ans[maxn],now_ans[maxn];
string s;
set<int> appear;
vector<pair<pii,int> > v;

int ppow(int a,int b)
{
    if (b==0) return 1;
    int ret=ppow(a,b/2);
    ret=(ret*ret)%mod;
    if (b&1) ret=ret*a%mod;
    return ret;
}

inline int H(int N,int R)
{
    return p[N+R-1]*invp[N-1]%mod*invp[R]%mod;
}

void cal(int K)
{
    int sum=0;
    for (int i=0;i<=100000-K;i++)
    {
        sum=(sum+H(K,i)*_25[i]%mod*inv_26[K+i]%mod)%mod;
        now_ans[i+K]=(sum*_26[i+K])%mod;
    }
}

int32_t main()
{
    IOS
    p[0]=invp[0]=_26[0]=_25[0]=inv_26[0]=1;
    for (int i=1;i<=1000000;i++)
    {
        p[i]=p[i-1]*i%mod;
        invp[i]=ppow(p[i],mod-2);
        _26[i]=_26[i-1]*26%mod;
        inv_26[i]=ppow(_26[i],mod-2);
        _25[i]=_25[i-1]*25%mod;
    }
    memset(ans,-1,sizeof(ans));

    cin>>q>>s;
    k=s.length();
    for (int i=1;i<=q;i++)
    {
        cin>>type;
        if (type==1)
        {
            cin>>s;
            k=s.length();
            if (appear.find(k)==appear.end()) appear.insert(k);
        }
        else
        {
            cin>>n;
            v.pb(mkp(mkp(k,n),i));
        }
    }

    int last=-1;
    for (auto i:v)
    {
        if (i.F.F!=last)
        {
            last=i.F.F;
            cal(last);
        }
        if (i.F.F>i.F.S) ans[i.S]=0;
        else ans[i.S]=now_ans[i.F.S];
    }
    for (int i=1;i<=q;i++)
    {
        if (ans[i]!=-1) cout<<ans[i]<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e6+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q;
int k,n,type;
int p[maxn],invp[maxn],_26[maxn],_25[maxn],inv_26[maxn];
string s;
set<int> appear;
map<pii,int> M;

int ppow(int a,int b)
{
    if (b==0) return 1;
    int ret=ppow(a,b/2);
    ret=(ret*ret)%mod;
    if (b&1) ret=ret*a%mod;
    return ret;
}

inline int H(int N,int R)
{
    return p[N+R-1]*invp[N-1]%mod*invp[R]%mod;
}

void Add(int K)
{
    appear.insert(K);
    int sum=0;
    for (int i=0;i<=100000-K;i++)
    {
        sum=(sum+H(K,i)*_25[i]%mod*inv_26[K+i]%mod)%mod;
        M[mkp(i+K,K)]=(sum*_26[i+K])%mod;
    }
}

int32_t main()
{
    IOS
    p[0]=invp[0]=_26[0]=_25[0]=inv_26[0]=1;
    for (int i=1;i<=1000000;i++)
    {
        p[i]=p[i-1]*i%mod;
        invp[i]=ppow(p[i],mod-2);
        _26[i]=_26[i-1]*26%mod;
        inv_26[i]=ppow(_26[i],mod-2);
        _25[i]=_25[i-1]*25%mod;
    }
    cin>>q>>s;
    k=s.length();
    Add(k);
    while (q--)
    {
        cin>>type;
        if (type==1)
        {
            cin>>s;
            k=s.length();
            if (appear.find(k)==appear.end()) Add(k);
        }
        else
        {
            cin>>n;
            if (n<k) cout<<0<<endl;
            else cout<<M[mkp(n,k)]<<endl;
        }
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned int uint;
#define MAX 100005
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"Possible" : "Impossible");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    x+=y; if (x>=mod) x-=mod;
}
void et() {
    puts("-1"); exit(0);
}

ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
int fac[MAX], inv[MAX];
void init() {
    fac[0]=1; inv[0]=1;
    REQ(i,MAX-1) {
        fac[i] = (ll)fac[i-1] * i %MOD;
        inv[i] = fastPow(fac[i],  MOD-2);
    }
}
int C(int n, int m) {
    if (n<m) return 0;
    return (ll)fac[n]*inv[m]%MOD * inv[n-m]  %MOD;
}

int N = 100000, cnt;
unordered_map<int, int> id;
vector<int> caAns[MAX];
void pre(int L, int id) {
    assert(id < 500); assert(caAns[id].empty());
    caAns[id].resize(N+1, 0);
    int p25 = 1;
    for(int n = L;n<=N;n++) {
        addmod(caAns[id][n], (ll)caAns[id][n-1] * 26 % MOD);
        addmod(caAns[id][n], (ll)p25 * C(n-1,L-1) % MOD);
        p25 = (ll)p25 *  25 %MOD;
    }
}
char ts[MAX];
void fmain() {
    scanf("%d%s", &m, ts);
    init();
    int L = strlen(ts);
    id[L]=++cnt; pre(L, cnt);
    for(int i=0,ty;i<m;i++) {
        assert(cnt < 500);
        scanf("%d", &ty);
        if (ty == 2) {
            scanf("%d", &k);
            printf("%d\n", caAns[id[L]][k]);
        } else {
            scanf("%s", ts); L = strlen(ts);
            if (id.find(L) == id.end()) {
                id[L]=++cnt; pre(L, cnt);
            }
        }
    }
}
int main() {
    fmain();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned int uint;
#define MAX 100055
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"Possible" : "Impossible");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    x+=y; if (x>=mod) x-=mod;
}
void et() {
    puts("-1"); exit(0);
}

ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
int fac[MAX], inv[MAX];
void init() {
    fac[0]=1; inv[0]=1;
    REQ(i,MAX-1) {
        fac[i] = (ll)fac[i-1] * i %MOD;
        inv[i] = fastPow(fac[i],  MOD-2);
    }
}
int C(int n, int m) {
    if (n<m) return 0;
    return (ll)fac[n]*inv[m]%MOD * inv[n-m]  %MOD;
}

int N = 100000, cnt;
unordered_map<int, int> id;
vector<int> caAns[MAX];
void pre(int L, int id) {
    caAns[id].resize(N+1, 0);
    for(int n = L;n<=N;n++) {
        addmod(caAns[id][n], (ll)caAns[id][n-1] * 26 % MOD);
        addmod(caAns[id][n], (ll)fastPow(25, n-L) * C(n-1,L-1) % MOD);
    }
}
char ts[MAX];
void fmain() {
    scanf("%d%s", &m, ts);
    init();
    int L = strlen(ts);
    id[L]=++cnt; pre(L, cnt);
    for(int i=0,ty;i<m;i++) {
        scanf("%d", &ty);
        if (ty == 2) {
            scanf("%d", &k);
            printf("%d\n", caAns[id[L]][k]);
        } else {
            scanf("%s", ts); L = strlen(ts);
            if (id.find(L) == id.end()) {
                id[L]=++cnt; pre(L, cnt);
            }
        }
    }
}
int main() {
    fmain();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned int uint;
#define MAX 100005
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"Possible" : "Impossible");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    x+=y; if (x>=mod) x-=mod;
}
void et() {
    puts("-1"); exit(0);
}

ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
int fac[MAX], inv[MAX];
void init() {
    fac[0]=1; inv[0]=1;
    REQ(i,MAX-1) {
        fac[i] = (ll)fac[i-1] * i %MOD;
        inv[i] = fastPow(fac[i],  MOD-2);
    }
}
int C(int n, int m) {
    if (n<m) return 0;
    return (ll)fac[n]*inv[m]%MOD * inv[n-m]  %MOD;
}

int N = 100000, cnt;
unordered_map<int, int> id;
vector<int> caAns[MAX];
void pre(int L, int id) {
     assert (id < 500);
     asseert(caAns[id].empty());
    caAns[id].resize(N+1, 0);
    int p25 = 1;
    for(int n = L;n<=N;n++) {
        addmod(caAns[id][n], (ll)caAns[id][n-1] * 26 % MOD);
        addmod(caAns[id][n], (ll)p25 * C(n-1,L-1) % MOD);
        p25 = (ll)p25 *  25 %MOD;
    }
}
char ts[MAX];
void fmain() {
    scanf("%d%s", &m, ts);
    init();
    int L = strlen(ts);
    id[L]=++cnt; pre(L, cnt);
    for(int i=0,ty;i<m;i++) {
        assert(cnt < 500);
        scanf("%d", &ty);
        if (ty == 2) {
            scanf("%d", &k);
            printf("%d\n", caAns[id[L]][k]);
        } else {
            scanf("%s", ts); L = strlen(ts);
            if (id.find(L) == id.end()) {
                id[L]=++cnt; pre(L, cnt);
            }
        }
    }
}
int main() {
    fmain();
    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG=20;

ll n, k, q, u, v, x, y, t, a, b, ans;
ll A[MAXN];
ll tav25[MAXN], tav26[MAXN], tav[MAXN];
ll F[MAXN], I[MAXN];
map<int, array<int, MAXN>> mp;
string s;

ll powmod(ll a, ll b){
	if (!b) return 1;
	if (b&1) return a*powmod(a*a%mod, b>>1)%mod;
	return powmod(a*a%mod, b>>1);
}

ll C(ll n, ll r){
	if (r<0 || r>n) return 0;
	return (F[n]*I[r]%mod)*I[n-r]%mod;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	tav25[0]=tav26[0]=F[0]=tav[0]=1;
	tav[1]=25*powmod(26, mod-2)%mod;
	for (int i=1; i<MAXN; i++){
		tav25[i]=tav25[i-1]*25%mod;
		tav26[i]=tav26[i-1]*26%mod;
		tav[i]=tav[i-1]*tav[1]%mod;
		F[i]=F[i-1]*i%mod;
	}
	I[MAXN-1]=powmod(F[MAXN-1], mod-2);
	for (int i=MAXN-1; i; i--) I[i-1]=I[i]*i%mod;

	cin>>q>>s;
	k=s.size();
	while (q--){
		cin>>t;
		if (t==1){
			cin>>s;
			k=s.size();
			continue ;
		}
		cin>>n;
		if (!mp.count(k)){
			mp[k][0]=0;
			for (int i=1; i<MAXN; i++) mp[k][i]=(mp[k][i-1]+C(i-1, k-1)*tav[i])%mod;
		}
		cout<<(mp[k][n]*powmod(26, n)%mod)*powmod(25, mod-1-k)%mod<<'\n';
	}

	return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG=20;

ll n, k, q, u, v, x, y, t, a, b, ans;
ll A[MAXN];
ll tav25[MAXN], tav26[MAXN];
ll F[MAXN], I[MAXN];
unordered_map<int, int> mp[MAXN];
string s;

ll powmod(ll a, ll b){
	if (!b) return 1;
	if (b&1) return a*powmod(a*a%mod, b>>1)%mod;
	return powmod(a*a%mod, b>>1);
}

ll C(ll n, ll r){
	if (r<0 || r>n) return 0;
	return (F[n]*I[r]%mod)*I[n-r]%mod;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	tav25[0]=tav26[0]=F[0]=1;
	for (int i=1; i<MAXN; i++){
		tav25[i]=tav25[i-1]*25%mod;
		tav26[i]=tav26[i-1]*26%mod;
		F[i]=F[i-1]*i%mod;
	}
	I[MAXN-1]=powmod(F[MAXN-1], mod-2);
	for (int i=MAXN-1; i; i--) I[i-1]=I[i]*i%mod;

	cin>>q>>s;
	k=s.size();
	while (q--){
		cin>>t;
		if (t==1){
			cin>>s;
			k=s.size();
			continue ;
		}
		cin>>n;
		if (mp[n].count(k)){
			cout<<mp[n][k]<<'\n';
			continue ;
		}
		ans=0;
		for (int i=k; i<=n; i++) ans=(ans+C(i-1, k-1)*(tav25[i-k]*tav26[n-i]%mod))%mod;
		cout<<(mp[n][k]=ans)<<'\n';
	}

	return 0;
}


/*
 * Author       : YangDavid
 * Created Time : 2020年02月23日 星期日 17时05分38秒
 */

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int MOD = 1000000007, MAXN = 102020;
int add(int x, int y) { return x + y >= MOD ? x + y - MOD : x + y; }
int sub(int x, int y) { return x - y < 0 ? x - y + MOD : x - y; }
int mul(int x, int y) { return 1ll * x * y % MOD; }
int qpow(int x, int y) {
    int ret = 1;
    for(; y; y >>= 1, x = mul(x, x))
        if(y & 1) ret = mul(ret, x);
    return ret;
}
int inv(int x) { return qpow(x, MOD - 2); }
int fac[MAXN], ifac[MAXN], p25[MAXN], ip25[MAXN];
struct Qry {
    int typ, x;
    Qry(int t, int y): typ(t), x(y) { }
};
vector<Qry> e;
vector< vector<int> > pref;
map<int, int> ord;
int C(int n, int m) {
    if(n < m) return 0;
    return mul(fac[n], mul(ifac[m], ifac[n - m]));
}
const int i25 = inv(25);
int main() {
    fac[0] = ifac[0] = p25[0] = ip25[0] = 1;
    for(int i = 1; i < MAXN; ++i) {
        fac[i] = mul(fac[i - 1], i);
        ifac[i] = inv(fac[i]);
        p25[i] = mul(p25[i - 1], 25);
        ip25[i] = mul(ip25[i - 1], i25);
    }
    int m; scanf("%d", &m);
    string s; cin >> s;
    e.emplace_back(1, s.length());
    ord[s.length()] = 1;
    while(m--) {
        int typ, x;
        scanf("%d", &typ);
        if(typ == 1) {
            cin >> s;
            x = (int)s.length();
            ord[x] = 1;
        } else if(typ == 2)
            scanf("%d", &x);
        e.emplace_back(typ, x);
    }
    int pc = 0;
    for(auto &it : ord) {
        it.second = pc++;
        int len = it.first;
        vector<int> pre(MAXN);
        for(int n = len; n <= 100002; ++n)
            pre[n] = add(mul(26, pre[n - 1]), mul(C(n - 1, len - 1), p25[n-len]));
        pref.push_back(pre);
    }
    int clen =  -1;
    for(auto [typ, x] : e) {
        if(typ == 1) clen = x;
        else printf("%d\n", pref[ord[clen]][x]);
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5 + 5, R = 320, mod = 1e9 + 7;
vector<pair<int, int>> v;
vector<int> lens;
ll f[N], inv[N], p25[N];
int ans[R][N];

ll bp(ll a, ll p) {
    ll r = 1;
    while (p > 0) {
        if (p & 1)
            r = r * a % mod;
        p >>= 1;
        a = a * a % mod;
    }
    return r;
}

ll c(int n, int k) {
    return f[n] * inv[k] % mod * inv[n - k] % mod;
}

int main()
{
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    p25[0] = inv[0] = f[0] = 1;
    for (int i = 1; i < N; i++) {
        f[i] = f[i - 1] * i % mod;
        inv[i] = bp(f[i], mod - 2);
        p25[i] = p25[i - 1] * 25 % mod;
    }

    int q;
    cin >> q;

    string s;

    cin >> s;
    v.push_back({1, s.size()});
    lens.push_back(s.size());

    while (q--) {
        int x, y;
        cin >> x;

        if (x == 1) {
            cin >> s;
            v.push_back({1, s.size()});
            lens.push_back(s.size());
        }
        else {
            cin >> y;
            v.push_back({2, y});
        }
    }

    sort(lens.begin(), lens.end());

    lens.erase(unique(lens.begin(), lens.end()), lens.end());

    for (int i = 0; i < lens.size(); i++) {
        int a = lens[i];

        for (int j = a; j < N; j++) {
            ans[i][j] = (ans[i][j - 1] * 26ll + p25[j - a] * c(j - 1, a - 1)) % mod;
        }
    }

    int cur = 0;

    for (auto p : v) {
        if (p.first == 1) {
            cur = lower_bound(lens.begin(), lens.end(), p.second) - lens.begin();
        }
        else {
            cout << ans[cur][p.second] << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5 + 5, R = 500, mod = 1e9 + 7;
vector<pair<int, int>> v;
vector<int> lens;
ll f[N], inv[N], p25[N];
int ans[R][N];

ll bp(ll a, ll p) {
    ll r = 1;
    while (p > 0) {
        if (p & 1)
            r = r * a % mod;
        p >>= 1;
        a = a * a % mod;
    }
    return r;
}

ll c(int n, int k) {
    return f[n] * inv[k] % mod * inv[n - k] % mod;
}

int main()
{
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    p25[0] = inv[0] = f[0] = 1;
    for (int i = 1; i < N; i++) {
        f[i] = f[i - 1] * i % mod;
        inv[i] = bp(f[i], mod - 2);
        p25[i] = p25[i - 1] * 25 % mod;
    }

    int q;
    cin >> q;

    string s;

    cin >> s;
    v.push_back({1, s.size()});
    lens.push_back(s.size());

    while (q--) {
        int x, y;
        cin >> x;

        if (x == 1) {
            cin >> s;
            v.push_back({1, s.size()});
            lens.push_back(s.size());
        }
        else {
            cin >> y;
            v.push_back({2, y});
        }
    }

    sort(lens.begin(), lens.end());

    lens.erase(unique(lens.begin(), lens.end()), lens.end());

    for (int i = 0; i < lens.size(); i++) {
        int a = lens[i];

        for (int j = a; j < N; j++) {
            ans[i][j] = (ans[i][j - 1] * 26ll + p25[j - a] * c(j - 1, a - 1)) % mod;
        }
    }

    int cur = 0;

    for (auto p : v) {
        if (p.first == 1) {
            cur = lower_bound(lens.begin(), lens.end(), p.second) - lens.begin();
        }
        else {
            cout << ans[cur][p.second] << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5 + 5, R = 400, mod = 1e9 + 7;
vector<pair<int, int>> v;
vector<int> lens;
ll f[N], inv[N], p25[N];
int ans[R][N];

ll bp(ll a, ll p) {
    ll r = 1;
    while (p > 0) {
        if (p & 1)
            r = r * a % mod;
        p >>= 1;
        a = a * a % mod;
    }
    return r;
}

ll c(int n, int k) {
    if (n < k)
        exit(-42);
    return f[n] * inv[k] % mod * inv[n - k] % mod;
}

int main()
{
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    p25[0] = inv[0] = f[0] = 1;
    for (int i = 1; i < N; i++) {
        f[i] = f[i - 1] * i % mod;
        inv[i] = bp(f[i], mod - 2);
        p25[i] = p25[i - 1] * 25 % mod;
    }

    int q;
    cin >> q;

    string s;

    cin >> s;
    v.push_back({1, s.size()});
    lens.push_back(s.size());

    while (q--) {
        int x, y;
        cin >> x;

        if (x == 1) {
            cin >> s;
            v.push_back({1, s.size()});
            lens.push_back(s.size());
        }
        else {
            cin >> y;
            v.push_back({2, y});
        }
    }

    sort(lens.begin(), lens.end());

    lens.erase(unique(lens.begin(), lens.end()), lens.end());

    for (int i = 0; i < lens.size(); i++) {
        int a = lens[i];

        for (int j = a; j < N; j++) {
            ans[i][j] = (ans[i][j - 1] * 26ll + p25[j - a] * c(j - 1, a - 1)) % mod;
        }
    }

    int cur = 0;

    for (auto p : v) {
        if (p.first == 1) {
            cur = lower_bound(lens.begin(), lens.end(), p.second) - lens.begin();
        }
        else {
            cout << ans[cur][p.second] << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5 + 5, R = 400, mod = 1e9 + 7;
vector<pair<int, int>> v;
vector<int> lens;
ll f[N], inv[N], p25[N];
int ans[R][N];

ll bp(ll a, ll p) {
    ll r = 1;
    while (p > 0) {
        if (p & 1)
            r = r * a % mod;
        p >>= 1;
        a = a * a % mod;
    }
    return r;
}

ll c(int n, int k) {
    return f[n] * inv[k] % mod * inv[n - k] % mod;
}

int main()
{
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    p25[0] = inv[0] = f[0] = 1;
    for (int i = 1; i < N; i++) {
        f[i] = f[i - 1] * i % mod;
        inv[i] = bp(f[i], mod - 2);
        p25[i] = p25[i - 1] * 25 % mod;
    }

    int q;
    cin >> q;

    string s;

    cin >> s;
    v.push_back({1, s.size()});
    lens.push_back(s.size());

    while (q--) {
        int x, y;
        cin >> x;

        if (x == 1) {
            cin >> s;
            v.push_back({1, s.size()});
            lens.push_back(s.size());
        }
        else {
            cin >> y;
            v.push_back({2, y});
        }
    }

    sort(lens.begin(), lens.end());

    lens.erase(unique(lens.begin(), lens.end()), lens.end());

    for (int i = 0; i < lens.size(); i++) {
        int a = lens[i];

        for (int j = a; j < N; j++) {
            ans[i][j] = (ans[i][j - 1] * 26ll + p25[j - a] * c(j - 1, a - 1)) % mod;
        }
    }

    int cur = 0;

    for (auto p : v) {
        if (p.first == 1) {
            cur = lower_bound(lens.begin(), lens.end(), p.second) - lens.begin();
        }
        else {
            if (cur >= R) {
                return -42;
            }
            cout << ans[cur][p.second] << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= (b); --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)

#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define vsz(v) (int)(v.size())
#define pb push_back

const long long BIG = 1000000000000000000LL;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

void solve();
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
	ull b, m;
	FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
	ull reduce(ull a) {
		ull q = (ull)((L(m) * a) >> 64);
		ull r = a - q * b; // can be proven that 0 <= r < 2*b
		return r >= b ? r - b : r;
	}
};
const int m197 = (int)(1e9) + 7;
FastMod F(m197);

template<int MOD>
struct ModInt
{
	long long x;
	ModInt() : x(0) { }
	ModInt(long long u) : x(u) { if (x >= MOD || x < 0) { x = F.reduce(x); if (x < 0) x += MOD; } }
	ModInt(const ModInt &m) : x(m.x) { }
	ModInt& operator=(const ModInt &m) { x = m.x; return *this; }
	friend bool operator==(const ModInt& a, const ModInt& b) { return a.x == b.x; }
	friend bool operator!=(const ModInt& a, const ModInt& b) { return a.x != b.x; }
	friend bool operator<(const ModInt& a, const ModInt& b) { return a.x < b.x; }

	ModInt& operator+=(const ModInt& m) { x += m.x; if (x >= MOD) x -= MOD; return *this; }
	ModInt& operator-=(const ModInt& m) { x -= m.x; if (x < 0) x += MOD; return *this; }
	ModInt& operator*=(const ModInt& m) { x = F.reduce(x*m.x); return *this; }

	friend ModInt operator+(const ModInt& a, const ModInt &b) { return ModInt(a) += ModInt(b); }
	friend ModInt operator-(const ModInt& a, const ModInt &b) { return ModInt(a) -= ModInt(b); }
	friend ModInt operator*(const ModInt& a, const ModInt &b) { return ModInt(a) *= ModInt(b); }

	static long long fp(long long u, int k)
	{
		if (k == 0) return 1;
		long long res = fp(u, k/2);
		res = (res*res) % MOD;
		if (k & 1) res = (res*u) % MOD;
		return res;
	}

	ModInt fastpow(int k) { return ModInt(fp(x, k)); }
	ModInt inv() { return ModInt(fp(x, MOD-2)); }
};

using num = ModInt<m197>;

const int borne = (int)(1e5) + 5;
int nbReq;
num alpha[2];
num powAlpha[2][borne];
num powInv[2][borne];
num fact[borne];
num invfact[borne];
vector<num> rep[borne];

num binom(int n, int k)
{
	if (k < 0 || k > n) return num(0);
	return fact[n] * invfact[k] * invfact[n-k];
}

void calc(int szstr)
{
	rep[szstr].resize(borne);
	num somme = 0;
	for (int n = szstr; n < borne; ++n) {
		somme += binom(n-1, szstr-1) * powAlpha[0][n-szstr] * powInv[1][n];
		rep[szstr][n] = powAlpha[1][n] * somme;
	}
}

void solve()
{
	fact[0] = num(1);
	form2(i, 1, borne) fact[i] = fact[i-1] * num(i);
	form(i, borne) invfact[i] = fact[i].inv();

	alpha[0] = num(25);
	alpha[1] = num(26);
	form(k, 2) {
		powAlpha[k][0].x = 1;
		powInv[k][0].x = 1;
		powAlpha[k][1] = alpha[k];
		powInv[k][1] = alpha[k].inv();
		form2(i, 2, borne) {
			powAlpha[k][i] = powAlpha[k][i-1] * powAlpha[k][1];
			powInv[k][i] = powInv[k][i-1] * powInv[k][1];
		}
	}
	int nbReq; cin >> nbReq;
	string s; cin >> s;
	int cur = s.size();
	form(i, nbReq) {
		int mode; cin >> mode;
		if (mode == 1) {
			cin >> s;
			if (cur != vsz(s) && cur > 100) {
				rep[cur].clear();
				rep[cur].shrink_to_fit();
			}
			cur = s.size();
		} else {
			if (rep[cur].empty()) calc(cur);
			int x; cin >> x;
			cout << rep[cur][x].x << "\n";
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= (b); --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)

#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define vsz(v) (int)(v.size())
#define pb push_back

const long long BIG = 1000000000000000000LL;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

void solve();
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

template<int MOD>
struct ModInt
{
	long long x;
	ModInt() : x(0) { }
	ModInt(long long u) : x(u) { if (x >= MOD || x < 0) { x %= MOD; if (x < 0) x += MOD; } }
	ModInt(const ModInt &m) : x(m.x) { }
	ModInt& operator=(const ModInt &m) { x = m.x; return *this; }
	friend bool operator==(const ModInt& a, const ModInt& b) { return a.x == b.x; }
	friend bool operator!=(const ModInt& a, const ModInt& b) { return a.x != b.x; }
	friend bool operator<(const ModInt& a, const ModInt& b) { return a.x < b.x; }

	ModInt& operator+=(const ModInt& m) { x += m.x; if (x >= MOD) x -= MOD; return *this; }
	ModInt& operator-=(const ModInt& m) { x -= m.x; if (x < 0) x += MOD; return *this; }
	ModInt& operator*=(const ModInt& m) { x = (x*m.x)%MOD; return *this; }

	friend ModInt operator+(const ModInt& a, const ModInt &b) { return ModInt(a) += ModInt(b); }
	friend ModInt operator-(const ModInt& a, const ModInt &b) { return ModInt(a) -= ModInt(b); }
	friend ModInt operator*(const ModInt& a, const ModInt &b) { return ModInt(a) *= ModInt(b); }

	static long long fp(long long u, int k)
	{
		if (k == 0) return 1;
		long long res = fp(u, k/2);
		res = (res*res) % MOD;
		if (k & 1) res = (res*u) % MOD;
		return res;
	}

	ModInt fastpow(int k) { return ModInt(fp(x, k)); }
	ModInt inv() { return ModInt(fp(x, MOD-2)); }
};

const int m197 = (int)(1e9) + 7;
using num = ModInt<m197>;

const int borne = (int)(1e5) + 5;
int nbReq;
num alpha[2];
num powAlpha[2][borne];
num powInv[2][borne];
num fact[borne];
num invfact[borne];
vector<num> rep[borne];

num binom(int n, int k)
{
	if (k < 0 || k > n) return num(0);
	return fact[n] * invfact[k] * invfact[n-k];
}

void calc(int szstr)
{
	rep[szstr].resize(borne);
	num somme = 0;
	for (int n = szstr; n < borne; ++n) {
		somme += binom(n-1, szstr-1) * powAlpha[0][n-szstr] * powInv[1][n];
		rep[szstr][n] = powAlpha[1][n] * somme;
	}
}

void solve()
{
	fact[0] = num(1);
	form2(i, 1, borne) fact[i] = fact[i-1] * num(i);
	form(i, borne) invfact[i] = fact[i].inv();

	alpha[0] = num(25);
	alpha[1] = num(26);
	form(k, 2) {
		powAlpha[k][0].x = 1;
		powInv[k][0].x = 1;
		powAlpha[k][1] = alpha[k];
		powInv[k][1] = alpha[k].inv();
		form2(i, 2, borne) {
			powAlpha[k][i] = powAlpha[k][i-1] * powAlpha[k][1];
			powInv[k][i] = powInv[k][i-1] * powInv[k][1];
		}
	}
	int nbReq; cin >> nbReq;
	string s; cin >> s;
	int cur = s.size();
	form(i, nbReq) {
		int mode; cin >> mode;
		if (mode == 1) {
			cin >> s;
			if (cur != vsz(s) && cur > 50) {
				rep[cur].clear();
				rep[cur].shrink_to_fit();
			}
			cur = s.size();
		} else {
			if (rep[cur].empty()) calc(cur);
			int x; cin >> x;
			cout << rep[cur][x].x << "\n";
		}
	}
}