Task
RANDy is back at it again, but now he is entering a more advanced dog trick competition with his canine friend, Pompieru.
There are tricks which can be performed in this competition, numbered from to and Pompieru knows how to perform all of them.
However, performing specific tricks one after the other is not always possible: he can only perform trick right after trick if he has been trained to do so.
RANDy trained Pompieru to perform pairs of consecutive tricks , meaning that Pompieru knows how to perform trick right after trick .
The participants are required to perform tricks in this specific order.
Pompieru will first perform the trick earning points. When Pompieru finishes performing the trick he will move on to trick .
If he can perform the trick right after he will earn points, otherwise he can choose to perform some (one or more) in between tricks, enabling him to perform . If he succeeds this way, he will earn point.
If not, he will be disqualified from the contest, earning as many points as he gathered prior to his disqualification.
Help RANDy and Pompieru find the number of points they can score in this competition.
Input data
The input file consists of:
- a line containing integers and .
- a line containing the integers .
- a line containing integer .
- lines, the -th of which consisting of integers , .
Output data
The output file must contain a single line consisting of integer .
Constraints and clarifications
- .
- .
- .
- for each .
- for each .
- and for each .
- All the ordered pairs are distinct.
- It is allowed to perform a trick multiple times.
# | Score | Constraints |
---|---|---|
1 | 0 | Examples. |
2 | 21 | . |
3 | 45 | , . |
4 | 34 | No additional limitations. |
Example 1
stdin
4 5
1 3 2 5
5
1 3
3 4
4 2
2 3
4 5
stdout
6
Explanation
In the first sample case, Pompieru earns points for performing the first trick. He then earns more points because he can perform trick right after trick ,
then earns one more point performing trick and then trick . After that he earns one more point by performing tricks , , and finally .
Example 2
stdin
4 5
1 3 5 2
5
1 3
3 4
4 2
2 3
4 5
stdout
5
Explanation
In the second sample case, Pompieru earns points for performing the first trick. He then earns more points because he can perform trick right after trick ,
then earns one more point performing trick and then trick . After that, there is no sequence of tricks he can do in order to perform trick , so he gets disqualified, earning a total of points.