package algospot_wordchain;

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

public class Main {

    public static int C;
    public static int N;
    public static String WORD[];
    public static int MAP[][] = new int[26][26];
    public static int IN_DEGREE[] = new int[26];
    public static int OUT_DEGREE[] = new int[26];
    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];
            for (int j = 0; j < N; j++) {
                WORD[j] = br.readLine();
            }

            for (int j = 0; j < 26; j++) {
                Arrays.fill(MAP[j], 0);
            }
            Arrays.fill(IN_DEGREE, 0);
            Arrays.fill(OUT_DEGREE, 0);

            RESULT.clear();

            solve();
        }
    }

    private static void solve() {
        for (int i = 0; i < N; i++) {
            makeGraph(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 makeGraph(int i) {
        int start = WORD[i].charAt(0) - 'a';
        int end = WORD[i].charAt(WORD[i].length() - 1) - 'a';

        MAP[start][end]++;
        INDE

    }

}

@@ -172,16 +172,45 @@
 Arrays;%0A
+import java.util.LinkedList;%0A
 import j
@@ -479,32 +479,83 @@
 %5D = new int%5B26%5D;
+%0A%0A    public static LinkedList%3CString%3E%5B%5D%5B%5D WORDMAP;
 %0A    public stat
@@ -567,22 +567,23 @@
 rayList%3C
-String
+Integer
 %3E RESULT
@@ -599,16 +599,23 @@
 rayList%3C
+Integer
 %3E();%0A%0A  
@@ -1445,16 +1445,61 @@
 EE, 0);%0A
+            WORDMAP = new LinkedList%5B26%5D%5B26%5D;
 %0A       
@@ -1679,32 +1679,46 @@
     
-if (RESULT.isEmpty() %7C%7C 
+getEulerCircuitOrTrail();%0A        if (
 RESU
@@ -1726,16 +1726,20 @@
 T.size()
+ - 1
  != N) %7B
@@ -1851,17 +1851,17 @@
 1; i %3E= 
-0
+1
 ; i--) %7B
@@ -1882,38 +1882,114 @@
    S
-ystem.out.printf(RESULT.get(i)
+tring word = WORDMAP%5BRESULT.get(i)%5D%5BRESULT.get(i - 1)%5D.removeLast();%0A                System.out.print(word
  + %22
@@ -2236,16 +2236,45 @@
 end%5D++;%0A
+        OUT_DEGREE%5Bstart%5D++;%0A
         
@@ -2279,11 +2279,874 @@
   IN
+_
 DE
-%0A
+GREE%5Bend%5D++;%0A        if (WORDMAP%5Bstart%5D%5Bend%5D == null) %7B%0A            WORDMAP%5Bstart%5D%5Bend%5D = new LinkedList%3CString%3E();%0A        %7D%0A        WORDMAP%5Bstart%5D%5Bend%5D.add(WORD%5Bi%5D);%0A    %7D%0A%0A    private static void getEulerCircuit(int start) %7B%0A        for (int end = 0; end %3C 26; end++) %7B%0A            while (MAP%5Bstart%5D%5Bend%5D %3E 0) %7B%0A                MAP%5Bstart%5D%5Bend%5D--;%0A                getEulerCircuit(end);%0A            %7D%0A        %7D%0A        RESULT.add(start);%0A    %7D%0A%0A    private static void getEulerCircuitOrTrail() %7B%0A        for (int i = 0; i %3C 26; i++) %7B%0A            // find start node%0A            if (IN_DEGREE%5Bi%5D + 1 == OUT_DEGREE%5Bi%5D) %7B%0A                getEulerCircuit(i);%0A                return;%0A            %7D%0A        %7D%0A%0A        for (int i = 0; i %3C 26; i++) %7B%0A            if (IN_DEGREE%5Bi%5D %3E 0) %7B%0A                getEulerCircuit(i);%0A                return;%0A            %7D%0A        %7D
 %0A   

Implement for algospot wordchain