//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} struct SCC { struct csr { vector start, elist; csr(int n, const vector> &edges) : start(n+1), elist(edges.size()) { for(pair e:edges) { start[e.first+1]++; } for(int i = 1; i <= n; i++) { start[i] += start[i-1]; } vector counter = start; for(pair e:edges) { elist[counter[e.first]++] = e.second; } } }; int n; vector> edges; SCC(int n_) : n(n_) {} void add(int from, int to) {edges.emplace_back(from, to);} pair> scc_ids() { csr g(n, edges); int now_ord = 0, group_num = 0; vector used, low(n), ord(n, -1), ids(n); used.reserve(n); auto dfs = [&](auto self, int v)->void { low[v] = ord[v] = now_ord++; used.push_back(v); for(int i = g.start[v]; i < g.start[v+1]; i++) { int to = g.elist[i]; if(ord[to] == -1) { self(self, to); low[v] = min(low[v], low[to]); } else { low[v] = min(low[v], ord[to]); } } if(low[v] == ord[v]) { while(true) { int u = used.back(); used.pop_back(); ord[u] = n; ids[u] = group_num; if(u == v) break; } group_num++; } }; for(int i = 0; i < n; i++) { if(ord[i] == -1) dfs(dfs, i); } for(int &x:ids) { x = group_num - 1 - x; } return {group_num, ids}; } vector> scc() { pair> ids = scc_ids(); int group_num = ids.first; vector counts(group_num); for(int x:ids.second) counts[x]++; vector> groups(group_num); for(int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for(int i = 0; i < n; i++) { groups[ids.second[i]].push_back(i); } return groups; } }; //SCC is needed struct two_sat { int n; vector boolean_val; SCC scc; two_sat():n(0), scc(0) {} two_sat(int n_):n(n_), boolean_val(n_), scc(n_<<1) {} void add(int i, bool ii, int j, bool jj) { scc.add(i + (ii?0:n), j + (jj?n:0)); scc.add(j + (jj?0:n), i + (ii?n:0)); } bool satisfiable() { vector id = scc.scc_ids().second; for(int i = 0; i < n; i++) { if(id[i] == id[i+n]) return false; boolean_val[i] = id[i] < id[i+n]; } return true; } }; #undef _GLIBCXX_DEBUG string to_string(const string &s) {return '"' + s + '"';} string to_string(const char *s) {return to_string(string(s));} string to_string(bool b) {return b?"true":"false";} string to_string(vector v) { string res = "{"; for(int i = 0; i < int(v.size()); i++) { if(i) res += ", "; res += to_string(v[i]); } res += "}"; return res; } template string to_string(bitset v) { string res; for(size_t i = 0; i < N; i++) res += char('0' + v[i]); return res; } template string to_string(pair); template string to_string(tuple); template string to_string(tuple); template string to_string(A v) { bool first = true; string res = "{"; for(const auto &x:v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } template string to_string(pair p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template string to_string(tuple t) { return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")"; } template string to_string(tuple t) { return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ")"; } void debug_out() {cerr << endl;} template void debug_out(Head H, Tail... T) { cerr << ' ' << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 822 #endif //head void imp() { cout << "Impossible\n"; } int pl(vs::iterator itb, vs::iterator ite, string s) { auto itr = upper_bound(itb, ite, s); itr--; int res = itr-itb; (*itr).back()++; return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vs s(n), s_(n), s__(n); rep(i, n) { cin >> s[i]; s_[i] = s[i]; s__[i] = s[i]; reverse(all(s__[i])); } sort(all(s_)); sort(all(s__)); rep(i, n) { auto it = equal_range(all(s_), s_[i]); if(it.second-it.first > 2) { imp(); return 0; } } two_sat ts(n); vector

p(n); vi _(n), __(n); rep(i, n) { string x = s[i]; int u = pl(all(s_), x); reverse(all(x)); int v = pl(all(s__), x); p[i] = {u, v}; _[u] = i; __[v] = i; } debug(_);debug(__); rep(i, n) { int j = p[i].first+1; while(j < n and s_[j][0] == s[i][0]) { ts.add(i, true, _[j++], true); } auto itr = lower_bound(all(s_), s[i].substr(2)); j = itr-s_.begin(); while(j < n and s_[j][0] == s[i][2]) { debug(i, _[j], s[i], s[_[j]]); ts.add(i, false, _[j++], true); } j = p[i].second+1; while(j < n and s__[j][0] == s[i][2]) { ts.add(i, false, __[j++], false); } string ss = s[i].substr(0, 2); j = p[i].first+1; while(j < n and s_[j].substr(0, 2) == ss) { ts.add(i, false, _[j++], false); } ss = s[i].substr(1); itr = lower_bound(all(s_), ss); j = itr-s_.begin(); while(j < n and s_[j].substr(0, 2) == ss) { ts.add(i, true, _[j++], false); } reverse(all(ss)); j = p[i].second+1; while(j < n and s__[j].substr(0, 2) == ss) { ts.add(i, true, __[j++], true); } } if(!ts.satisfiable()) { imp(); return 0; } rep(i, n) { if(ts.boolean_val[i]) { cout << s[i].substr(0, 2) << ' ' << s[i][2] << '\n'; } else { cout << s[i][0] << ' ' << s[i].substr(1) << '\n'; } } }