static void Main(String[] args)
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int n = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < n; i++)
        {
            string   str = Console.ReadLine();
            string[] num = str.Split(' ');
            Graph    g   = new Graph(Convert.ToInt32(num[0]));
            int      e   = Convert.ToInt32(num[1]);
            for (int j = 0; j < e; j++)
            {
                str = Console.ReadLine();
                num = str.Split(' ');
                g.addEdge(Convert.ToInt32(num[0]) - 1, Convert.ToInt32(num[1]) - 1);
            }
            int s = Convert.ToInt32(Console.ReadLine()) - 1;
            //Console.WriteLine("F**k {0}", s);
            BreadthFirstPaths bfs = new BreadthFirstPaths(g, s);

            for (int j = 0; j < g.V(); j++)
            {
                if (bfs.hasPath(j) && j != s)
                {
                    Console.Write("{0} ", bfs.DistTo(j) * 6);
                }
                else if (!bfs.hasPath(j) && j != s)
                {
                    Console.Write("{0} ", -1);
                }
            }
            Console.WriteLine();
        }
    }