#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> T;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
template<class T> void chmin(T& a,const T& b){if(a>b)a=b;}
template<class T> void chmax(T& a,const T& b){if(a<b)a=b;}
struct UnionFind{
	int par[210],rank[210];
	void init(){
		for(int i=0;i<210;i++){par[i]=i;rank[i]=0;}
	}
	int find(int x){
		if(par[x]==x)return x;
		return par[x]=find(par[x]);
	}
	void unite(int x,int y){
		x = find(x);
		y = find(y);
		if(x==y)return;
		if(rank[x]>rank[y])par[y]=x;
		else{
			par[x]=y;
			if(rank[x]==rank[y])rank[y]++;
		}
		return;
	}
	bool same(int x,int y){
		return find(x)==find(y);
	}
}uf;
struct edge{
	int from,to,cost;
	edge(){}
	edge(int from,int to,int cost):from(from),to(to),cost(cost){}
	bool operator < (const edge& a) const{
		return cost < a.cost;
	}
};
int n,m,k;
vector<edge> A,B;
int cost,use_a_max,use_a_min,use;
void check(int x){
	uf.init();
	int p = 0,q = 0;
	cost = 0;
	use_a_max = 0;
	use = 0;
	while(p<A.size()||q<B.size()){
		edge e;
		bool flag = false;
		if(p==A.size())e = B[q++];
		else if(q==B.size()){
			e = A[p++];
			flag = true;
		}else{
			if(A[p].cost+x<=B[q].cost){
				e = A[p++];
				flag = true;
			}else e = B[q++];
		}
		if(!uf.same(e.from,e.to)){
			uf.unite(e.from,e.to);
			cost += e.cost;
			if(flag)use_a_max++;
			use++;
		}
	}
	uf.init();
	p = 0,q = 0;
	cost = 0;
	use_a_min = 0;
	use = 0;
	while(p<A.size()||q<B.size()){
		edge e;
		bool flag = false;
		if(p==A.size())e = B[q++];
		else if(q==B.size()){
			e = A[p++];
			flag = true;
		}else{
			if(A[p].cost+x<B[q].cost){
				e = A[p++];
				flag = true;
			}else e = B[q++];
		}
		if(!uf.same(e.from,e.to)){
			uf.unite(e.from,e.to);
			cost += e.cost;
			if(flag)use_a_min++;
			use++;
		}
	}
	return;
}
bool solve(){
	cin >> n >> m >> k;
	if(n==0&&m==0&&k==0)return false;
	A.clear();
	B.clear();
	for(int i=0;i<m;i++){
		int u,v,c;
		char l;
		cin >> u >> v >> c >> l;
		if(l=='A')A.pb(edge(u,v,c));
		else B.pb(edge(u,v,c));
	}
	sort(all(A));
	sort(all(B));
	for(int i=-105;i<=105;i++){
		check(i);
		//cout << i << ' ' << use_a_min << ' ' << use_a_max << ' ' << cost << endl;
		if(use_a_min<=k&&k<=use_a_max&&use==n-1){
			printf("%d\n",cost+i*use_a_min-i*k);
			return true;
		}
	}
	printf("-1\n");
	return true;
}
int main(){
	while(solve()){}
	return 0;
}
