A city is made up of many small islands, and its citizens live on those islands. Moving between islands requires a ferry, which the citizens find inconvenient. The mayor decided to build bridges that connect all the islands.
The city has two construction companies, A and B. The mayor asked both for quotes, and every proposal that came back has the form "company A (or company B) builds a bridge between island u and island v for w hundred million yen".
The mayor wants to accept part of the proposals and end up with the cheapest plan. Accepting too many proposals from one company could bankrupt the other, and the city has only these two construction companies, so that outcome is unwelcome. To avoid complaints about wasteful construction, the mayor can accept only the smallest number of bridges that connects all the islands, that is n−1 of them. The mayor therefore decided to accept exactly k proposals from company A and exactly n−1−k proposals from company B.
Write a program that computes the cost of the cheapest plan meeting these conditions. The cost of a plan is the sum of the costs written in the accepted proposals.
The input consists of several datasets, at most 30 of them. Each dataset has the following format.
n m k
u1 v1 w1 l1
...
um vm wm lm
The first line holds three integers n, m, k, where n is the number of islands, m is the total number of proposals, and k is the number of proposals to order from company A (2≤n≤200, 1≤m≤600, 0≤k≤n−1). Islands are numbered 1 through n.
Each of the next m lines holds one proposal as three integers ui, vi, wi and one character li, where ui and vi are the two islands the bridge joins, wi is the cost of the bridge in hundred million yen, and li is the name of the company that submitted the proposal (1≤ui≤n, 1≤vi≤n, 1≤wi≤100, and li is 'A' or 'B'). Every bridge joins two distinct islands, so ui=vi. A company submits at most one proposal per pair of islands, so if i=j and li=lj then {ui,vi}={uj,vj}.
The end of the input is a line with three zeros separated by single spaces.
For each dataset, print the cost of the cheapest plan, in hundred million yen, as a single integer on its own line. If no plan meets the conditions, print -1.