#include #define X first #define Y second #define pb push_back #define mp make_pair #define SZ(X) (X.size()) #define mst(a,b) memset((a),(b),sizeof(a)) #define lowbit(a) ((a)&(-a)) using namespace std; typedef long long LL; typedef long long ll; typedef pair pii; typedef pair pll; const int N = 5e5 + 10; const int mod = 1e9+7; const int inf = 0x3f3f3f3f; const LL INF = 1LL << 61; const int maxn=100+10; struct Edge{ int u,v,w,type; Edge(int u=0,int v=0,int w=0,int type=0):u(u),v(v),w(w),type(type){} bool operator < (const Edge &rhs) const { return w>T; while (T--){ int n,m; cin>>n>>m; for (int i=0;i>u>>v>>w>>s; int type; if (s[0]=='R') type=0; else if (s[0]=='G') type=1; else type=2; edge[i]=Edge(u,v,w,type); } sort(edge,edge+m); cout<<"Case #"< hehe,haha; init(); int ans=0; for (int i=0;i1) continue; int dx=Find(edge[i].u),dy=Find(edge[i].v); if (dx!=dy){ ans+=edge[i].w; fa[dy]=dx; hehe.erase(hehe.find(edge[i].w)); } } if (hehe.size()!=m-(n-1)) ans=inf; init(); int res=0; for (int i=0;i=inf) xx=-1; cout<