A grammar of a language is a set of rules which show the way syntactically correct sentences are build in the language. If the number of sentences is finite then these rules can be specified as a directed acyclic AND-OR graph, as that illustrated in figure 1. We assume, by convention, that the AND nodes are marked by ‘’, the OR nodes are marked by ‘|’ and the leaf nodes are labeled by any printable character different than ‘’ and ‘|’. Each node of the graph designates a sequence of sentences. Generally, such a sequence can contain identical members. We say that a node can generate the sequence of sentences it designates as follows: • A leaf generates a single sentence which is the label of the node. For example the node labeled ‘a’ in figure 1 generates the sentence ‘a’. • An OR node generates the sequence of sentences which is the union of the sequences generated by its successors. For instance, the sole OR node in figure 1 generates the sequence of sentences ‘a’, ‘b’. The order of generation (i.e. the order of sentences in the sequence) is the order of the node successors. • An AND node generates sentences computed as the concatenation of the sentences generated by its successors. If there are n successors the concatenation uses n sentences, each generated by a successor. In addition, the concatenation is performed left to right according to the order of the successors. For example, the AND node from figure 1 generates the sequence of sentences ‘ab’, ‘bb’. This sequence is computed by appending the sentence generated by the leaf node ‘b’ to each sentence generated by the OR node. Generally, if i0, i1, . . . , in is the compound index of the i-th sentence generated by an AND node with n successors, where ik is the index of a sentence corresponding to the k-th successor, then the sequence of sentences generated by the AND node is in lexicographic ascending order of the compound indexes of its members. The sentences generated by the root node of the graph are the sentences of the language whose syntax is described by the graph. The graph from figure 1 describes a language with two sentences ‘ab’ and ‘bb’. The sentences designated by a node of the graph can be gen- erated all at once, or incrementally, a number of sentences at a time. The term incrementally means that the sentences gener- ated at a given time are the next sentences which follow from those previously generated. For example, an incremental genera- tor working for the root node of the graph in figure 1 will be able to generate on its first call the sentence ‘ab’ and on its second call the sentence ‘bb’. By convention, if the sentences the node are exhausted the generator restarts with the first sentence of the node. In the example above for the third call the generator will produce ‘ab’. Your problem is to write a program which behaves as an incremental sentence generator. The program reads a graph, as explained below, and then, one at a time, an integer k. Let |k| be the absolute value of k. The program generates the next |k| sentences of the root node of the graph, prints k and, if k > 0, prints the generated sentences. The program continues with a new set of data, if any, when it reads a null value. Figure 1: An AND-OR graph
2/3 Input The integer on the first line is the number of data sets. Each data set contains a graph description and a sequence of integers which control the sentence generation process for the graph. The graph is read as follows:
3/3 Sample Output -1 3 bb ab bb 0