#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 0;
}

