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 startNode) {
        Map<Integer, Integer> visited = new HashMap();
        Queue<Integer> nodesToVisit = new LinkedList<Integer>();
        Queue<Integer> nodesToVisitDepth = new LinkedList<Integer>();

        nodesToVisit.add(startNode);
        nodesToVisitDepth.add(0);

        while (nodesToVisit.size() != 0) {
            int currentNode = nodesToVisit.poll();
            int currentDepth = nodesToVisitDepth.poll();
            for (int neighbor : this.adjacency_lists[currentNode]){
                if (!visited.containsKey(neighbor)) {
                    nodesToVisit.add(neighbor);
                    nodesToVisitDepth.add(currentDepth + 1);
                }
            }
            if (!visited.containsKey(currentNode) || visited.get(currentNode) > currentDepth) {
                //System.out.println("Putting: " + Integer.toString(currentNode) + " Depth: " + Integer.toString(currentDepth));
                visited.put(currentNode, currentDepth);
            }
        }
        //System.out.println(visited);
        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));
        }
    }
}


@@ -155,17 +155,16 @@
 lists;%0A%0A
-%0A
     publ
@@ -491,50 +491,192 @@
     
-this.adjacency_lists%5Bnode_a%5D.add(node_b);%0A
+if (!this.adjacency_lists%5Bnode_a%5D.contains(node_b)) %7B%0A            this.adjacency_lists%5Bnode_a%5D.add(node_b);%0A        %7D%0A        if (!this.adjacency_lists%5Bnode_b%5D.contains(node_a)) %7B%0A    
     
@@ -721,16 +721,26 @@
 ode_a);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1632,16 +1632,34 @@
  HashMap
+%3CInteger, Integer%3E
 ();%0A    
@@ -3320,9 +3320,8 @@
     %7D%0A%7D%0A
-%0A

Check for already added edges