Baza

Count the database rows that match each query in every column not marked -1.

Easy2Brute forceMatrixInterviewNo attempts yetTime limit1sMemory limit64 MB

Problem

Mirko got a summer internship at a large IT company. The company keeps a database of NN rows and MM columns.

On his first day Mirko received QQ queries. Each query is a list of MM numbers. Some of the numbers were lost in transit, and every lost position is written as -1. Mirko wants to know how many rows of the database correspond to each query, that is, how many rows hold the same number as the query in every position that is not -1.

For example, if the query is -1 3 2, count the rows whose first column holds any number, whose second column holds 3, and whose third column holds 2.

Help Mirko and answer all of the queries.

Input

The first line contains two integers NN (1N1031 \le N \le 10^3) and MM (1M1031 \le M \le 10^3), the size of the database.

Each of the next NN lines contains MM integers AijA_{ij} (1Aij1061 \le A_{ij} \le 10^6), the content of the database.

The next line contains QQ (1Q501 \le Q \le 50), the number of queries.

Each of the next QQ lines contains MM integers BijB_{ij} (Bij=1B_{ij} = -1 or 1Bij1061 \le B_{ij} \le 10^6), the description of the iith query.

Output

Print QQ lines. The iith line holds the answer to the iith query.

Hint

The database in the first example has four rows. The first query asks how many rows have the number 2 in the third column. Rows 1 (1 5 2) and 3 (4 3 2) qualify, so the answer is 2. The second query asks for rows with 3 in the second column and 2 in the third column. Only row 3 (4 3 2) qualifies, so the answer is 1. The third query is all -1, so it asks for the total number of rows, which is 4.