package com.javarush.task.task18.task1825;

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

/*
Собираем файл
*/

public class Solution {

    static TreeMap<Integer, String> treeMap = new TreeMap<>();

    public static void main(String[] args) throws IOException {

        String command = null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        /* Считываем данные с консоли и формируем массив файлов */
        while (true) {

            command = reader.readLine();

            if (!command.toLowerCase().equals("end")) {

                // Непосредственно само заполнение
                fillCollection(command);

            }
            else {

                break;

            }
        }

        // Создаем главный файл
        if (treeMap.size() > 0) {

            createMainFile(treeMap.firstEntry().getValue());

        }

        reader.close();
    }

    private static void fillCollection(String command) {

        treeMap.put(Integer.parseInt(command.substring(command.indexOf(".part") + 5)), command);

    }

    private static void createMainFile(String command) throws IOException {

        String mainFile = command.substring(0, command.indexOf(".part"));

        File file = new File(mainFile);

        // если файл создан
        if (file.createNewFile()) {

            mergeFileStreams(mainFile);

        }
    }

    private static void mergeFileStreams(String mainFile) throws IOException {

        FileOutputStream outputStream = new FileOutputStream(mainFile);
        FileInputStream inputStream = null;

        // Бежим по коллекции и начинаем писать файл
        for (Map.Entry<Integer, String> g : treeMap.entrySet()) {

            inputStream = new FileInputStream(g.getValue());
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            outputStream.write(buffer);
            inputStream.close();
        }

        outputStream.close();

    }
}