#include<stdio.h>
#define maxn 110
#define min(a,b) (a)>(b)?(b):(a)
double a[maxn];
int v[maxn];
double f[maxn][maxn*maxn];
int main()
{
int T, cas = 1;
int n;
double p;
int sum;
scanf("%d", &T);
while (T--)
{
sum = 0;
scanf("%lf%d", &p, &n);
for (int i = 1; i <= n; i++)
scanf("%d%lf", &v[i], &a[i]), sum += v[i];
for (int i = 1; i <= sum; i++)
f[0][i] = -1;
f[0][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= sum; j++)
{
if (j - v[i]<0 || f[i - 1][j - v[i]]<-0.5)
f[i][j] = f[i - 1][j];
else
if(f[i - 1][j]<0)
f[i][j] = f[i - 1][j - v[i]] + (1 - f[i - 1][j - v[i]])*a[i];
else
f[i][j] = min(f[i - 1][j], f[i - 1][j - v[i]] + (1 - f[i - 1][j - v[i]])*a[i]);
}
int ans = 0;
for (int i = 0; i <= sum; i++)
if (f[n][i]>-0.5 && f[n][i]<p)
ans = i;
printf("Case %d: %d\n", cas++, ans);
}
return 0;
}