// ==============================================
// Library
// ==============================================
#define _CRT_SECURE_NO_WARNINGS

// When you are using the gcc compiler, you need only the following lines.
// #include <bits/stdc++.h>

// When you are using other compilers, you need the following line instead.
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>

using namespace std;

// competitive macro
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i ,n) for (int i = (n) - 1; i >= 0; i--)
#define LL long long
#define ULL unsigned long long

// If you use this function, "cin" speeds up.
// However, you can not mix "cin" and "scanf", "cout" and "printf".
void fastIOS()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
}
// ==============================================
// ==============================================

int main()
{
	fastIOS();

	string sa, sb, sc;
	cin >> sa;
	cin >> sb;
	cin >> sc;

	int saSize = sa.size();
	int sbSize = sb.size();
	int scSize = sc.size();

	bool faWin = false;
	bool fbWin = false;
	bool fcWin = false;
	string schPlayer = "A"; // A:Player A, B:Player B, C:Player C
	bool fNonFinish = true;

	int posSA = 0;
	int posSB = 0;
	int posSC = 0;

	while (fNonFinish)
	{
		if (schPlayer == "A")
		{
			if (posSA == saSize)
			{
				faWin = true;
				fNonFinish = false;
			}
			else if (sa[posSA] == 'a')
			{
				posSA++;
				schPlayer = "A";
			}
			else if (sa[posSA] == 'b')
			{
				posSA++;
				schPlayer = "B";
			}
			else if (sa[posSA] == 'c')
			{
				posSA++;
				schPlayer = "C";
			}
		}
		else if (schPlayer == "B")
		{
			if (posSB == sbSize)
			{
				fbWin = true;
				fNonFinish = false;
			}
			else if (sb[posSB] == 'a')
			{
				posSB++;
				schPlayer = "A";
			}
			else if (sb[posSB] == 'b')
			{
				posSB++;
				schPlayer = "B";
			}
			else if (sb[posSB] == 'c')
			{
				posSB++;
				schPlayer = "C";
			}
		}
		else if (schPlayer == "C")
		{
			if (posSC == scSize)
			{
				fcWin = true;
				fNonFinish = false;
			}
			else if (sc[posSC] == 'a')
			{
				posSC++;
				schPlayer = "A";
			}
			else if (sc[posSC] == 'b')
			{
				posSC++;
				schPlayer = "B";
			}
			else if (sc[posSC] == 'c')
			{
				posSC++;
				schPlayer = "C";
			}
		}
	}

	if (faWin)
		cout << "A" << "\n";
	else if (fbWin)
		cout << "B" << "\n";
	else if (fcWin)
		cout << "C" << "\n";
}

Measured memory_bytes: 256