Book Replacement

Time limit1sMemory limit128 MB

Summary
Simulate a library queue where a librarian relocates books between capacity-limited desks and a shelf using an LRU-like eviction rule, and compute total access cost.
Level

Medium6 of 10

Topics
Simulation, Queue, Implementation
Solved
No attempts yet

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 mm desks D1,D2,…,DmD_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 cc 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 D1,D2,…,DmD_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 D1D_1 using the following procedure.

  • If D1D_1 is not full (it currently holds fewer than cc books), put the requested book on D1D_1.
  • If D1D_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 D1D_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 D1D_1 closest to the entrance, or on the shelf if every desk other than D1D_1 is full;
    4. takes the requested book back from its temporary place;
    5. finally puts the requested book on D1D_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 DiD_i costs ii, and an access to the shelf costs m+1m + 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 33 desks, each holding at most 11 book, and two students: the first requests books 60,61,6260, 61, 62 and the second requests 70,6070, 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,6260, 70, 61, 60, 62. Serving book 6060 first costs 55 (take it from the shelf for 44, put it on D1D_1 for 11). Serving 7070 then costs 1313, and serving 61,60,6261, 60, 62 costs 14,12,1414, 12, 14 respectively, for a total of 5+13+14+12+14=585 + 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. mm is the number of desks with m≤10m \le 10. cc is the maximum number of books allowed on one desk with c≤30c \le 30. nn is the number of students with n≤100n \le 100. For the ii-th student, kik_i is the number of books requested with ki≤50k_i \le 50, and bi1,…,bikib_{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 100100; 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.

Examples1

  1. Example 1

    Input
    2 1 1
    1
    50
    2 1 2
    1
    50
    1
    60
    2 1 2
    2
    60 61
    1
    70
    4 2 3
    3
    60 61 62
    1
    70
    2
    80 81
    3 1 2
    3
    60 61 62
    2
    70 60
    1 2 5
    2
    87 95
    3
    96 71 35
    2
    68 2
    3
    3 18 93
    2
    57 2
    2 2 1
    5
    1 2 1 3 1
    0 0 0
    
    Expected output
    4
    16
    28
    68
    58
    98
    23