Jazz Journey

Given a fixed closed tour and ticket prices for one-way and round-trip fares, find the minimum cost to cover every leg of the tour.

Medium7GraphGreedyHash mapNo attempts yetTime limit5sMemory limit512 MB

Problem

Ivan is planning a large European tour with his jazz band. There are nn cities in Europe, numbered with integers 11 through nn. Ivan is planning dd concerts in cities a1,a2,,ada_1, a_2, \ldots, a_d in that exact order. Two consecutive concerts are never in the same city (aiai+1a_i \ne a_{i+1}), some cities may be visited many times, and the tour ends in the same city where it began (a1=ada_1 = a_d).

Ivan always takes a direct flight between cities aia_i and ai+1a_{i+1}. However, he wants to be smart with his ticket purchases to save money. Airlines price tickets based on supply and demand, so it may happen, for example, that one-way tickets are more expensive than round trip tickets between the same cities.

There are two kinds of tickets available for purchase:

  • A one-way ticket from origin city aa to destination city bb can be used to fly from aa to bb once. It cannot be used in the opposite direction.
  • A round trip ticket from origin city aa to destination city bb can be used to fly once from aa to bb and once from bb to aa. The return segment (from bb to aa) does not need to be used. However, the segments have to be flown in order: Ivan may not use the return segment of a ticket to fly from bb to aa unless he has already used the first segment of that ticket to fly from aa to bb.

You are given a list of available airfares. Find the least amount of money Ivan needs to spend on tickets to complete his journey. Ivan can purchase any number of tickets for each airfare. Once again, Ivan needs to take a direct flight from aia_i to ai+1a_{i+1} for every i=1,2,,d1i = 1, 2, \ldots, d-1. You may assume that the journey can be completed using the given airfares.

Input

The first line contains two integers nn and dd (2n,d3000002 \le n, d \le 300000): the number of cities in Europe and the number of concerts.

The second line contains the integers a1,a2,,ada_1, a_2, \ldots, a_d (1ain1 \le a_i \le n, aiai+1a_i \ne a_{i+1}, a1=ada_1 = a_d): the planned tour schedule.

The third line contains an integer mm (3m3000003 \le m \le 300000): the number of airfares. The kk-th of the following mm lines contains four tokens sks_k, dkd_k, tkt_k, pkp_k that describe the kk-th airfare:

  • sks_k and dkd_k (1sk,dkn1 \le s_k, d_k \le n, skdks_k \ne d_k) are the origin and the destination city, respectively.
  • tkt_k is an uppercase letter O or R, denoting a one-way or a round trip ticket, respectively.
  • pkp_k (1pk1091 \le p_k \le 10^9) is the ticket price, an integer.

Output

Output the least amount of money needed to purchase tickets that allow Ivan to complete the planned tour.