#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i, n) for (int (i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++) #define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--) #define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--) #define DEBUG(C) cerr << #C << " = " << C << endl; using LL = long long; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using VD = vector; using VVD = vector; using PII = pair; using PDD = pair; using PLL = pair; using VPII = vector; template using VT = vector; #define ALL(a) begin((a)), end((a)) #define RALL(a) rbegin((a)), rend((a)) #define SORT(a) sort(ALL((a))) #define RSORT(a) sort(RALL((a))) #define REVERSE(a) reverse(ALL((a))) #define MP make_pair #define FORE(a, b) for (auto &&a : (b)) #define FIND(s, e) ((s).find(e) != (s).end()) #define EB emplace_back template inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;} template inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;} const int INF = 1e9; const int MOD = INF + 7; const LL LLINF = 1e18; const long double EPS = 1e-9; class StronglyConnectedComponentDecomposition { private: std::vector used; std::vector> graph, revgraph; std::vector comp; std::vector sortNode; void dfs(int now) { this->used[now] = true; for (const int e : this->graph[now]) { if (!this->used[e]) dfs(e); } this->sortNode.emplace_back(now); return; } void dfs2(int now, int cnt) { this->used[now] = true; for (const int e : this->revgraph[now]) { if (!this->used[e]) dfs2(e, cnt); } this->comp[now] = cnt; return; } public: StronglyConnectedComponentDecomposition() {} StronglyConnectedComponentDecomposition(const int n) : used(n, false), graph(n), revgraph(n), comp(n, 0) {} void resize(const int n) { this->used.resize(n, false); this->graph.resize(n); this->revgraph.resize(n); this->comp.resize(n, 0); } void addEdge(const int from, const int to) { this->graph[from].emplace_back(to); this->revgraph[to].emplace_back(from); return; } int solve() { std::fill(std::begin(this->used), std::end(this->used), false); for (int i = 0; i < (int)this->used.size(); i++) { if (!used[i]) dfs(i); } std::fill(std::begin(this->used), std::end(this->used), false); std::reverse(std::begin(this->sortNode), std::end(this->sortNode)); int res = 0; for (const int n : this->sortNode) { if (!this->used[n]) dfs2(n, res++); } return res; } std::vector getComp() { return this->comp; } bool same(const int a, const int b) { return this->comp[a] == this->comp[b]; } int operator[](const int n) { return comp[n]; } }; // need SCC Library!!!! class Two_Sat { private: StronglyConnectedComponentDecomposition scc; int sz; public: Two_Sat(const int n) : scc(n * 2), sz(n) {} Two_Sat() {} // add (a OR b) void addLogic(const int a, const bool ab, const int b, const bool bb) { this->scc.addEdge(a + (ab ? 0 : this->sz), b + (bb ? this->sz : 0)); this->scc.addEdge(b + (bb ? 0 : this->sz), a + (ab ? this->sz : 0)); } // (logic 1) AND (logic 2) AND ... (logic N) <- satisfy bool calcSatisfy() { this->scc.solve(); for (int i = 0; i < this->sz; i++) { if (this->scc.same(i, i + this->sz)) return false; } return true; } bool operator[](const int n) { return this->scc[n] > this->scc[n + sz]; } }; const int MAX = 1e5 + 10; char strbuf[MAX]; string next() { scanf("%s", strbuf); return strbuf; } int N; string S[MAX]; int main(void) { int cnt = 0; scanf("%d", &N); for (int i = 0; i < N; ++i) { S[i] = next(); } if (N > 1000) { puts("Impossible"); return 0; } Two_Sat ts(N); for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { if (S[i].substr(0, 1) == S[j].substr(0, 1) || S[i].substr(1, 2) == S[j].substr(1, 2)) { ts.addLogic(i, true, j, true); } if (S[i].substr(0, 1) == S[j].substr(2, 1) || S[i].substr(1, 2) == S[j].substr(0, 2)) { ts.addLogic(i, true, j, false); } if (S[i].substr(2, 1) == S[j].substr(0, 1) || S[i].substr(0, 2) == S[j].substr(1, 2)) { ts.addLogic(i, false, j, true); } if (S[i].substr(2, 1) == S[j].substr(2, 1) || S[i].substr(0, 2) == S[j].substr(0, 2)) { ts.addLogic(i, false, j, false); } } } if (!ts.calcSatisfy()) { puts("Impossible"); return 0; } for (int i = 0; i < N; ++i) { if (ts[i]) { for (int j = 0; j < 3; ++j) { putchar(S[i][j]); if (j == 0) putchar(' '); } putchar('\n'); } else { for (int j = 0; j < 3; ++j) { putchar(S[i][j]); if (j == 1) putchar(' '); } putchar('\n'); } } }