#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


//https://icpc.iisf.or.jp/past-icpc/domestic2015/judge/

#define NUM 605

enum Type{
	A,
	B,
};

enum Ret{
	MANY,
	FEW,
	OK,
};

struct Edge{
	bool operator<(const struct Edge &arg) const{
		if(weight != arg.weight){
			return weight < arg.weight;
		}else{

			return type > arg.type;
		}
	};
	int from,to,weight;
	Type type;
};



int V,E,K;
int parent[205],height[205];
Edge edge[NUM];

int getParent(int id){
	if(parent[id] == id)return id;
	else{
		return parent[id] = getParent(parent[id]);
	}
}

bool isSame(int a, int b){
	return getParent(a) == getParent(b);
}

void unite(int a,int b){
	int a_parent = getParent(a);
	int b_parent = getParent(b);

	if(a_parent == b_parent)return;

	if(height[a_parent] > height[b_parent]){
		parent[b_parent] = a_parent;
	}else{
		if(height[a_parent] == height[b_parent])height[b_parent]++;
		parent[a_parent] = b_parent;
	}
}

void init(){

	for(int i = 0; i < V; i++){

		parent[i] = i;
		height[i] = 0;
	}
}

Ret is_OK(int add_value){

	int sum = 0,num_A = 0,num_B = 0;

	init();

	for(int i = 0; i < E; i++){

		if(edge[i].type == A){
			edge[i].weight += add_value;
		}
	}

	sort(edge,edge+E);

	for(int i = 0; i < E; i++){
		if(!isSame(edge[i].from,edge[i].to)){

			sum += edge[i].weight;
			unite(edge[i].from,edge[i].to);

			if(edge[i].type == A){

				num_A++;
			}else{

				num_B++;
			}
		}
	}

	for(int i = 0; i < E; i++){

		if(edge[i].type == A){
			edge[i].weight -= add_value;
		}
	}

	if(num_A == K && num_B == V-1-K){

		return OK;
	}else if(num_A < K){

		return FEW;
	}else{

		return MANY;
	}
}

void func(){

	char buf[2];

	init();

	for(int i = 0; i < E; i++){

		scanf("%d %d %d %s",&edge[i].from,&edge[i].to,&edge[i].weight,buf);

		edge[i].from--;
		edge[i].to--;

		unite(edge[i].from,edge[i].to);

		if(buf[0] == 'A'){

			edge[i].type = A;
		}else{

			edge[i].type = B;
		}
	}

	int num_boss = 0;

	for(int i = 0; i < V; i++){
		if(i == getParent(i)){

			num_boss++;
		}
	}

	if(num_boss > 1){

		printf("-1\n");
		return;
	}


	int L = -1000000,R = 1000000,mid;

	Ret ret;

	while(L+2 <= R){

		mid = (L+R)/2;

		ret = is_OK(mid);

		if(ret == FEW){

			R = mid;
		}else{

			L = mid;
		}
	}

	init();

	int add_1 = L;
	int num_1 = 0;

	for(int i = 0; i < E; i++){

		if(edge[i].type == A){
			edge[i].weight += add_1;
		}
	}

	sort(edge,edge+E);

	for(int i = 0; i < E; i++){
		if(!isSame(edge[i].from,edge[i].to)){
			unite(edge[i].from,edge[i].to);

			if(edge[i].type == A){

				num_1++;
			}
		}
	}

	if(num_1 < K){

		printf("-1\n");
		return;
	}

	for(int i = 0; i < E; i++){

		if(edge[i].type == A){
			edge[i].weight -= add_1;
		}
	}

	int add_2 = R;
	int num_2 = 0;

	for(int i = 0; i < E; i++){

		if(edge[i].type == A){
			edge[i].weight += add_2;
		}
	}

	sort(edge,edge+E);

	init();

	int sum = 0;

	for(int i = 0; i < E; i++){
		if(!isSame(edge[i].from,edge[i].to)){
			sum += edge[i].weight;
			unite(edge[i].from,edge[i].to);

			if(edge[i].type == A){

				num_2++;
			}
		}
	}

	if(num_2 > K){

		printf("-1\n");
		return;
	}

	printf("%d\n",sum-R*K);
}


int main(){

	while(true){

		scanf("%d %d %d",&V,&E,&K);
		if(V == 0 && E == 0 && K == 0)break;

		func();
	}


	return 0;
}



Measured memory_bytes: 3284