Book Replacement

Time limit1sMemory limit128 MB

Problem

The deadline for Professor Hachioji's assignment is tomorrow. To finish it, students must copy pages from many reference books in the library. Every reference book is kept in a storeroom that only the librarian may enter, so a student who wants a copy must ask the librarian, who fetches the requested book from the storeroom and makes the copy.

Students wait in a single queue in front of the counter. A student may request only one book at a time. After a request has been served, if the student still has more books to request, the student goes to the back of the queue.

The overall situation is shown in Figure 1 (The Library).

The storeroom contains $m$ desks $D_1, D_2, \dots, D_m$ and one shelf, placed in a line in that order from the door toward the back of the room. Each desk holds at most $c$ books. Initially every desk is empty (all books start on the shelf), and no new students arrive after the library opens.

To serve a request, the librarian looks for the book on $D_1, D_2, \dots, D_m$ in that order and finally on the shelf. After finding the book, the librarian takes it and gives a copy of the page to the student. The librarian then returns the book to $D_1$ using the following procedure.

  • If $D_1$ is not full (it currently holds fewer than $c$ books), put the requested book on $D_1$.
  • If $D_1$ is full, the librarian:
    1. temporarily puts the requested book on the non-full desk closest to the entrance, or on the shelf if every desk is full;
    2. takes from $D_1$ the book that has not been requested for the longest time (the least recently used book);
    3. puts that book on the non-full desk other than $D_1$ closest to the entrance, or on the shelf if every desk other than $D_1$ is full;
    4. takes the requested book back from its temporary place;
    5. finally puts the requested book on $D_1$.

Cost. Only accesses to a desk or the shelf cost anything: each time a book is put onto or taken from a location, that counts as one access. An access to desk $D_i$ costs $i$, and an access to the shelf costs $m + 1$. Every other action is free. Simulate the students and the librarian and report the total cost of processing all requests.

Cost accounting example. Suppose there are $3$ desks, each holding at most $1$ book, and two students: the first requests books $60, 61, 62$ and the second requests $70, 60$. Because a student who still has requests rejoins the back of the queue, the books are served in the order $60, 70, 61, 60, 62$. Serving book $60$ first costs $5$ (take it from the shelf for $4$, put it on $D_1$ for $1$). Serving $70$ then costs $13$, and serving $61, 60, 62$ costs $14, 12, 14$ respectively, for a total of $5 + 13 + 14 + 12 + 14 = 58$.

Input

The input consists of several datasets. Each dataset has the following form.

m c n
k1
b11 ... b1k1
...
kn
bn1 ... bnkn

All values are positive integers. $m$ is the number of desks with $m \le 10$. $c$ is the maximum number of books allowed on one desk with $c \le 30$. $n$ is the number of students with $n \le 100$. For the $i$-th student, $k_i$ is the number of books requested with $k_i \le 50$, and $b_{i1}, \dots, b_{ik_i}$ are the IDs of the requested books, given in request order. Every book has a distinct ID, and each ID is less than $100$; a student may request the same book more than once.

The end of the input is a line containing three zeros separated by spaces (0 0 0); it is not a dataset.

Output

For each dataset, output the total cost of processing all of its requests on its own line.