include <stdio.h>

include <string.h>

struct Info1
{
double hash[300];
double tot;
int flag;
};

struct Info1 fapiao[35];

double maxi=-1;
double Q;

void DFS(int i,double sum,int n)
{
if(i==n)
{
if(sum>maxi)
{
maxi=sum;
}
return;
}
DFS(i+1,sum,n);
if(sum+fapiao[i].tot<=Q&&fapiao[i].flag==1)
{
DFS(i+1,sum+fapiao[i].tot,n);
}

}

int main()
{
int N;
int i,j;
char str[50];
char ch;
while(scanf("%lf %d",&Q,&N)!=EOF&&N)
{
int m;
for(i=0;i<N;i++)
{
fapiao[i].flag=1;
fapiao[i].tot=0;
memset(fapiao[i].hash,0,sizeof(fapiao[i].hash));
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%s",str);
double temp;
sscanf(str,"%c:%lf",&ch,&temp);
fapiao[i].hash[ch]+=temp;
fapiao[i].tot+=temp;
if(fapiao[i].hash[ch]>600) fapiao[i].flag=0;
if(ch!='A'&&ch!='B'&&ch!='C')
{
fapiao[i].flag=0;
}
}
if((int)fapiao[i].tot>1000) fapiao[i].flag=0;
}
DFS(0,0,N);
printf("%.2f\n",maxi);
}
return 0;
}