#include <iostream>
#include <string>
using namespace std;

int next_turn(char c) {
  return int(c) - int('a');
}

int main()
{
  string S[3];
  cin >> S[0] >> S[1] >> S[2];
  int pos[3] = {0};
  int turn = 0;
  for (;;) {
    if (pos[turn] >= S[turn].length()) {
      cout << char(int('A')+turn) << endl;
      break;
    }
    int next = next_turn(S[turn][pos[turn]]);
    pos[turn]++;
    turn = next;
  }
  return 0;
}

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