#include <algorithm>
#include <cassert>
#include <cstdio>
#include <tuple>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < int(n); ++(i))
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using ll = long long;
using namespace std;
template <class T> inline void setmin(T & a, T const & b) { a = min(a, b); }

struct disjoint_sets {
    vector<int> data;
    disjoint_sets() = default;
    explicit disjoint_sets(size_t n) : data(n, -1) {}
    bool is_root(int i) { return data[i] < 0; }
    int find_root(int i) { return is_root(i) ? i : (data[i] = find_root(data[i])); }
    int set_size(int i) { return - data[find_root(i)]; }
    int union_sets(int i, int j) {
        i = find_root(i); j = find_root(j);
        if (i != j) {
            if (set_size(i) < set_size(j)) swap(i,j);
            data[i] += data[j];
            data[j] = i;
        }
        return i;
    }
    bool is_same(int i, int j) { return find_root(i) == find_root(j); }
};

template <typename UnaryPredicate>
ll binsearch(ll l, ll r, UnaryPredicate p) { // [l, r), p is monotone
    assert (l < r);
    -- l;
    while (r - l > 1) {
        ll m = (l + r) / 2;
        (p(m) ? r : l) = m;
    }
    return r; // = min { x in [l, r) | p(x) }, or r
}

template <typename T> struct weighted_edge { int u, v; T cost; bool is_a; };
template <typename T> bool operator < (weighted_edge<T> const & a, weighted_edge<T> const & b) { return make_pair(a.cost, a.is_a) < make_pair(b.cost, b.is_a); } // weak ordering
tuple<int, int, int> minimum_spanning_tree(int n, vector<vector<pair<int, int> > > const & g, vector<vector<pair<int, int> > > const & h, int a) {
    vector<weighted_edge<int> > edges;
    repeat (p, 2) {
        repeat (i, n) {
            for (auto edge : (p ? g : h)[i]) {
                int j, cost; tie(j, cost) = edge;
                if (i < j) edges.push_back((weighted_edge<int>) { i, j, cost + (p ? a : 0), bool(p) });
            }
        }
    }
    whole(sort, edges);
    int a_count = 0;
    int b_count = 0;
    int total_cost = 0;
    disjoint_sets sets(n);
    for (auto e : edges) { // Kruskal's method, O(E \log E)
        if (not sets.is_same(e.u, e.v)) {
            sets.union_sets(e.u, e.v);
            (e.is_a ? a_count : b_count) += 1;
            total_cost += e.cost;
        }
    }
    return make_tuple(a_count, b_count, total_cost);
}
int count_a_edges(int n, vector<vector<pair<int, int> > > const & g, vector<vector<pair<int, int> > > const & h, int a) {
    int a_count, b_count;
    tie(a_count, b_count, ignore) = minimum_spanning_tree(n, g, h, a);
    if (a_count + b_count != n - 1) return -1;
    return a_count;
}

constexpr int W_MAX = 100;
int main() {
    while (true) {
        int n, m, k; scanf("%d%d%d", &n, &m, &k);
        if (n == 0 and m == 0 and k == 0) break;
        vector<vector<pair<int, int> > > g(n); // of A
        vector<vector<pair<int, int> > > h(n); // of B
        repeat (i, m) {
            int u, v, w; char l; scanf("%d%d%d %c", &u, &v, &w, &l); -- u; -- v;
            assert (l == 'A' or l == 'B');
            (l == 'A' ? g : h)[u].emplace_back(v, w);
            (l == 'A' ? g : h)[v].emplace_back(u, w);
        }
        constexpr int inf = 10 * W_MAX;
        int pinf_a = count_a_edges(n, g, h, + inf);
        int ninf_a = count_a_edges(n, g, h, - inf);
        int result;
        if (pinf_a == -1 or ninf_a == -1 or (pinf_a - k) * (ninf_a - k) > 0) {
            result = -1;
        } else {
            int a = binsearch(- 2 * W_MAX, 3 * W_MAX, [&](int a) {
                int a_count = count_a_edges(n, g, h, a);
                return a_count <= k;
            });
            int a_count, total_cost;
            tie(a_count, ignore, total_cost) = minimum_spanning_tree(n, g, h, a);
            assert (a_count <= k);
            result = total_cost - a * k;
        }
        printf("%d\n", result);
    }
    return 0;
}

Measured memory_bytes: 2700