class Whoops {

    public static void main(String... args) throws IOException
    {
        // grab data from standard in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (reader.ready())
        {
            String firstLine = reader.readLine();
            String[] playerNames = firstLine.split(" ");
            String secondLine = reader.readLine();
            int rounds = Integer.parseInt(secondLine);

            // create a double linked list of players
            Whoops whoops = new Whoops();
            Player firstPlayer = null;
            Player previousPlayer = null;
            for (String player : playerNames)
            {
                Player newPlayer = whoops.players.get(player);
                if (newPlayer == null)
                {
                    newPlayer = new Player(player);
                }
                if (firstPlayer == null)
                {
                    firstPlayer = previousPlayer = newPlayer;
                }
                else
                {
                    previousPlayer.nextPlayer = newPlayer;
                    newPlayer.previousPlayer = previousPlayer;
                    // always update the ends so we always have a circle
                    firstPlayer.previousPlayer = newPlayer;
                    newPlayer.nextPlayer = firstPlayer;
                }
                previousPlayer = newPlayer;
            }

            // play Whoops! for n rounds
            whoops.play(firstPlayer, rounds);
        }
        reader.close();

    }

}