#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef unsigned long long uLL; const int maxn=100000+10; const int maxm=1000+10; const int mod=(int)1e9+7; const int INF=0x3f3f3f3f; const LL LINF=(1LL<<60); const double PI=acos(-1.0); #define mst(a,b) memset(a,b,sizeof(a)) #define rep(i,a,b) for (int i=(a);i #define X first #define Y second struct node{ bool a[110]; int c,k; node(){ c=k=0; mst(a,0); } }; node dp[110][1100]; int main(){ #ifdef local freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif ios::sync_with_stdio(false); cin.tie(0); int T,cas=1; cin>>T; while (T--){ int w[110],v[110]; int b,n; cin>>b>>n; for (int i=1;i<=n;i++) cin>>v[i]>>w[i]; for (int i=1;i<=n;i++){ for (int j=0;j<=b;j++){ dp[i][j]=dp[i-1][j]; if (j-w[i]>=0){ if (dp[i-1][j-w[i]].c+v[i]>dp[i][j].c){ dp[i][j]=dp[i-1][j-w[i]]; dp[i][j].c+=v[i]; dp[i][j].k+=i; dp[i][j].a[i]=true; } else if (dp[i-1][j-w[i]].c+v[i]==dp[i][j].c){ if (dp[i-1][j-w[i]].k+i