#include <iostream>
#include <cstring>
usingnamespacestd;intT,N,M;chararr[50][101];charstr[10][8]={"0001101","0011001","0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011"};inthashtable[10000];unsignedlong_hash(constchar*str){unsignedlonghash=5381;intc;while(c=*str++){hash=(((hash<<5)+hash)+c)%10000;}returnhash%10000;}typedefstructTrie{boolisleaf;intval;Trie*child[2];};Trienodes[100];intmcount;Trieroot;Trie*new_node(){return&nodes[mcount++];}voidinsert(Trie*ptr,char*str,intv){if(*str=='\0'){ptr->isleaf=true;ptr->val=v;}else{if(ptr->child[*str-'0']=='\0')ptr->child[*str-'0']=new_node();insert(ptr->child[*str-'0'],str+1,v);}}int_find(Trie*ptr,char*str){if(ptr->isleaf==true)returnptr->val;if(ptr->child[*str-'0']=='\0')return-1;return_find(ptr->child[*str-'0'],str+1);}voidinit_trie(){for(inti=0;i<10;i++){insert(&root,str[i],i);}}intmain(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);init_trie();cin>>T;intkey;for(inti=1;i<=T;i++){memset(hashtable,0,sizeof(hashtable));intsol=0;cin>>N>>M;for(inti=0;i<N;i++){cin>>arr[i];key=_hash(arr[i]);if(hashtable[key]==1){i--;N--;}elsehashtable[key]=1;}for(inti=0;i<N;i++){// 각 행에 대해서 암호코드 찾기 시작intcipher[8],ctop=0;for(intpadding=0;padding<7;padding++){for(intindex=padding;index<=strlen(arr[i])-7;index+=7){if(ctop==8){padding=8;break;}inttemp=_find(&root,arr[i]+index);if(temp==-1){ctop=0;}else{cipher[ctop++]=temp;}}}if(ctop==8){intchecksum=(cipher[0]+cipher[2]+cipher[4]+cipher[6])*3+(cipher[1]+cipher[3]+cipher[5])+cipher[7];if(checksum%10==0){for(inti=0;i<8;i++)sol+=cipher[i];break;}}}cout<<"#"<<i<<" "<<sol<<endl;}}