package algospot_wordchain;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {

    public static int C;
    public static int N;
    public static String WORD[];
    public static int VISITED[];
    public static ArrayList<String> RESULT = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("input.txt");
        System.setIn(fis);

        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        StringTokenizer st = new StringTokenizer(br.readLine());
        C = Integer.parseInt(st.nextToken());
        for (int i = 0; i < C; i++) {
            st = new StringTokenizer(br.readLine());
            N = Integer.parseInt(st.nextToken());
            WORD = new String[N];
            VISITED = new int[N];
            RESULT.clear();
            for (int j = 0; j < N; j++) {
                WORD[j] = br.readLine();
            }
            solve();
        }
    }

    private static void solve() {
        for (int i = 0; i < N; i++) {
            dfs(i);
        }

        if (RESULT.isEmpty() || RESULT.size() != N) {
            System.out.println("IMPOSSIBLE");
        } else {
            for (int i = RESULT.size() - 1; i >= 0; i--) {
                System.out.printf(RESULT.get(i) + " ");
            }
            System.out.println();
        }
    }

    private static void dfs(int i) {
        if (VISITED[i] == 1) {
            return;
        }
        VISITED[i] = 1;

        boolean hasNext = false;
        char startChar = WORD[i].charAt(WORD[i].length() - 1);
        for (int j = 0; j < N; j++) {
            if (i != j && startChar == WORD[j].charAt(0)) {
                dfs(j);
                hasNext = true;
            }
        }
        if (hasNext || i == N - 1) {
            RESULT.add(WORD[i]);
        }
    }

}

@@ -147,16 +147,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -333,24 +333,139 @@
 tic int 
-VISITED%5B
+MAP%5B%5D%5B%5D = new int%5B26%5D%5B26%5D;%0A    public static int IN_DEGREE%5B%5D = new int%5B26%5D;%0A    public static int OUT_DEGREE%5B%5D = new int%5B26
 %5D;%0A    p
@@ -1087,70 +1087,8 @@
 N%5D;%0A
-            VISITED = new int%5BN%5D;%0A            RESULT.clear();%0A
     
@@ -1172,32 +1172,239 @@
 ;%0A            %7D%0A
+%0A            for (int j = 0; j %3C 26; j++) %7B%0A                Arrays.fill(MAP%5Bj%5D, 0);%0A            %7D%0A            Arrays.fill(IN_DEGREE, 0);%0A            Arrays.fill(OUT_DEGREE, 0);%0A%0A            RESULT.clear();%0A%0A
             solv
@@ -1509,19 +1509,25 @@
         
-dfs
+makeGraph
 (i);%0A   
@@ -1855,19 +1855,25 @@
 ic void 
-dfs
+makeGraph
 (int i) 
@@ -1887,438 +1887,147 @@
    i
-f (VISITED%5Bi%5D == 1) %7B%0A            return;%0A        %7D%0A        VISITED%5Bi%5D = 1;%0A%0A        boolean hasNext = false;%0A        char startChar = WORD%5Bi%5D.charAt(WORD%5Bi%5D.length() - 1);%0A        for (int j = 0; j %3C N; j++) %7B%0A            if (i != j && startChar == WORD%5Bj%5D.charAt(0)) %7B%0A                dfs(j);%0A                hasNext = true;%0A            %7D%0A        %7D%0A        if (hasNext %7C%7C i == N - 1) %7B%0A            RESULT.add(WORD%5Bi%5D);%0A        %7D
+nt start = WORD%5Bi%5D.charAt(0) - 'a';%0A        int end = WORD%5Bi%5D.charAt(WORD%5Bi%5D.length() - 1) - 'a';%0A%0A        MAP%5Bstart%5D%5Bend%5D++;%0A        INDE%0A
 %0A   

add temp code