There are N strings of lowercase alphabet only. The i-th string is S_i. Every string is unique.

Provide answers for the Q queries below. The i-th query has the following format:

Query: An integer k_i and a string p_{i,1}p_{i,2}...p_{i,26} that results from permuting {`a`,`b`,...,`z`} are given. Output the sequence of the string S_{k_i} among the N strings in lexicographical order when the literal sequence is p_{i,1}<p_{i,2}<...<p_{i,26}.

Constraints

* 1 ≦ N,Q ≦ 100000
* 1 ≦ |S_i| (1 ≦ i ≦ N)
* S_i (1 ≦ i ≦ N) is a string of lowercase alphabet.
* The sum of |S_i| is no more than 400000.
* Every S_i is unique.
* 1 ≦ k_i ≦ N (1 ≦ i ≦ Q)
* For all 1 ≦ i ≦ Q, p_{i,1}p_{i,2}...p_{i,26} is a permutation of `abcd...z`.

Input

Inputs are provided from standard inputs in the following form.


N
S_1
:
S_N
Q
k_1 p_{1,1}p_{1,2}...p_{1,26}
:
k_Q p_{Q,1}p_{Q,2}...p_{Q,26}


Output

Output Q lines.

On line i, for the i-th query, output an integer indicating the sequence of the string S_{k_i} among the N strings in lexicographical order.

Examples

Input

5
aa
abbaa
abbba
aaab
aaaaaba
5
1 abcdefghijklmnopqrstuvwxyz
2 bacdefghijklmnopqrstuvwxyz
3 abcdefghijklmnopqrstuvwxyz
4 bacdefghijklmnopqrstuvwxyz
5 abcdefghijklmnopqrstuvwxyz


Output

1
2
5
4
2


Input

8
abrakatabra
abadaba
abracadabra
atcoder
grand
contest
ababa
a
6
3 abcdefghijklmnopqrstuvwxyz
6 qwertyuiopasdfghjklzxcvbnm
8 poiuytrewqlkjhgfdsamnbvcxz
2 qazwsxedcrfvtgbyhnujmikolp
1 plokmijnuhbygvtfcrdxeszwaq
4 mnbvcxzasdfghjklpoiuytrewq


Output

4
8
2
3
4
7
import java.io.*;
import java.util.*;

/**
 * Created by Katushka on 11.03.2020.
 */
public class Main {

    static int[] readArray(int size, InputReader in) {
        int[] a = new int[size];
        for (int i = 0; i < size; i++) {
            a[i] = in.nextInt();
        }
        return a;
    }

    static long[] readLongArray(int size, InputReader in) {
        long[] a = new long[size];
        for (int i = 0; i < size; i++) {
            a[i] = in.nextLong();
        }
        return a;
    }

    static void sortArray(int[] a) {
        Random random = new Random();

        for (int i = 0; i < a.length; i++) {
            int randomPos = random.nextInt(a.length);
            int t = a[i];
            a[i] = a[randomPos];
            a[randomPos] = t;
        }
        Arrays.sort(a);
    }

    private static class Node {
        char ch;
        Node[] children = new Node[27];
        List<Integer> strings = new ArrayList<>();
        int size = 0;

        public Node(char ch) {
            this.ch = ch;
        }

        @Override
        public String toString() {
            return String.valueOf(ch);
        }
    }


    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

        int n = in.nextInt();
        int[][][] lessThan = new int[27][27][];
        String[] allStrings = new String[n];

        Node root = new Node((char) 0);

        for (int i = 0; i < n; i++) {
            String s = in.nextString() + (char) ('a' - 1);
            allStrings[i] = s;
            Node node = root;
            for (int j = 0; j < s.length(); j++) {
                int c = getCharIndex(s.charAt(j));
                if (node.children[c] == null) {
                    node.children[c] = new Node(s.charAt(j));
                }
                node = node.children[c];
                node.size++;
            }
            node.strings.add(i);
        }

        fill(lessThan, root, allStrings, new ArrayList<>(), 0);

        int q = in.nextInt();
        for (int ii = 0; ii < q; ii++) {
            int k = in.nextInt();
            String order = (char) ('a' - 1) + in.nextWord();

            int less = 0;
            for (int i = 0; i < order.length(); i++) {
                for (int j = i + 1; j < order.length(); j++) {
                    int c1 = getCharIndex(order.charAt(i));
                    int c2 = getCharIndex(order.charAt(j));
                    if (lessThan[c1][c2] != null) {
                        less += lessThan[c1][c2][k - 1];
                    }
                }
            }
            out.println(less + 1);
        }

        out.close();
    }

    private static void fill(int[][][] lessThan, Node node, String[] allStrings, List<int[]> letterSize, int i) {
        for (Integer sNum : node.strings) {
            for (int j = 0; j < letterSize.size(); j++) {
                int[] sizeToAdd = letterSize.get(j);
                for (int k = 0; k < sizeToAdd.length; k++) {
                    if (sizeToAdd[k] != 0) {
                        int ch2 = getCharIndex(allStrings[sNum].charAt(j));
                        if (lessThan[k][ch2] == null) {
                            lessThan[k][ch2] = new int[allStrings.length];
                        }
                        lessThan[k][ch2][sNum] += sizeToAdd[k];
                    }
                }
            }
        }

        for (int ch1 = 0; ch1 < node.children.length; ch1++) {
            if (node.children[ch1] == null) {
                continue;
            }
            letterSize.add(new int[27]);
            for (int ch2 = 0; ch2 < node.children.length; ch2 ++) {
                if (node.children[ch2] == null || ch1 == ch2) {
                    continue;
                }
                letterSize.get(letterSize.size() - 1)[ch2] += node.children[ch2].size;
            }
            fill(lessThan, node.children[ch1], allStrings, letterSize, i + 1);
            letterSize.remove(letterSize.size() - 1);
        }
    }

    private static int getCharIndex(Character ch1) {
        return (ch1 - 'a' + 1);
    }


    private static void outputArray(int[] ans, PrintWriter out) {
        StringBuilder str = new StringBuilder();
        for (int i = 1; i < ans.length; i++) {
            int an = ans[i];
            str.append(an).append(' ');
        }
        out.println(str);
    }

    private static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                createTokenizer();
            }
            return tokenizer.nextToken();
        }

        private void createTokenizer() {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public String nextWord() {
            return next();
        }

        public String nextString() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public char nextChar() {
            return next().charAt(0);
        }

        public int[] nextInts() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                createTokenizer();
            }
            List<Integer> res = new ArrayList<>();
            while (tokenizer.hasMoreElements()) {
                res.add(Integer.parseInt(tokenizer.nextToken()));
            }
            int[] resArray = new int[res.size()];
            for (int i = 0; i < res.size(); i++) {
                resArray[i] = res.get(i);
            }
            return resArray;
        }
    }
}
