//In the Name of God //Ya Ali #include #define err(A) //cout << #A << " = " << (A) << endl using namespace std; int n; int p1[24], p2[24], c[24], g[24], h[24]; string ans = "z"; void check(int msk) { memset(g, -1, sizeof g); memset(h, 0, sizeof h); bool bad = false; for (int i = 0; i < n; i ++) if ((1 << i) & msk) { swap(p1[i], p2[i]); err(i); } for (int i = n; i --;) { h[i] = max(h[i], c[i]); if (p1[i] == -1) { continue; } // C[i] if (c[p1[i]] != c[i]) { if (g[p1[i]] != -1 and g[p1[i]] != c[i]) { bad = true; break; } g[p1[i]] = c[i]; } // G[i] if (g[i] != -1) { if (c[p2[i]] != g[i]) { if (g[p2[i]] != -1 and g[p2[i]] != g[i]) { bad = true; break; } g[p2[i]] = g[i]; } } else if (h[i]) { h[p2[i]] = max(h[p2[i]], h[i]); } } string tmp; for (int i = 0; i < n; i ++) { if (p2[i] == -1) { if (g[i] == -1) { g[i] = h[i]; } } else { if (g[i] == -1) { if (c[p2[i]] >= h[i]) g[i] = c[p2[i]]; else g[i] = g[p2[i]]; } } if (g[i] < h[i]) { bad = true; break; } tmp += char('a' + c[i]); tmp += char('a' + g[i]); tmp += '\n'; } for (int i = 0; i < n; i ++) if ((1 << i) & msk) swap(p1[i], p2[i]); if (!bad) ans = min(ans, tmp); } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; i++) { cin >> p1[i] >> p2[i]; p1[i]--; p2[i]--; char cc; cin >> cc; c[i] = cc - 'a'; } // check(24); for (int msk = 0; msk < (1 << n); msk ++) check(msk); if (ans == "z") cout << -1 << endl; else cout << ans; return 0; }