#include
#include
#include
//#define max(a,b) (a)>(b)?(a):(b)
using namespace std;
int main()
{
int t,m[110],n;
double f[11100];
double p,pi[110];
scanf("%d",&t);
while(t--)
{
memset(f,0,sizeof(f));
scanf("%lf%d",&p,&n);
int i;
int sum = 0;
//printf("%d \n",v);
for(i=1;i<=n;i++){
scanf("%d%lf",&m[i],&pi[i]);
// printf(" %d \n",h[i]);
sum += m[i];
}
for(i=1;i<=sum;i++) f[i] = 1;
f[0] = 0;
for(i=1;i<=n;i++)
for(int j=sum;j>=m[i];j--)
f[j]=min(f[j],1-(1-f[j-m[i]])*(1-pi[i]));
for(i=sum;i>=0;i--)
{
if(f[i]<=p)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}