RT
#include<iostream>
using namespace std;
int n,a[50],t,ans;
void clear_(){
ans=1e9+7;
for(int i=0;i<30;i++)a[i]=0;
}
bool fsh(){
for(int i=0;i<30;i++){
if(a[i]!=0)return 0;
}
return 1;
}
void dfs(int st){
if(st>ans){
//cout<<st<<" "<<ans;
return;
}
if(fsh())ans=st;
st++;
int k=0;
for(int i=3;i<=15;i++){//单顺子
k++;
if(!a[i]){
if(k>=5){
k--;
for(int j=i-k;j<=i-4;j++){
for(int l=4;l<=k;l++){
if(j+l>i)break;
for(int x=j;x<=j+l;x++){
a[x]--;
}
dfs(st);
for(int x=j;x<=j+l;x++){
a[x]++;
}
}
}
}
k=0;
}
}
k=0;
for(int i=3;i<=15;i++){//双顺子
k++;
if(a[i]<2){
if(k>=3){
k--;
for(int j=i-k;j<=i-2;j++){
for(int l=2;l<=k;l++){
if(j+l>i)break;
for(int x=j;x<=j+l;x++){
a[x]-=2;
}
dfs(st);
for(int x=j;x<=j+l;x++){
a[x]+=2;
}
}
}
}
k=0;
}
}
k=0;
for(int i=3;i<=15;i++){//三顺子
k++;
if(a[i]<3){
if(k>=2){
k--;
for(int j=i-k;j<=i-1;j++){
for(int l=1;l<=k;l++){
if(j+l>i)break;
for(int x=j;x<=j+l;x++){
a[x]-=3;
}
dfs(st);
for(int x=j;x<=j+l;x++){
a[x]+=3;
}
}
}
}
k=0;
}
}
k=0;
for(int i=2;i<=14;i++){//四带二
if(a[i]==4){
a[i]-=4;
for(int j=2;j<=16;j++){
if(a[j]){
a[j]--;
dfs(st);
a[j]++;
}
}
a[i]+=4;
}
}
for(int i=2;i<=14;i++){//四带二对
if(a[i]==4){
a[i]-=4;
for(int j=2;j<=14;j++){
if(a[j]>=2){
a[j]-=2;
dfs(st);
a[j]+=2;
}
}
a[i]+=4;
}
}
for(int i=2;i<=14;i++){//三带二
if(a[i]>=3){
a[i]-=3;
for(int j=2;j<=14;j++){
if(a[j]>=2){
a[j]-=2;
dfs(st);
a[j]+=2;
}
}
a[i]+=3;
}
}
for(int i=2;i<=14;i++){//三带一
if(a[i]>=3){
a[i]-=3;
for(int j=2;j<=16;j++){
if(a[j]){
a[j]--;
dfs(st);
a[j]++;
}
}
a[i]+=3;
}
}
// if(a[16]==2){//火箭
// a[16]=0;
// dfs(st);
// a[16]=2;
// }
for(int i=2;i<=14;i++){//炸弹
if(a[i]==4){
a[i]-=4;
dfs(st);
a[i]+=4;
}
}
for(int i=2;i<=14;i++){//三牌
if(a[i]>=3){
a[i]-=3;
dfs(st);
a[i]+=3;
}
}
for(int i=2;i<=16;i++){//对牌
if(a[i]>=2){
a[i]-=2;//若可以减两次,则炸弹更优
dfs(st);
a[i]+=2;
}
}
for(int i=2;i<=16;i++){//单牌
if(a[i]){
a[i]--;//同上
dfs(st);
a[i]++;
}
}
}
int main(){
cin>>t>>n;
while(t--){
clear_();
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
if(x==0){
a[16]++;
}
else if(x==1){
a[14]++;
}
else a[x]++;
}
dfs(0);
cout<<ans<<endl;
}
}