#include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define M 1000000007
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define f first
#define s second
#define b begin
#define e end
#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for(i=a;i<b;i++)
#define RFOR(i,a,b) for(i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define itfr(it,x) for(it=x.begin();it!=x.end();it++)
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

int main()
{
    flash;
    //sieve();
    ll T=1,n,m,k,x,y,z;
    // cin>>T;
    while(T--)
    {
        string a,b,c;
        cin>>a>>b>>c;
        n = a.length();
        m = b.length();
        k = c.length();
        x = 0; y = 0; z = 0;
        char cur = 'a';
        while(x<=n&&y<=m&&z<=k)
        {
            if(cur=='a')
            {
                cur = a[x];
                x++;
            }
            else if(cur=='b')
            {
                cur = b[y];
                y++;
            }
            else
            {
                cur = c[z];
                z++;
            }
        }
        if(x>n)
            cout<<'A';
        else if(y>m)
            cout<<'B';
        else
            cout<<'C';
        cout<<endl;
    }
}

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
#define MAX_V 100
#define INF LONG_MAX / 4
using namespace std;

typedef struct {
  int to, cost;
} edge;

vector<edge> G[MAX_V];
long long d[MAX_V][MAX_V];

bool find_negative_cycle(int v) {
  for (int i = 0; i < v; i++) {
    if (d[i][i] < 0) return true;
  }
  return false;
}

void warshall_floyd(int v) {
  for (int k = 0; k < v; k++) {
    for (int i = 0; i < v; i++) {
      if (d[i][k] == INF) continue;
      for (int j = 0; j < v; j++) {
	if (d[k][j] == INF) continue;
	d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

void init(int v) {
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < v; j++) {
      if (i == j) d[i][j] = 0;
      else d[i][j] = INF;
    }
  }
  
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      d[i][G[i][j].to] = G[i][j].cost;
    }
  }
}

int main() {
  int v, e;
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    edge temp;
    int s, t, d;
    cin >> s >> t >> d;
    temp.to = t;
    temp.cost = d;
    G[s].push_back(temp);
  }

  init(v);
  warshall_floyd(v);

  if (find_negative_cycle(v) == true) {
    cout << "NEGATIVE CYCLE" << endl;
    return 0;
  }
  
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < v; j++) {
      if (INF - 20000000 <= d[i][j] && d[i][j] <= INF) {
	cout << "INF";
      } else {
	cout << d[i][j];
      }
      if (j < v - 1) {
	cout << ' ';
      } else {
	cout << endl;
      }
    }
  }

  return 0;
}