import java.io.*;
import java.util.*;

public class Solution {

    private int nodes;
    private int edges;
    private LinkedList<Integer>[] adjacency_lists;


    public Solution(int nodes, int edges) {
        this.nodes = nodes;
        this.edges = edges;
        this.adjacency_lists = new LinkedList[nodes];
        for (int i = 0; i < nodes; i++) {
            adjacency_lists[i] = new LinkedList<Integer>();
        }
    }

    public void add_edge(int node_a, int node_b) {
        this.adjacency_lists[node_a].add(node_b);
        this.adjacency_lists[node_b].add(node_a);
    }

    public String distances_from(int start_node) {
        Map<Integer, Integer> visited = breadth_first_search(start_node);

        List<Integer> distances_from = new ArrayList<Integer>();
        for (int i = 0; i < this.nodes; i++) {
            if (i == start_node) {
                continue;
            } else if (visited.containsKey(i)) {
                distances_from.add(visited.get(i) * 6);
            } else {
                distances_from.add(-1);
            }
        }
        return space_delimit(distances_from);
    }

    public String space_delimit(List<Integer> list) {
        StringJoiner joiner = new StringJoiner(" ");
        for (Integer i : list) {
            joiner.add(i.toString());
        }
        return joiner.toString();
    }

    private Map<Integer, Integer> breadth_first_search(int start_node) {
        Map<Integer, Integer> visited = new HashMap();
        TreeMap<Integer, Integer> queue = new TreeMap<Integer, Integer>();

        visited.put(start_node, 0);
        queue.put(0, start_node);
    
        while (queue.size() != 0) {
            Map.Entry<Integer, Integer> current = queue.pollFirstEntry();
            int current_depth = current.getKey();
            int current_node = current.getValue();
            for (int neighbor : this.adjacency_lists[current_node]){
                if (!visited.containsKey(neighbor)) {
                    visited.put(neighbor, current_depth + 1);
                    queue.put(current_depth + 1, neighbor);
                } else if (visited.get(neighbor) > current_depth + 1) {
                    visited.put(neighbor, current_depth + 1);
                }
            }
        }

        return visited;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int queries = in.nextInt();
        for (int query_i = 0; query_i < queries; query_i++) {
            int nodes = in.nextInt();
            int edges = in.nextInt();
            Solution graph = new Solution(nodes, edges);
            for (int edge_i = 0; edge_i < edges; edge_i++) {
                int node_a = in.nextInt();
                int node_b = in.nextInt();
                graph.add_edge(node_a - 1, node_b - 1);
            }
            int start_node = in.nextInt();
            System.out.println(graph.distances_from(start_node - 1));
        }
    }
}

@@ -1414,34 +1414,33 @@
 search(int start
-_n
+N
 ode) %7B%0A        M
@@ -1493,31 +1493,29 @@
         
-TreeMap
+Queue
 %3CInteger
 , Intege
@@ -1510,48 +1510,110 @@
 eger
-, Integer%3E queue = new TreeMap%3CInteger, 
+%3E nodesToVisit = new LinkedList%3CInteger%3E();%0A        Queue%3CInteger%3E nodesToVisitDepth = new LinkedList%3C
 Inte
@@ -1633,33 +1633,34 @@
     
-visited.put
+nodesToVisit.add
 (start
-_n
+N
 ode
-, 0
 );%0A 
@@ -1670,38 +1670,34 @@
     
-queue.put(0, start_node);%0A    
+nodesToVisitDepth.add(0);%0A
 %0A   
@@ -1708,21 +1708,28 @@
  while (
-queue
+nodesToVisit
 .size() 
@@ -1752,66 +1752,43 @@
     
-Map.Entry%3CInteger, Integer%3E current = queue.pollFirstEntry
+int currentNode = nodesToVisit.poll
 ();%0A
@@ -1814,82 +1814,38 @@
 rent
-_d
+D
 epth = 
-current.getKey();%0A            int current_node = current.getValue
+nodesToVisitDepth.poll
 ();%0A
@@ -1904,18 +1904,17 @@
 %5Bcurrent
-_n
+N
 ode%5D)%7B%0A 
@@ -1990,47 +1990,33 @@
     
-visited.put(neighbor, current_depth + 1
+nodesToVisit.add(neighbor
 );%0A 
@@ -2038,25 +2038,37 @@
     
-queue.put
+nodesToVisitDepth.add
 (current
 _dep
@@ -2063,26 +2063,25 @@
 (current
-_d
+D
 epth + 1
 , neighb
@@ -2076,18 +2076,8 @@
  + 1
-, neighbor
 );%0A 
@@ -2092,34 +2092,56 @@
        %7D
- else
+%0A            %7D%0A           
  if (
+!
 visited.
 get(neig
@@ -2136,20 +2136,59 @@
 ted.
-get(neighbor
+containsKey(currentNode) %7C%7C visited.get(currentNode
 ) %3E 
@@ -2194,26 +2194,21 @@
  current
-_d
+D
 epth
- + 1
 ) %7B%0A    
@@ -2223,96 +2223,239 @@
     
-    visited.put(neighbor, current_depth + 1);%0A                %7D%0A            %7D%0A        %7D%0A
+//System.out.println(%22Putting: %22 + Integer.toString(currentNode) + %22 Depth: %22 + Integer.toString(currentDepth));%0A                visited.put(currentNode, currentDepth);%0A            %7D%0A        %7D%0A        //System.out.println(visited);
 %0A   
@@ -3151,8 +3151,9 @@
     %7D%0A%7D%0A
+%0A

Solve all but test case #5