枚举Mi,算对应的P,取最大概率大于1-P
#include<bits/stdc++.h>
using namespace std;
#define sci(num) scanf("%d",&num)
#define scl(num) scanf("%lld",&num)
#define mem(a,b) memset(a,b,sizeof a)
#define cpy(a,b) memcopy(a,b,sizeof b)
typedef long long LL;
typedef pair<int,int> pair;
const int MAX_N = 100010;
double dp[MAX_N];
double P;
int N;
double Ps[MAX_N];
int Ms[MAX_N];
int main() {
int cases;
sci(cases);
while (cases -- ) {
scanf("%lf%d",&P,&N);
int sum = 0;
for (int i = 1;i <= N;i++) {
scanf("%d%lf",&Ms[i],&Ps[i]);
Ps[i] = 1 - Ps[i];
sum += Ms[i];
}
dp[0] = 1.0;
for (int i = 1;i < MAX_N;i++)
dp[i] = 0.0;
for (int i = 1;i <= N;i++) {
for (int j = sum;j >= Ms[i];j--) {
dp[j] = max(dp[j - Ms[i]] * Ps[i],dp[j]);
}
}
for (int j = MAX_N - 1;j >= 0;j --) {
if (dp[j] > (1 - P)) {
printf("%d\n",j);
break;
}
}
}
}