#include #define Maxn 1007 #define inf 200000007 using namespace std; int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int T,n,s; int a[Maxn],b[Maxn]; pair f[1007][107]; int last[1007][107]; int ans[107],cnt,t; int main() { int T=read(); for (int l=1;l<=T;l++) { s=read(),n=read(); for (int i=1;i<=n;i++) a[i]=read(),b[i]=read(); memset(f,0,sizeof(f)); memset(last,0,sizeof(last)); for (int j=n;j;j--) for (int i=s;i>=0;i--) { if (i>=b[j]&&make_pair(f[i-b[j]][j+1].first+a[j],f[i-b[j]][j+1].second-j)>f[i][j]) { f[i][j]=make_pair(f[i-b[j]][j+1].first+a[j],f[i-b[j]][j+1].second-j); last[i][j]=j; } if (f[i][j+1]>f[i][j]) { f[i][j]=f[i][j+1]; last[i][j]=last[i][j+1]; } } cnt=0,t=0; int now=s,r=1; while (last[now][r]>0) { ans[++t]=last[now][r]; cnt+=b[last[now][r]]; int now1=now-b[last[now][r]]; r=last[now][r]+1; now=now1; } printf("Case #%d:\n",l); printf("%d %d\n",f[s][1].first,cnt); if (f[s][1].first>0) { for (int i=1;i