#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10, M = 1e6+10, INF = 0x3f3f3f3f;
int h[N],ecnt,A[N];
struct Edge{
int to,nxt,f, w;
}e[M];
int q[M],vis[N],dis[N], flow[N], pre[N];
int n,m,S,T,E,sum,Enum;
void add(int a,int b,int f,int w)
{
e[ecnt].to = b, e[ecnt].f = f, e[ecnt].w = w, e[ecnt].nxt = h[a], h[a] = ecnt ++;
e[ecnt].to = a, e[ecnt].f = 0, e[ecnt].w = -w,e[ecnt].nxt = h[b], h[b] = ecnt ++;
}
int spfa()
{
memset(dis, 0x3f, sizeof dis);
memset(vis, 0, sizeof vis);
memset(pre, -1, sizeof pre);
int hh = 0, tt = -1;
q[++ tt] = S; dis[S] = 0; vis[S] = 1, flow[S] = INF;
while(hh <= tt)
{
int u = q[hh ++]; vis[u] = 0;
for(int i = h[u]; ~i; i = e[i].nxt)
{
int to = e[i].to;
if(e[i].f && dis[to] > dis[u] + e[i].w)
{
dis[to] = dis[u] + e[i].w;
pre[to] = i;
flow[to] = min(flow[u], e[i].f);
if(!vis[to]) vis[to] = 1, q[++ tt] = to;
}
}
}
if(pre[T] == -1) return -1;
return flow[T];
}
int solve()
{
int f = 0, w = 0;
while(spfa() != -1)
{
f += flow[T]; w += dis[T] * flow[T];
int x = T;
while(x != S)
{
e[pre[x]]. f -= flow[T];
e[pre[x] ^ 1]. f += flow[T];
x = e[pre[x] ^ 1]. to;
}
}
if(f != sum) return -1;
return w + Enum;
}
int main()
{
int cas;
cin >> cas >> E >> E;
while( cas -- )
{
sum = Enum = ecnt = 0;
memset(h, -1, sizeof h);
memset(A, 0, sizeof A);
cin >> n >> m;
S = 0, T = n + 1;
for(int i = 0; i < m; ++i)
{
int x, y;
char op[2];
scanf("%d%d%s",&x, &y, op);
if(op[0] =='A')
{
add(x, y, INF, E);
A[x] --; A[y] ++;
Enum += E;
}
else if(op[0] == 'B')
{
A[x] --; A[y] ++;
Enum += E;
}
else if(op[0] == 'C')
{
add(x, y, INF, E);
}
else add(x, y, 1, E);
}
for(int i = 1; i <= n; ++i)
if(A[i] > 0) add(S, i, A[i], 0), sum += A[i];
else if(A[i] < 0) add(i, T, -A[i], 0);
printf("%d\n",solve());
}
}