P5270 No Matter What, Lord Divine Tree Will Drop the Database and Run Away.

Background

As everyone knows, Divine J (Joker) goes to Chengdu FZ every few days pretending to attend classes, but is actually playing with pointers. Divine J can, when nobody is paying attention, pull out a pointer and point himself to any position (a living person suddenly appearing in a specimen cabinet?), or swap the pointers of two people (the Chengdu FZ version of *Your Name*?), or chant system commands at the OJ to make it randomly slow down (mcfx: why did the CPU get slower after boosting?). Lord Divine Tree is very unhappy, because a tree must stay in place, and Lord Divine Tree also does not know pointers. But Lord Divine Tree is a god, so he plans to delete the database of this universe and run away, so that the idle Divine J can only play cards with Lord Divine Tree.

Description

There is a string $S$ of length $T$ and $n$ small strings $a_i$. You are given an array $R$ of length $m$, indexed starting from 1. Initially, there is an empty string $X$. Lord Divine Tree will perform $Q$ operations. In the $i$-th operation, he appends the small string $a_{R_{(i-1)\bmod m+1}}$ to the end of $X$. After each operation, Lord Divine Tree checks whether the string $X$ has a suffix such that, after an arbitrary permutation, it can become $S$. Ask how many times the string $X$ has a suffix that can become $S$ after **an arbitrary permutation** (that is, the counts of all characters are the same). Unfortunately, the character set size of this string is as large as $10^5$, so you must read the strings as integer arrays.

Input Format

Input $n, T, Q$. Then input $T$ numbers representing the string $S$. Then input $n$ lines. In each line, the first number $len$ is the length, followed by $len$ numbers representing this small string. Each input number is in the range $[0,10^5]$. Then input $m$. Input one line with $m$ numbers representing $R$.

Output Format

Output the answer.

Explanation/Hint

### Explanation for Sample 1 ![](https://cdn.luogu.com.cn/upload/pic/54698.png) ### Constraints For all testdata, $n, T, m \leq 10^5$, $1 \leq R_i \leq n$, $Q \leq 10^9$. The total length of all small strings does not exceed $10^5$. All characters are in $[0,10^5]$. | Subtask | $n$ | $T$ | $Q$ | $m$ | Special Properties | | :-: | :-: | :-: | :-: | :-: | :- | | 1 (20 points) | $n \le 10$ | $T \le 10$ | $Q \le 100$ | $m \le 10$ | The alphabet is $[0,5]$, and the total length of all small strings does not exceed $100$. | | 2 (30 points) | $n \le 10^3$ | $T \le 100$ | $Q \le 10^9$ | $m \le 10^3$ | The alphabet is $[0,5]$, $R_i = i$, $n = m$, and the total length of all small strings does not exceed $100000$. | | 3 (10 points) | $n \le 10^5$ | $T \le 10^5$ | $Q \le 10^9$ | $m \le 10^5$ | The alphabet is $[0,5]$, $R_i = i$, $n = m$. | | 4 (40 points) | $n \le 10^5$ | $T \le 10^5$ | $Q \le 10^9$ | $m \le 10^5$ | | Translated by ChatGPT 5