Language: C++

#include<iostream>
#include<map>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define pb         push_back
#define ALL(C)     (C).begin(),(C).end()

const int CONST = 12;

bool have_same(vector<int> &a){
  rep(i,a.size())REP(j,i+1,a.size())if (a[i] == a[j])return true;
  return false;
}

void generate(vector<string> & a,vector<vector<int> > &b){
  vector<int> base[5];
  string    opdata[5]={"","7","M7","m","m7"};
  base[0].pb(0);base[0].pb(4);base[0].pb(7);
  base[1].pb(0);base[1].pb(4);base[1].pb(7);base[1].pb(10);
  base[2].pb(0);base[2].pb(4);base[2].pb(7);base[2].pb(11);
  base[3].pb(0);base[3].pb(3);base[3].pb(7);
  base[4].pb(0);base[4].pb(3);base[4].pb(7);base[4].pb(10);

  vector<int> pat;

  string option="";
  string ori[]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  string add[]={"-9","9","+9","-11","11","+11","-13","13","+13"};
  int   addp[]={1,2,3,4,5,6,8,9,10};
  string   h[]={"-5","+5"};
  //  int     hp[]={11,1};
  int     hp[]={11,1};
  

  rep(k,5){
    pat=base[k];
    option=opdata[k];
    rep(i,12){      
      vector<int> cpy=pat;
      rep(j,pat.size())cpy[j]=(cpy[j]+i)%CONST;
      a.pb(ori[i]+option);
      b.pb(cpy);
      vector<int> cpy2=cpy;
      cpy2.pb(-1);
      rep(j,9){
	cpy2[cpy2.size()-1]=(i+addp[j])%CONST;
	if (have_same(cpy2))continue;
	a.pb(ori[i]+option+"("+add[j]+")");
	b.pb(cpy2);
      }
      
      int base=cpy[cpy.size()-1];
      rep(j,2){
	//cpy[cpy.size()-1]=(base+hp[j])%CONST;
	cpy[2]=(base+hp[j])%CONST;
	a.pb(ori[i]+option+"("+h[j]+")");
	b.pb(cpy);
      }
    }
  }
   
  rep(i,a.size()){
    //    cout << a[i] << " ";// << b[i].size() << endl;
    //    rep(j,b[i].size())cout << b[i][j]<<" ";cout <<endl;
    sort(ALL(b[i]));
  }
}


bool  is_same_vector(vector<int> &a,vector<int> &b){
  if (a.size() != b.size())return false;
  rep(i,a.size())if (a[i] != b[i])return false;
  /*  
  cout << endl;
  cout <<"tete"<<endl;
  rep(i,a.size())cout << a[i];cout << endl;
  rep(i,b.size())cout << b[i];cout << endl;
  */  
  return true;
}

main(){
  vector<string> a;
  vector<vector<int> > b;
  generate(a,b);
  string ori[]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  map<string,int> M;
  rep(i,12)M[ori[i]]=i;
  
  int te;
  cin>>te;
  while(te--){
    int cnt = 0;
    int n;
    cin>>n;
    vector<int> in;
    rep(i,n){
      string tmp;cin>>tmp;in.pb(M[tmp]);
    }
    sort(ALL(in));
    
    //    rep(k,in.size()){
      rep(i,a.size()){
	if (is_same_vector(b[i],in)){
	  if (cnt)cout << " ";
	  cout << a[i];
	  cnt++;
	}
      }
      /*      
      int tmp = in[0];
      rep(i,in.size()-1)in[i]=in[i+1];
      in[in.size()-1]=tmp;
    }
      */
    if (cnt == 0)cout << "UNKNOWN";
    cout << endl;
  }
  return false;
}

Compiler output:
a.cc: In function 'void generate(std::vector<std::__cxx11::basic_string<char> >&, std::vector<std::vector<int> >&)':
a.cc:67:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   67 |     sort(ALL(b[i]));
      |     ^~~~
      |     short
a.cc: At global scope:
a.cc:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   84 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:102:5: error: 'sort' was not declared in this scope; did you mean 'short'?
  102 |     sort(ALL(in));
      |     ^~~~
      |     short