#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; const int MAXN=40000; const int MAXE=20000000; const int inf=1<<30; int head[MAXN],s,t,cnt,ans; int d[MAXN],pre[MAXN]; bool vis[MAXN]; int q[MAXE]; int le[210],ri[210],fee[210]; int a[500],b[500]; int pos; struct Edge { int u,v,c,w,next; }edge[MAXE]; void addedge(int u,int v,int w,int c) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].c=c; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].u=v; edge[cnt].w=-w; edge[cnt].c=0; edge[cnt].next=head[v]; head[v]=cnt++; } int SPFA() { int l,r; memset(pre,-1,sizeof(pre)); memset(vis,0,sizeof(vis)); for(int i=0;i<=t;i++) d[i]=inf; d[s]=0; l=0;r=0; q[r++]=s; vis[s]=1; while(l<r) { int u=q[l++]; vis[u]=0; for(int j=head[u];j!=-1;j=edge[j].next) { int v=edge[j].v; if(edge[j].c>0&&d[u]+edge[j].w<d[v]) { d[v]=d[u]+edge[j].w; pre[v]=j; if(!vis[v]) { vis[v]=1; q[r++]=v; } } } } if(d[t]==inf) return 0; return 1; } void MCMF() { int flow=0; while(SPFA()) { int u=t; int mini=inf; while(u!=s) { if(edge[pre[u]].c<mini) mini=edge[pre[u]].c; u=edge[pre[u]].u; } flow+=mini; u=t; ans+=d[t]*mini; while(u!=s) { edge[pre[u]].c-=mini; edge[pre[u]^1].c+=mini; u=edge[pre[u]].u; } } } int bin(int u) { int l=1,r=pos,m=(l+r)/2; while(l<=r) { m=(l+r)/2; if(u==b[m]) return m; else if(u>b[m]) l=m+1; else if(u<b[m]) r=m-1; } } int main() { int n,k; int cas; int i,j; scanf("%d",&cas); while(cas--) { scanf("%d%d",&n,&k); cnt=0; int now=0; for(i=1;i<=n;i++) { scanf("%d%d%d",&le[i],&ri[i],&fee[i]); a[now++]=le[i]; a[now++]=ri[i]; } sort(a,a+now); b[1]=a[0]; pos=1; for(i=1;i<now;i++) if(a[i]!=b[pos]) b[++pos]=a[i]; //for(i=1;i<=pos;i++)printf("%d ",b[i]); //printf(" "); s=0;t=pos+1; //printf(" %d %d ",s,t); for(i=0;i<=t;i++) head[i]=-1; for(i=1;i<=n;i++) { int u,v; u=bin(le[i]); v=bin(ri[i]); //printf("%d %d ",u,v); addedge(u,v,-fee[i],1); } for(i=0;i<t;i++) addedge(i,i+1,0,k); ans=0; MCMF(); printf("%d ",-ans); } return 0; }