#include #include #include #include #include #define ls (u<<1) #define rs (u<<1|1) #define maxn 1010 #define ll long long #define INF 1e9 using namespace std; #define max(a,b) (a)>(b)?(a):(b) #define min(a,b) (a)<(b)?(a):(b) int dp[maxn],v[maxn],w[maxn],vis[maxn][maxn],d[maxn]; int a[maxn][120]; int check(int n,int m){ int t1=a[n][0]; int t2=a[m][0]; for(int i=1,j=1;i<=t1&&j<=t2;i++,j++){ if(a[n][i]!=a[m][j]) return a[n][i]-a[m][j]; } return 0; } int main(){ int T,t = 1; scanf("%d",&T); while(T--){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d",&w[i],&v[i]); } int sum = 0; memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); memset(d,0,sizeof(d)); for(int i=1;i<=m;i++){ for(int j=n;j>=v[i];j--){ if(dp[j-v[i]]+w[i] > dp[j]){ dp[j] = dp[j-v[i]]+w[i]; vis[i][j] = 1; if(dp[j] > sum){ sum = dp[j]; } } } } ll minm=INF; int k=0,mark=0; for(int p=n;p>=0;p--){ if(dp[p]==sum){ ll ans=0; int i=m,j=p,top = 1; while(i>=1&&j>=0){ if(vis[i][j]){ a[k][top++]=i; ans+=i; j-=v[i]; } i--; } a[k][0]=top-1; sort(a[k]+1,a[k]+1+a[k][0]); if(minm>sum){ minm=sum; mark=k; } else if(minm==sum&&check(k,mark)<0){ minm=sum; mark=k; } k++; } } ll val=0,cost=0; int top=a[mark][0]; sort(a[mark]+1,a[mark]+1+a[mark][0]); for(int i=1;i<=top;i++){ val+=w[a[mark][i]]; cost+=v[a[mark][i]]; } printf("Case #%d:\n",t++); printf("%lld %lld\n",val,cost); for(int i=1;i=1) printf("%d\n",a[mark][top]); } return 0; }