#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
map<string,int> artistmap;
typedef array<char,4> state;
unordered_map<ull,char> from[2][100];
vector<state> states[2][100];
vi adj[2][100];
int a[100];
ull getid(const state& s) {
    ull res=0;
    for(int i=0;i<4;++i) res|=((ull)s[i])<<(i*8);
    return res;
}
void dfs(state s, int at, int d=0, bool rev=false) {
    if(d==4) return;
    s[0] = a[at];
    sort(all(s));
    auto id = getid(s);
    for(int to : adj[rev][at]) {
        if(find(all(s),a[to])==s.end()) {
            if(!from[rev][to].count(id)) {
                if(d==3) states[rev][to].push_back(s);
                from[rev][to][id]=at;
                dfs(s,to,d+1,rev);
            }
        }
    }
}
void reconstruct(state s, int at, bool rev=false) {
    vi ans;
    for(int i=0;i<4;++i) {
        int fr = from[rev][at][getid(s)];
        for(int j=0;j<4;++j) if(s[j]==a[fr]) s[j]=0;
        sort(all(s));
        ans.push_back(fr+1);
        at=fr;
    }
    if(!rev) reverse(all(ans));
    cout << ans;
}
int main() {
    int n; cin >> n;
    for(int i=0;i<n;++i) {
        string s; cin >> s;
        if(!artistmap.count(s)) artistmap[s]=artistmap.size()+1;
        a[i]=artistmap[s];
        int k; cin >> k;
        for(int j=0;j<k;++j) {
            int to; cin >> to,to--;
            if(a[to]!=a[i]) {
                adj[0][i].push_back(to);
                adj[1][to].push_back(i);
            }
        } 
    }
    for(int i=0;i<n;++i) {
        dfs({}, i);
        dfs({},i,0,true);
    }
    vi order(n); iota(all(order),0);
    random_shuffle(all(order));
    for(int i:order) {
        unordered_map<ull,int> cnt;
        int total=states[0][i].size();
        cnt.reserve(total*3);
        for(auto s : states[0][i]) {
            for(int j=0;j<16;++j) {
                auto ss = s;
                for(int k=0;k<4;++k) if(j&(1<<k)) {
                    ss[k]=0;
                }
                sort(all(ss));
                cnt[getid(ss)]++;
            }
        }
        for(auto s : states[1][i]) {
            int cur = total;
            for(int j=0;j<15;++j) {
                auto ss = s;
                int odd=0;
                for(int k=0;k<4;++k) if(j&(1<<k)) {
                    ss[k]=0;
                    odd^=1;
                }
                sort(all(ss));
                // inclusion exclusion
                cur+=(1-2*odd)*cnt[getid(ss)];
            }
            if(cur) {
                for(auto t : states[0][i]) {
                    bool found=true;
                    for(auto c : s) for(auto cc : t) if(c==cc) {
                        found=false;
                        break;
                    }
                    if(!found) continue;
                    reconstruct(t,i);
                    cout << ' ' << i+1 << ' ';
                    reconstruct(s,i,true);
                    cout << '\n';
                    exit(0);
                }
                assert(false);
            }

        }
    }
    cout << "fail\n";
}