题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4035
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
const int maxn =1e2+5;
const int mod=9999991;
///const int ub=1e6;
///const double e=2.71828;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
数学不等式
a>b
c>d
ac+bd-ad-bc=(a-b)*(c-d)>0
ac+bd>ad+bc
*/
struct node
{
char s[100];
ll v;
bool operator<(const node&y ) const
{
if(v==y.v)
{
int flag=strcmp(s,y.s);
return flag<0;
}
return v>y.v;
}
}p[maxn];
int n,m;
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",p[i].s);
scanf("%lld",&p[i].v);
}
sort(p,p+n);
ll ans=0;for(int i=0;i<m;i++) ans+=1LL*(m-i)*p[i].v;
printf("%lld ",ans);
for(int i=0;i<m;i++) printf("%s%c",p[i].s,(i!=m-1)?' ':'\n');
}
return 0;
}