#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("-1"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
#define bn(x) ((1<<x)-1)
#define newline puts("")
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair< int ,int > P;
typedef vector< int > vi;
typedef vector< vi> vvi;
typedef vector< ll> vl;
typedef vector< P> vp;
inline int in( ) { int x; scanf ( "%d" ,& x) ; return x; }
inline void priv( vi a) { rep( i,sz( a) ) printf ( "%d%c" ,a[ i] ,i== sz( a) - 1 ? '\n ' : ' ' ) ; }
template < typename T> istream& operator>> ( istream& i,vector< T> & v)
{ rep( j,sz( v) ) i>> v[ j] ; return i; }
template < typename T> string join( const vector< T> & v)
{ stringstream s; rep( i,sz( v) ) s<< ' ' << v[ i] ; return s.str ( ) .substr ( 1 ) ; }
template < typename T> ostream& operator<< ( ostream& o,const vector< T> & v)
{ if ( sz( v) ) o<< join( v) ; return o; }
template < typename T1,typename T2> istream& operator>> ( istream& i,pair< T1,T2> & v)
{ return i>> v.fi >> v.se ; }
template < typename T1,typename T2> ostream& operator<< ( ostream& o,const pair< T1,T2> & v)
{ return o<< v.fi << "," << v.se ; }
const int MX = 100005 , INF = 1001001001 ;
const ll LINF = 1e18 ;
const double eps = 1e-10 ;
// Max flow
// !! Be care of double and INF !!
struct Maxflow {
typedef int TT;
int n;
vi to, next, head, dist, it;
vector< TT> lim;
Maxflow( ) { }
Maxflow( int n) : n( n) ,head( n,- 1 ) ,it( n) { }
void add( int a, int b, TT c= 1 ) {
next.pb ( head[ a] ) ; head[ a] = sz( to) ; to.pb ( b) ; lim.pb ( c) ;
next.pb ( head[ b] ) ; head[ b] = sz( to) ; to.pb ( a) ; lim.pb ( 0 ) ;
}
void bfs( int sv) {
dist = vi( n,INF) ; // INF !!
queue< int > q;
dist[ sv] = 0 ; q.push ( sv) ;
while ( ! q.empty ( ) ) {
int v = q.front ( ) ; q.pop ( ) ;
for ( int i = head[ v] ; i ! = - 1 ; i = next[ i] ) {
if ( lim[ i] && dist[ to[ i] ] == INF) { // double INF !!
dist[ to[ i] ] = dist[ v] + 1 ; q.push ( to[ i] ) ;
}
}
}
}
TT dfs( int v, int tv, TT nf= INF) { // INF !!
if ( v == tv) return nf;
for ( ; it[ v] ! = - 1 ; it[ v] = next[ it[ v] ] ) {
int u = to[ it[ v] ] ; TT f;
if ( ! lim[ it[ v] ] || dist[ v] >= dist[ u] ) continue ;
if ( f = dfs( u, tv, min( nf, lim[ it[ v] ] ) ) , f) { // double !!
lim[ it[ v] ] - = f;
lim[ it[ v] ^ 1 ] + = f;
return f;
}
}
return 0 ;
}
int solve( int sv, int tv) {
TT flow = 0 , f;
while ( 1 ) {
bfs( sv) ;
if ( dist[ tv] == INF) return flow; // INF !!
rep( i,n) it[ i] = head[ i] ;
while ( f = dfs( sv,tv) , f) flow + = f;
}
}
} ;
//
int read( ) {
char c;
cin >> c;
if ( c == '-' ) return - 1 ;
if ( c == '*' ) return - 2 ;
return c- 'A' ;
}
const int hands[ ] = { 5 ,5 ,4 ,4 } ;
const int cards[ ] = { 6 ,6 ,9 } ;
int n;
vvi suggest;
vvi clue;
vi hand;
vvi can;
void solve( vi distribution) {
vi now;
rep( i,12 ) if ( distribution[ i] == 4 ) now.pb ( i) ;
if ( sz( now) ! = 2 ) return ;
vvi table( 5 ,vi( 9 ) ) ;
bool ok = true ;
auto add = [ & ] ( int i, int j, int x) {
if ( j < 12 ) {
if ( ( distribution[ j] == i) ! = ( x == 1 ) ) ok = false ;
return ;
}
j - = 12 ;
if ( table[ i] [ j] && table[ i] [ j] ! = x) {
ok = false ;
return ;
}
table[ i] [ j] = x;
} ;
rep( i,5 ) add( 0 ,hand[ i] ,1 ) ;
rep( i,n) {
rep( j,sz( clue[ i] ) ) {
if ( clue[ i] [ j] ! = - 1 ) break ;
int ni = ( i+ j+ 1 ) % 4 ;
rep( k,3 ) add( ni,suggest[ i] [ k] ,- 1 ) ;
}
int ni = ( i+ sz( clue[ i] ) ) % 4 ;
if ( clue[ i] .back ( ) == - 2 ) {
bool has = false ;
rep( k,2 ) {
if ( distribution[ suggest[ i] [ k] ] == ni) has = true ;
}
if ( ! has) add( ni,suggest[ i] [ 2 ] ,1 ) ;
} else if ( clue[ i] .back ( ) ! = - 1 ) {
add( ni,clue[ i] .back ( ) ,1 ) ;
}
}
if ( ! ok) return ;
rep( j,9 ) {
int cnt = 0 ;
rep( i,5 ) if ( table[ i] [ j] == 1 ) cnt++ ;
if ( cnt > 1 ) return ;
if ( cnt == 1 ) {
rep( i,5 ) if ( table[ i] [ j] ! = 1 ) table[ i] [ j] = - 1 ;
}
}
now.pb ( - 1 ) ;
for ( int room = 12 ; room < 21 ; ++ room) {
if ( table[ 4 ] [ room- 12 ] == - 1 ) continue ;
int sv = 4 + 9 , tv = sv+ 1 ;
Maxflow g( tv+ 1 ) ;
rep( i,4 ) rep( j,9 ) {
if ( table[ i] [ j] ! = - 1 ) g.add ( i,j+ 4 ) ;
}
vi cnt( 5 ) ;
rep( j,12 ) cnt[ distribution[ j] ] ++ ;
rep( i,4 ) g.add ( sv,i,hands[ i] - cnt[ i] ) ;
rep( j,9 ) if ( room ! = j+ 12 ) g.add ( j+ 4 ,tv) ;
now.back ( ) = room;
if ( g.solve ( sv,tv) == 9 - 1 ) {
can.pb ( now) ;
}
}
}
bool fineRemoved( vi distribution) {
rep( i,2 ) {
int cnt = 0 , cnt2 = 0 ;
for ( int j = 6 * i; j < 6 * i+ 6 ; ++ j) {
if ( j < sz( distribution) && distribution[ j] == 4 ) cnt++ , cnt2++ ;
if ( j >= sz( distribution) ) cnt2++ ;
}
if ( cnt > 1 ) return false ;
if ( ! cnt2) return false ;
}
return true ;
}
bool fineHands( vi distribution) {
vi cnt( 5 ) ;
rep( i,sz( distribution) ) cnt[ distribution[ i] ] ++ ;
rep( i,4 ) if ( cnt[ i] > hands[ i] ) return false ;
return true ;
}
void dfs( vi distribution) {
if ( ! fineRemoved( distribution) ) return ;
if ( ! fineHands( distribution) ) return ;
if ( sz( distribution) == 12 ) {
solve( distribution) ;
return ;
}
distribution.pb ( 0 ) ;
if ( find( rng( hand) ,sz( distribution) - 1 ) ! = hand.end ( ) ) {
dfs( distribution) ;
} else {
rep( i,4 ) {
distribution.back ( ) ++ ;
dfs( distribution) ;
}
}
}
int main( ) {
cin >> n;
hand = vi( 5 ) ;
rep( i,5 ) hand[ i] = read( ) ;
suggest = vvi( n,vi( 3 ) ) ;
clue = vvi( n) ;
rep( i,n) {
rep( j,3 ) suggest[ i] [ j] = read( ) ;
rep( j,3 ) {
clue[ i] .pb ( read( ) ) ;
if ( clue[ i] .back ( ) ! = - 1 ) break ;
}
}
dfs( vi( ) ) ;
string ans;
rep( i,3 ) {
vi a;
rep( j,sz( can) ) a.pb ( can[ j] [ i] ) ;
assert ( sz( a) ) ;
sort( rng( a) ) ; uni( a) ;
if ( sz( a) == 1 ) ans + = 'A' + a[ 0 ] ;
else ans + = '?' ;
}
cout << ans<< endl;
return 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KICAgICAgcmV0dXJuOwogICAgfQogICAgdGFibGVbaV1bal0gPSB4OwogIH07CgogIHJlcChpLDUpIGFkZCgwLGhhbmRbaV0sMSk7CgogIHJlcChpLG4pIHsKICAgIHJlcChqLHN6KGNsdWVbaV0pKSB7CiAgICAgIGlmIChjbHVlW2ldW2pdICE9IC0xKSBicmVhazsKICAgICAgaW50IG5pID0gKGkraisxKSU0OwogICAgICByZXAoaywzKSBhZGQobmksc3VnZ2VzdFtpXVtrXSwtMSk7CiAgICB9CgogICAgaW50IG5pID0gKGkrc3ooY2x1ZVtpXSkpJTQ7CiAgICBpZiAoY2x1ZVtpXS5iYWNrKCkgPT0gLTIpIHsKICAgICAgYm9vbCBoYXMgPSBmYWxzZTsKICAgICAgcmVwKGssMikgewogICAgICAgIGlmIChkaXN0cmlidXRpb25bc3VnZ2VzdFtpXVtrXV0gPT0gbmkpIGhhcyA9IHRydWU7CiAgICAgIH0KICAgICAgaWYgKCFoYXMpIGFkZChuaSxzdWdnZXN0W2ldWzJdLDEpOwogICAgfSBlbHNlIGlmIChjbHVlW2ldLmJhY2soKSAhPSAtMSkgewogICAgICBhZGQobmksY2x1ZVtpXS5iYWNrKCksMSk7CiAgICB9CiAgfQoKICBpZiAoIW9rKSByZXR1cm47CgogIHJlcChqLDkpIHsKICAgIGludCBjbnQgPSAwOwogICAgcmVwKGksNSkgaWYgKHRhYmxlW2ldW2pdID09IDEpIGNudCsrOwogICAgaWYgKGNudCA+IDEpIHJldHVybjsKICAgIGlmIChjbnQgPT0gMSkgewogICAgICByZXAoaSw1KSBpZiAodGFibGVbaV1bal0gIT0gMSkgdGFibGVbaV1bal0gPSAtMTsKICAgIH0KICB9CgogIG5vdy5wYigtMSk7CiAgZm9yIChpbnQgcm9vbSA9IDEyOyByb29tIDwgMjE7ICsrcm9vbSkgewogICAgaWYgKHRhYmxlWzRdW3Jvb20tMTJdID09IC0xKSBjb250aW51ZTsKICAgIGludCBzdiA9IDQrOSwgdHYgPSBzdisxOwogIC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