#include <bits/stdc++.h>
using namespace std;

int main(){
    vector<queue<char>> s(3);
    for(int ix=0;ix<3;ix++){
        string tmp;
        cin >> tmp;
        for(int ixx=0;ixx<tmp.size();ixx++) s.at(ix).push(tmp.at(ixx));
    }
    char card;
    int turnplayer = 0;
    while(!s.at(turnplayer).empty()){
        card = s.at(turnplayer).front();
        s.at(turnplayer).pop();
        turnplayer = card - 'a';
    }
    if(turnplayer == 0) cout << "A" << endl;
    if(turnplayer == 1) cout << "B" << endl;
    if(turnplayer == 2) cout << "C" << endl;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
  string sa,sb,sc;
  cin>>sa>>sb>>sc;
  reverse(sa.begin(),sa.end());
  reverse(sb.begin(),sb.end());
  reverse(sc.begin(),sc.end());
  queue<char> card;
  card.push(sa[sa.size()-1]);
  sa.pop_back();
  char ans;
  while(true){
    char now=card.front();
    card.pop();
    if(now=='a'){
      if(sa.empty()) {ans='A'; break;}
      card.push(sa[sa.size()-1]);
      sa.pop_back();
    }
    if(now=='b'){
      if(sb.empty()) {ans='B'; break;}
      card.push(sb[sb.size()-1]);
      sb.pop_back();
    }
    if(now=='c'){
      if(sc.empty()) {ans='C'; break;}
      card.push(sc[sc.size()-1]);
      sc.pop_back();
    }
  }
  cout<<ans<<endl;
}

