#include<stdio.h>
#define inf 0x7fffffff
int max(int a,int b) {
return a>b?a:b;
}
typedef struct {
int dead,time;
char s[200];
}H;
typedef struct {
int val,last,time;
}F;
F dp[1<<16];
H a[20];
int ans[20];
int main() {
int i,j,T,n,m;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%d%d",a[i].s,&a[i].dead ,&a[i].time );
for(i=0;i<(1<<n);i++) {
dp[i].last=-1;
dp[i].val=inf;
}
dp[0].last=-1;
dp[0].val=0;
dp[0].time=0;
for(i=0;i<(1<<n);i++)
for(j=0;j<n;j++) {
if((i&(1<<j))!=0)
continue;
int p=(i|(1<<j));
dp[p].time=dp[i].time+a[j].time;
if(dp[p].val>dp[i].val+max(dp[p].time-a[j].dead,0)) {
dp[p].val=dp[i].val+max(dp[p].time-a[j].dead,0);
dp[p].last=j;
}
}
printf("%d\n",dp[(1<<n)-1].val);
int t=(1<<n)-1;
int k=n-1;
while(dp[t].last!=-1) {
ans[k--]=dp[t].last;
t=(t&(~(1<<dp[t].last)));
}
for(i=0;i<n;i++)
printf("%s\n",a[ans[i]].s);
}
return 0;
}