#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};
struct edge {
    ll u, v, w;
    char l;
};

bool operator> (const edge &a, const edge &b) {
    if(a.w != b.w) return a.w > b.w;
    return a.l > b.l;
}


edge edges[606];
int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        ll n, m, k;
        cin >> n >> m >> k;
        if(n == 0) break;
        for(int i = 1; i <= m; i++) {
            cin >> edges[i].u >> edges[i].v >> edges[i].w >> edges[i].l;
            edges[i].w *= 2;
        }
        UnionFind checker(n + 1);
        ll NUMA = 0;
        for(int i = 1; i <= m; i++) {
            if(edges[i].l == 'A') {
                if(checker.merge(edges[i].u, edges[i].v)) NUMA++;
            }
        }
        for(int i = 1; i <= m; i++) {
            if(edges[i].l == 'B') {
                checker.merge(edges[i].u, edges[i].v);
            }
        }
        if(checker.size(1) < n || NUMA < k) {
            cout << -1 << endl;
            continue;
        }
        checker.init(n + 1);
        NUMA = 0;
        for(int i = 1; i <= m; i++) {
            if(edges[i].l == 'B') {
                checker.merge(edges[i].u, edges[i].v);
            }
        }
        for(int i = 1; i <= m; i++) {
            if(edges[i].l == 'A') {
                if(checker.merge(edges[i].u, edges[i].v)) NUMA++;
            }
        }
        if(NUMA > k) {
            cout << -1 << endl;
            continue;
        }
        ll ok = -1e4;
        ll ng = 1e4;
        while(ng - ok > 1) {
            ll mid = (ok + ng) / 2;
            //cerr << "mid; " << mid << endl;
            priority_queue<edge, vector<edge>, greater<edge>> query;
            for(int i = 1; i <= m; i++) {
                edge now = edges[i];
                if(now.l == 'A') {
                    now.w += mid;
                }
                query.push(now);
            }
            UnionFind uni(n + 1);
            ll numa = 0;
            while(!query.empty()) {
                edge now = query.top();
                query.pop();
                if(uni.merge(now.u, now.v)) {
                    if(now.l == 'A') numa++;
                    //cerr << now.u << " " << now.v << " " << now.w << " " << now.l << endl;
                }
            }
            if(numa >= k) ok = mid;
            else ng = mid;
        }
        //cerr << "ok: " << ok << endl;
        ll ans = -ok * k;
        priority_queue<edge, vector<edge>, greater<edge>> query;
        for(int i = 1; i <= m; i++) {
            edge now = edges[i];
            if(now.l == 'A') {
                now.w += ok;
            }
            query.push(now);
        }
        UnionFind uni(n + 1);
        ll numa = 0;
        while(!query.empty()) {
            edge now = query.top();
            query.pop();
            if(uni.merge(now.u, now.v)) {
                if(now.l == 'A') numa++;
                ans += now.w;
                //cerr << now.u << " "<< now.v << " " << now.w << " " << now.l << endl;
            }
        }
        cout << ans / 2 << endl;
    }
    return 0;
}


Measured memory_bytes: 3252