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

public class Solution {

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

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

    public void addEdge(int nodeA, int nodeB) {
        if (!this.adjacencyLists[nodeA].contains(nodeB)) {
            this.adjacencyLists[nodeA].add(nodeB);
        }
        if (!this.adjacencyLists[nodeB].contains(nodeA)) {
            this.adjacencyLists[nodeB].add(nodeA);
        }
    }

    public String distancesFrom(int startNode) {
        Map<Integer, Integer> visited = breadthFirstSearch(startNode);
        List<Integer> distancesFrom = new ArrayList<Integer>();
        for (int i = 0; i < this.nodes; i++) {
            if (i != startNode) {
                distancesFrom.add(visited.getOrDefault(i, -1));
            }
        }
        return spaceDelimit(distancesFrom);
    }

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

    private Map<Integer, Integer> breadthFirstSearch(int startNode) {
        Map<Integer, Integer> visited = new HashMap<Integer, Integer>();
        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.adjacencyLists[currentNode]){
                if (!visited.containsKey(neighbor)) {
                    nodesToVisit.add(neighbor);
                    nodesToVisitDepth.add(currentDepth + 6);
                }
            }
            if (!visited.containsKey(currentNode) || visited.get(currentNode) > currentDepth) {
                visited.put(currentNode, currentDepth);
            }
        }
        return visited;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int queries = in.nextInt();
        for (int query = 0; query < queries; query++) {
            int nodes = in.nextInt();
            int edges = in.nextInt();
            Solution graph = new Solution(nodes, edges);
            for (int edge = 0; edge < edges; edge++) {
                int nodeA = in.nextInt();
                int nodeB = in.nextInt();
                graph.addEdge(nodeA - 1, nodeB - 1);
            }
            int startNode = in.nextInt();
            System.out.println(graph.distancesFrom(startNode - 1));
        }
    }
}

@@ -117,23 +117,21 @@
 ivate Li
-nkedLis
+st%3CSe
 t%3CIntege
@@ -132,18 +132,17 @@
 Integer%3E
-%5B%5D
+%3E
  adjacen
@@ -289,25 +289,33 @@
 new 
-LinkedList%5Bnodes%5D
+ArrayList%3CSet%3CInteger%3E%3E()
 ;%0A  
@@ -384,27 +384,26 @@
 ists
-%5Bi%5D = new LinkedLis
+.add(i, new HashSe
 t%3CIn
@@ -402,32 +402,33 @@
 shSet%3CInteger%3E()
+)
 ;%0A        %7D%0A    
@@ -478,37 +478,32 @@
 odeB) %7B%0A        
-if (!
 this.adjacencyLi
@@ -509,73 +509,19 @@
 ists
-%5BnodeA%5D.contains(nodeB)) %7B%0A            this.adjacencyLists%5B
+.get(
 nodeA
-%5D
+)
 .add
@@ -541,23 +541,8 @@
     
-%7D%0A        if (!
 this
@@ -560,73 +560,19 @@
 ists
-%5BnodeB%5D.contains(nodeA)) %7B%0A            this.adjacencyLists%5B
+.get(
 nodeB
-%5D
+)
 .add
@@ -580,26 +580,16 @@
 nodeA);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1772,17 +1772,21 @@
 ncyLists
-%5B
+.get(
 currentN
@@ -1788,17 +1788,17 @@
 rentNode
-%5D
+)
 )%7B%0A     

Replace adjacencyLists with Sets