{"trustable":true,"prependHtml":"\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\(\u0027, right: \u0027\\\\)\u0027, display: false},\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eYou are hired by CBOOPS company that is planning to create the new billing system \nfor recently coming to the market company MegaHrun.\u003c/p\u003e\n\n\u003cp\u003eThe country is divided to m regions that are united to n superregions. There are \nt towns in the country, each of them belongs to some region. \u003c/p\u003e\n\n\u003cp\u003ePhones numbers in the country consist of d digits. Each town has its own \ncollection of phone codes.No code within the country is the prefix of another \ncode. The code consists of two parts: the region code and the town code. For \nexample, Gatchina in Leningrad region has a code 81362, here 813 is Leningrad \nregion code and 62 is the town code. Each region has exactly one code, while the \ntown may have several codes. For example, St Petersburg region has region code \n812 and St Petersburg itself has town codes 1, 2, 3, and 5. For some regions the \ntown code may be empty, for example, Moscow region has code 095 and Moscow \nitself has no additional town codes. In this case we say that the town has 0 \ntown codes.\u003c/p\u003e\n\n\u003cp\u003eEach call is characterized by the region from which it was made and the town it \nwas made to.\u003c/p\u003e\n\n\u003cp\u003eThere are four types of calls depending on the region from which it was made. \nThe call can be made:\u003c/p\u003e\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026amp;nbsp1. from home region,\u003cbr\u003e\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026amp;nbsp2. from home network within home superregion, \u003cbr\u003e\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026amp;nbsp3. from home network outside home superregion, \u003cbr\u003e\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026amp;nbsp4. from alien network.\u003cbr\u003e\n\n\u003cp\u003eThe network of MegaHrunexists in several regions of the country. One of these \nregions is homefor the subscriber we are billing. A call from this region is of \nthe first type. Calls from other regions covered by the network in this \nsuperregion are of the second type. Calls from covered by the network regions of \nother superregion are of the third type. Calls from regions not covered by the \nnetwork are of the fourth type.\u003c/p\u003e\n\n\u003cp\u003eIn turn, depending on the destination, the call can be local, regional, \ninterregional, or long distance.\u003c/p\u003e\n\n\u003cp\u003eThe call is local if it is made to the same town that the caller is. The call is \nregional if it is made to the same region. The call is interregional, if it is \nmade to another region, covered by the network. In the other case the call is \nlong distance.\u003c/p\u003e\n\n\u003cp\u003eYour task is given the tariff options for all sixteen types of calls, and the \ndescriptions of the calls, calculate the total cost of the calls. We will only \nconsider outgoing calls.\u003c/p\u003e\n\n\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e\u003c/p\u003e\n\u003cp\u003eThe first line of the input file contains t, m, n, and d (1 \u0026lt;\u003d t \u0026lt;\u003d 10 000, 1 \u0026lt;\u003d m \n\u0026lt;\u003d 200, 1 \u0026lt;\u003d n \u0026lt;\u003d 20, 2 \u0026lt;\u003d d \u0026lt;\u003d 1000). The following m lines describe regions, \neach region is described with an integer number si, the superregion it belongs \nto, and the sequence of at most d-1 digits, the region code. \u003c/p\u003e\n\u003cp\u003eDescription contains ri, the regionthistownbelongs to, and pi, the number of \ntown codes for this town (0 \u0026lt;\u003d pi \u0026lt;\u003d 100). If pi \u0026gt; 0, the second line contains \ncodes of the town, separated by spaces (pi \u003d 0 means that the town has no \nadditional code to region code). The total length of the region code and the \ntown code does not exceed d-1.\u003c/p\u003e\n\u003cp\u003eThe next line contains h, the number of the home region of the number to bill, \nand z, the number of regions covered by the MegaHrunnetwork. The next line \ncontains z integer numbers, the numbers of regions covered. The home region is \nguaranteed to be covered. \u003c/p\u003e\n\u003cp\u003eThe following four lines contain four integer numbers each, the cost per \nminute of the local, regional, interregional, and long distance call for calls \nfrom home, home within superregion, home outside superregion, and alien network \nrespectively (costs are positive and do not exceed 100000). \u003c/p\u003e\n\u003cp\u003eThe next line contains c, the number of calls made (1 \u0026lt;\u003d c \u0026lt;\u003d 10000). Each of \nthe following c lines contains an integer number ti, the town from which the \ncall was made, d digits, the phone number of the call, and li, the length of \nthe call in minutes (1 \u0026lt;\u003d li \u0026lt;\u003d 1000). If the phone number does not belong to any \nof the towns, it is considered wrong and costs nothing. \u003c/p\u003e\n\n\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e\u003c/p\u003e\n\u003cp\u003eOutput the total cost of all calls.\u003c/p\u003e\n\n\u003cb\u003eSample Input:\u003c/b\u003e\u003cpre\u003e6 4 2 10\n1 812\n1 813\n2 095\n2 096\n1 4\n1 2 3 5\n1 1\n4\n2 1\n62 \n2 2\n6133 614\n3 0\n4 2\n11 12\n1 2\n1 3\n10 20 30 40\n20 40 50 60\n30 30 30 30\n60 70 100 120\n13\n1 8121234567 10\n1 8136255555 1\n1 8136133333 5\n1 8136139999 1\n1 0953456789 5\n1 0961234567 5\n1 0969876543 4\n2 8121234567 20\n2 8136134567 5\n5 8121234567 10\n5 0953456789 5\n6 0957654321 4\n6 8121234567 20\n\u003c/pre\u003e\n\n\u003cb\u003eSample Output:\u003c/b\u003e\u003cpre\u003e3940\n\u003c/pre\u003e\n\n\u003cbr\u003e\n"}}]}