#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e6;
int ne[N];
int main() {
    int a, b, n;
    while(cin >> a >> b >> n)
    {
        int a1 = a, b1 = b;
        int i, j;
        char c;
        while(n -- && cin >> i >> c >> j)
             ne[i] = j;
        //拼接链表,最终一定能到达-1或者公共点
	    while(a != b)
        {
            if(a == -1) a = b1;
            if(b == -1) b = a1;
            a = ne[a];
            b = ne[b];
        }
        cout << a << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")