Dog Trick Competition 2

Time limit: 1s Memory limit: 256MB Input: Output:

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 KK tricks which can be performed in this competition, numbered from 11 to KK 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 bb right after trick aa if he has been trained to do so.
RANDy trained Pompieru to perform MM pairs of consecutive tricks (Ai,Bi)(A_i, B_i), meaning that Pompieru knows how to perform trick BiB_i right after trick AiA_i.

The participants are required to perform NN tricks T0,T1,,TN1T_0, T_1, \ldots, T_{N-1} in this specific order.
Pompieru will first perform the trick T0T_0 earning 22 points. When Pompieru finishes performing the trick TiT_i he will move on to trick Ti+1T_{i + 1}.
If he can perform the trick Ti+1T_{i + 1} right after TiT_i he will earn 22 points, otherwise he can choose to perform some (one or more) in between tricks, enabling him to perform Ti+1T_{i + 1}. If he succeeds this way, he will earn 11 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 PP they can score in this competition.

Input data

The input file consists of:

  • a line containing integers NN and KK.
  • a line containing the NN integers T0,,TN1T_{0}, \, \ldots, \, T_{N-1}.
  • a line containing integer MM.
  • MM lines, the ii-th of which consisting of integers AiA_{i}, BiB_{i}.

Output data

The output file must contain a single line consisting of integer PP.

Constraints and clarifications

  • 2N250 0002 \le N \le 250 \ 000.
  • 1K250 0001 \le K \le 250 \ 000.
  • 1M250 0001 \le M \le 250 \ 000.
  • 1TiK1 \le T_i \le K for each i=0N1i = 0 \ldots N - 1.
  • TiTi+1T_i \neq T_{i + 1} for each i=0N2i = 0 \ldots N - 2.
  • 1Ai,BiK1 \le A_i, B_i \le K and AiBiA_i \neq B_i for each i=0M1i = 0 \ldots M - 1.
  • All the ordered pairs (Ai,Bi)(A_i, B_i) are distinct.
  • It is allowed to perform a trick multiple times.
# Score Constraints
1 0 Examples.
2 21 N,K,M200N, K, M \le 200.
3 45 1M1000001 \le M \le 100\,000, 1K500001 \le K \le 50\,000.
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 22 points for performing the first trick. He then earns 22 more points because he can perform trick 33 right after trick 11,
then earns one more point performing trick 44 and then trick 22. After that he earns one more point by performing tricks 33, 44, and finally 55.

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 22 points for performing the first trick. He then earns 22 more points because he can perform trick 33 right after trick 11,
then earns one more point performing trick 44 and then trick 55. After that, there is no sequence of tricks he can do in order to perform trick 22, so he gets disqualified, earning a total of 55 points.

Log in or sign up to be able to send submissions!