Document Compression

Alice needs to send text documents to Bob using the Internet. They want to use the communication channel as efficiently as possible, thus they decided to use a document codification scheme based on a set of basis documents. The scheme works as follows: • Each document to be transmitted is represented using a set of terms. This ignores the frequency of each term and its position in the original document, i.e., a document is represented by the list of different terms that it contains. • There is a set of basis documents that has been previously agreed upon by Alice and Bob. The basis documents are numbered and they are also represented using sets of terms. Both Alice and Bob have a copy of the set, so it does not need to be transmitted. • Before transmitting a particular document, Alice finds the basis documents that need to be combined to obtain the original content of the document. The documents are combined by uniting their corresponding sets of terms. • Alice transmits to Bob the list of indexes of the basis documents that represent the original document. • Bob rebuilds the original document by combining the corresponding basis documents specified by Alice. For example, suppose that the basis document set contains the following documents, each one specified by the list of different terms that it contains: Document 1: {2, 5, 7, 10} Document 2: {8, 9, 10} Document 3: {1, 2, 3, 4} Now, suppose that Alice wants to transmit the document {1, 2, 3, 4, 5, 7, 10}. This document can be obtained by combining the basis documents 1 and 3, so Alice transmits this list of two indices and Bob uses it to rebuild the original document. Your work is, given a set of basis documents and a document to be transmitted, to find the minimum number of basis documents needed to represent it, if it can be represented; otherwise, you have to indicate that it is impossible to attain a representation. Input Each test case is described as follows: • AlinewithtwointegernumbersM andN (1≤M ≤100,1≤N ≤100),separatedbyblanks. M corresponds to the number of basis documents and N corresponds to the number of documents to codify. • Each one of the following M + N lines contains the numbers k, t1, t2, . . . , tk, separated by blanks (1≤k≤16,1≤ti ≤16foreach1≤i≤k). Thefirstvalueindicatesthenumberof different terms in the document and the following numbers correspond to the different terms in the document. The first M lines describe the basis documents and the next N lines describe the documents to codify. The last test case is followed by a line containing two zeros.

2/2 Output For each test case you must print a line containing the integers r1, r2, . . . , rN (with a blank between each two numbers), where ri is the minimum number of basis documents required to represent the i-th document of the input (1 ≤ i ≤ N). If it is impossible to represent the i-th document, then ri must be the number ‘0’. Sample Input 31 4 2 5 7 10 3 8 9 10 41234 7 1 2 3 4 5 7 10 23 221 243 234 3312 43142 00 Sample Output 2 102