#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int t, n, i, j, temp, v[105];
double c, dp[10005], w[105];
scanf("%d", &t);
while(t--)
{
int sum=0;
scanf("%lf%d", &c, &n);
c=1-c;
for(i=0;i<n;i++)
{
scanf("%d%lf", &v[i], &w[i]);
w[i]=1-w[i];
sum+=v[i];
}
memset(dp, 0, sizeof(dp));
dp[0]=1;
for(i=0;i<n;i++)
for(j=sum;j>=v[i];j--)
dp[j]=max(dp[j], dp[j-v[i]]*w[i]);
for(i=sum;i>=0;i--)
if(dp[i]>=c)
break;
printf("%d\n", i);
}
return 0;
}
08-25
08-25