1 #include <bits/stdc++.h>
2 #define pb push_back
3 #define mp make_pair
4 #define fi first
5 #define se second
6 #define all(a) (a).begin(), (a).end()
7 #define fillchar(a, x) memset(a, x, sizeof(a))
8 #define huan printf("\n");
9 #define debug(a,b) cout<<a<<" "<<b<<" "<<endl;
10 using namespace std;
11 typedef long long ll;
12 const int maxn=150+10,inf=0x3f3f3f3f;
13 const ll mod=1e9+7;
14 int n,m;
15 int p[maxn],par[maxn],g[maxn][maxn];
16 struct edge
17 {
18 int u,v,w,id;
19 char col;
20 } a[maxn],b[maxn],c[maxn];
21 int cmp(edge a,edge b)
22 {
23 return a.w<b.w;
24 }
25 int find_(int x)
26 {
27 return p[x]==x?x:p[x]=find_(p[x]);
28 }
29 int _find(int x)
30 {
31 return x==par[x]?x:par[x]=_find(par[x]);
32 }
33 void unio(int a,int b)
34 {
35 int ra=_find(a);
36 int rb=_find(b);
37 if(ra!=rb)
38 par[rb]=ra;
39 }
40 int ans1[maxn],ans2[maxn];
41 int main()
42 {
43 int t,kase=1;
44 cin>>t;
45 while(t--)
46 {
47 map<int,int> mp;
48 scanf("%d %d",&n,&m);
49 int u,v,w;
50 for(int i=0; i<m; i++)
51 {
52 cin>>a[i].u>>a[i].v>>a[i].w>>a[i].col;
53 a[i].id=i;
54 }
55 printf("Case #%d:\n",kase++);
56 sort(a,a+m,cmp);
57 //for(int i=0;i<m;i++)
58 // debug(a[i].w,a[i].col);
59 if(n==1)
60 {
61 int sum=0;
62 for(int i=0; i<m; i++)
63 {
64 sum+=a[i].w;
65 cout<<sum<<endl;
66 }
67 continue;
68 }
69 //------------------------------------------------------------------------------
70 int cnt=0,flag1=1;mp.clear();
71 memset(g,inf,sizeof(g));
72 memset(ans1,inf,sizeof(ans1));
73 for(int i=1; i<=n; i++)
74 p[i]=i,par[i]=i;
75 for(int i=0; i<m; i++)
76 {
77 if((a[i].col==‘R‘||a[i].col==‘G‘)&&a[i].w<g[a[i].u][a[i].v]&&a[i].u!=a[i].v)
78 {
79 g[a[i].u][a[i].v]=g[a[i].v][a[i].u]=a[i].w;
80 b[cnt++]= {a[i].u,a[i].v,a[i].w,a[i].id,a[i].col};
81 unio(a[i].u,a[i].v);
82 }
83 }
84 for(int i=2; i<=n; i++)
85 if(_find(i-1)!=_find(i))flag1=0;
86 if(flag1)
87 {
88 sort(b,b+cnt,cmp);
89 ans1[n-1]=0;
90 for(int i=0; i<cnt; i++)
91 {
92 int x,y,z;
93 x=find_(b[i].u),y=find_(b[i].v),z=b[i].w;
94 if(x!=y)
95 {
96 ans1[n-1]+=z;
97 mp[b[i].id]=1;
98 p[x]=y;
99 }
100 }
101 int k=n;
102 for(int i=0; i<m; i++)
103 {
104 if(mp[a[i].id]==0)
105 ans1[k]=ans1[k-1]+a[i].w,k++;
106 }
107 }
108 //---------------------------------------------------------------------------------------------
109 int flag2=1;cnt=0;mp.clear();
110 memset(g,inf,sizeof(g));
111 memset(ans2,inf,sizeof(ans2));
112 for(int i=1; i<=n; i++)
113 p[i]=i,par[i]=i;
114 for(int i=0; i<m; i++)
115 {
116 if((a[i].col==‘B‘||a[i].col==‘G‘)&&a[i].w<g[a[i].u][a[i].v]&&a[i].u!=a[i].v)
117 {
118 g[a[i].u][a[i].v]=g[a[i].v][a[i].u]=a[i].w;
119 b[cnt++]={a[i].u,a[i].v,a[i].w,a[i].id,a[i].col};
120 unio(a[i].u,a[i].v);
121 }
122 }
123 for(int i=2; i<=n; i++)
124 if(_find(i-1)!=_find(i))flag2=0;
125 if(flag2==0&&flag1==0)
126 {
127 for(int i=1;i<=m; i++)
128 cout<<-1<<endl;
129 continue;
130 }
131 if(flag2)
132 {
133
134 sort(b,b+cnt,cmp);
135 ans2[n-1]=0;
136 for(int i=0; i<cnt; i++)
137 {
138 int x,y,z;
139 x=find_(b[i].u),y=find_(b[i].v),z=b[i].w;
140 if(x!=y)
141 {
142 ans2[n-1]+=z;
143 mp[b[i].id]=1;
144 p[x]=y;
145 }
146 }
147 int k=n;
148 for(int i=0; i<m; i++)
149 {
150 if(mp[a[i].id]==0)
151 ans2[k]=ans2[k-1]+a[i].w,k++;
152 }
153 }
154 for(int i=1; i<n-1; i++)
155 cout<<-1<<endl;
156 for(int i=n-1; i<=m; i++)
157 {
158 cout<<min(ans1[i],ans2[i])<<endl;
159 }
160 }
161 }