b[9999];m,i;c(int*x){i=*x-*1[&x];}main(a){for(;scanf("%*d%d%d",&m,&a)*m;printf("%d\n",a)){for(i=m*=3;i;scanf("%d%d%d",b+i,b-~i,b+i+2))i-=3;for(qsort(b,m/3,12,c);m;a=a-b[m+1]?a-b[m+2]?a:b[m+1]:b[m+2])m-=3;}}