#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define COUNT_COMMANDS 1000000
#define COUNT_STRING 100000
#define SRTING_LEN 2500

typedef enum {
    clear = 1, number, syntax, parameter
}errors_type;


typedef struct {
    char pattern[SRTING_LEN];
    char replacement[SRTING_LEN];
    int flag;
} SUBSTITUTION;

typedef struct {
    char pattern[SRTING_LEN];
} DELETION;

typedef struct {
    char source[SRTING_LEN];
    char dest[SRTING_LEN];
} TRANSLITERATION;

typedef struct {
    char pattern[SRTING_LEN];
    char text[SRTING_LEN];
} INSERTION;


typedef struct {
    char line[SRTING_LEN];
    int start; // Строки
    int end;
    char command[2];
    errors_type type;

    union {
        SUBSTITUTION s;
        DELETION d;
        TRANSLITERATION y;
        INSERTION i;
    } data;

}COMMAND;

typedef struct {
    char line[SRTING_LEN];
}ARRAY;

void print_error(char string[], errors_type type) {
    switch (type) {
    case number:
        printf("Error in command <%s>: Invalid line number.", string);
        break;
    case syntax:
        printf("Error in command <%s>: Command syntax error.", string);
        break;
    case parameter:
        printf("Error in command <%s>: Parameter error.", string);
        break;
    }
}

int find_index(char string1[], char string2[]) { // где ищем что ищем

    char* result = strstr(string1, string2);

    if (result == NULL) {
        return -1;
    }

    return result - string1;
}

void substitution(char line[], char pattern[], char replacement[], int flag) {

    if (flag == 0) {
        int position = find_index(line, pattern);
        if (position != -1) {

            char new_line[SRTING_LEN] = "";
            int index_line = 0, index_new = 0;

            for (; index_line < position; index_line++, index_new++) // До нахождения подстроки
                new_line[index_new] = line[index_line];

            for (int i = 0; replacement[i] != '\0'; i++, index_new++) // Замена
                new_line[index_new] = replacement[i];

            for (index_line += strlen(pattern); line[index_line] != '\0'; index_line++, index_new++) // После замены
                new_line[index_new] = line[index_line];

            new_line[index_new] = '\0'; // Завершение строки - null termination

            strcpy(line, new_line);
        }
    }
    else {
        while (1) {
            int position = find_index(line, pattern);
            if (position != -1) {

                char new_line[SRTING_LEN] = "";
                int index_line = 0, index_new = 0;

                for (; index_line < position; index_line++, index_new++) // До нахождения подстроки
                    new_line[index_new] = line[index_line];

                for (int i = 0; replacement[i] != '\0'; i++, index_new++) // Замена
                    new_line[index_new] = replacement[i];

                for (index_line += strlen(pattern); line[index_line] != '\0'; index_line++, index_new++) // После замены
                    new_line[index_new] = line[index_line];

                new_line[index_new] = '\0'; // Завершение строки - null termination

                strcpy(line, new_line);
            }
            else
                break;
        }
    }
}

void deletion(char line[], char pattern[]) {
    int position = find_index(line, pattern);
    if (position != -1)
        strcpy(line, "");
}

void transliteration(char line[], char source[], char dest[]) {
    for (int i = 0; line[i] != '\0'; i++) {
        int position = -1;
        for (int j = 0; source[j] != '\0'; j++) {
            if (line[i] == source[j]) {
                position = j;
                break;
            }
        }
        if (position != -1) {
            line[i] = dest[position];
        }
    }
}

void insertion(char line[], char pattern[], char text[]) {
    int position = find_index(line, pattern);
    if (position != -1) {
        char new_line[SRTING_LEN] = "";
        int index_line = 0, index_new = 0;

        for (; index_line < position; index_line++, index_new++)
            new_line[index_new] = line[index_line];

        for (int i = 0; text[i] != '\0'; i++, index_new++)
            new_line[index_new] = text[i];

        for (int i = 0; line[index_line] != '\0'; index_line++, i++, index_new++) {
            new_line[index_new] = line[index_line];
        }

        new_line[index_new] = '\0'; // Завершение строки - null termination
        strcpy(line, new_line);
    }
}

int main(int argc, char* argv[]) {
    int M, // Количество команд (от 1 до 1.000.000)
        N; // Количество строк входного текста (от 1 до 100.000)

    char num_M[10];
    fgets(num_M, sizeof(num_M), stdin);
    num_M[strcspn(num_M, "\n")] = 0;
    M = atoi(num_M);

    COMMAND* commands = malloc(M * sizeof(COMMAND));

    for (int i = 0; i < M; i++) { // Читаем строки

        char buffer[SRTING_LEN];
        fgets(buffer, sizeof(buffer), stdin);
        buffer[strcspn(buffer, "\n")] = 0;

        strcpy(commands[i].line, buffer); // Массив с текстом

        commands[i].type = clear; // По умолчанию все команды считаются корректными

        int j = 0;

        // Обозначение промежутка
        if (buffer[j] == 's' || buffer[j] == 'd' || buffer[j] == 'y' || buffer[j] == 'i') { // Границ нет
            commands[i].start = 1;
            commands[i].end = COUNT_STRING;
        }
        else if (buffer[j] >= '1' && buffer[j] <= '9') { // Границы есть и они корректны

            char* line_start = malloc(COUNT_STRING); // Первое значение
            int num = 0;
            for (; buffer[j] != ',' && buffer[j] != ' ' && buffer[j] != '\0'; num++, j++) {
                if (buffer[j] >= '0' && buffer[j] <= '9')
                    line_start[num] = buffer[j];
                else {
                    commands[i].type = number; // Ошибка в промежутках
                    break;
                }
            }
            line_start[num] = '\0'; // Завершение строки - null termination

            commands[i].start = atoi(line_start);
            free(line_start);


            if (buffer[j] == ',') { // Второе значение (если есть)
                j += 1;

                if (buffer[j] >= '1' && buffer[j] <= '9') {

                    char* line_end = malloc(COUNT_STRING);
                    num = 0;
                    for (; buffer[j] != ' ' && buffer[j] != '\0'; num++, j++) {
                        if (buffer[j] >= '0' && buffer[j] <= '9')
                            line_end[num] = buffer[j];
                        else {
                            commands[i].type = syntax; // Ошибка в промежутках
                            break;
                        }
                    }
                    line_end[num] = '\0'; // Завершение строки - null termination

                    commands[i].end = atoi(line_end);
                    free(line_end);
                }
                else
                    commands[i].type = number; // Ошибка в промежутке

            }
            else
                commands[i].end = commands[i].start;

            // Проверка на корректность найдённого промежутка
            if (commands[i].start > commands[i].end)
                commands[i].type = number; // Ошибка в промежутках

            j += 1; // Указатель не на пробеле!
        }
        else {
            int flag = 0;
            for (int f = j; buffer[f]; f++) {
                if (buffer[f] == ' ' || buffer[f] == ',')
                    flag = 1;
            }
            if (flag)
                commands[i].type = number; // Ошибка в промежутках (комманда начинается не с sdyi и не с цифр)
            else
                commands[i].type = syntax;
        }

        // Проверка
        if (commands[i].type != clear) {
            print_error(buffer, commands[i].type);
            free(commands);
            return EXIT_SUCCESS;;
        }

        // Тип комманды
        switch (buffer[j]) {
        case 's':
            commands[i].command[0] = 's';
            break;
        case 'd':
            commands[i].command[0] = 'd';
            break;
        case 'y':
            commands[i].command[0] = 'y';
            break;
        case 'i':
            commands[i].command[0] = 'i';
            break;
        }

        j += 1; // Слеш

        switch (commands[i].command[0]) {
        case 's':
            if (buffer[j] != '/') { // Проверка
                commands[i].type = syntax;
                break;
            }
            else {
                j += 1;

                // pattern
                char pattern[SRTING_LEN] = "";
                int letter = 0;
                for (; buffer[j] != '/' && buffer[j] != '\0'; letter++, j++)
                    pattern[letter] = buffer[j];
                pattern[letter] = '\0'; // Завершение строки - null termination
                strcpy(commands[i].data.s.pattern, pattern);

                if (buffer[j] == '\0') { // Проверка
                    commands[i].type = syntax;
                    break;
                }
                else if (commands[i].data.s.pattern[0] == '\0') { // Проверка
                    commands[i].type = parameter;
                    break;
                }

                j += 1; // не слеш

                // replacement
                char replacement[SRTING_LEN] = "";
                letter = 0;
                for (; buffer[j] != '/' && buffer[j] != '\0'; letter++, j++)
                    replacement[letter] = buffer[j];
                replacement[letter] = '\0';
                strcpy(commands[i].data.s.replacement, replacement);

                if (buffer[j] == '\0') { // Проверка
                    commands[i].type = syntax;
                    break;
                }
                else if (commands[i].data.s.replacement[0] == '\0') { // Проверка
                    commands[i].type = parameter;
                    break;
                }

                j += 1; // не слеш

                if (buffer[j] != '\0' && buffer[j] == 'g' && buffer[j + 1] == '\0') // flag
                    commands[i].data.s.flag = 1;
                else if (buffer[j] == '\0') // Проверка
                    commands[i].data.s.flag = 0;
                else
                    commands[i].type = syntax;

                break;
            }
        case 'd':
            if (buffer[j] != '/') { // Проверка
                commands[i].type = syntax;
                break;
            }
            else {
                j += 1;

                // pattern
                char pattern[SRTING_LEN] = "";
                int letter = 0;
                for (; buffer[j] != '/' && buffer[j] != '\0'; letter++, j++)
                    pattern[letter] = buffer[j];
                pattern[letter] = '\0'; // Завершение строки - null termination
                strcpy(commands[i].data.d.pattern, pattern);

                if (buffer[j] == '\0') // Проверка
                    commands[i].type = syntax;
                else if (buffer[j + 1] != '\0')
                    commands[i].type = syntax;
                else if (commands[i].data.d.pattern[0] == '\0')
                    commands[i].type = parameter;


                break;
            }
        case 'y':
            if (buffer[j] != '/') { // Проверка
                commands[i].type = syntax;
                break;
            }
            else {
                j += 1; // не слеш

                // source
                char source[SRTING_LEN] = "";
                int letter = 0;
                for (; buffer[j] != '/' && buffer[j] != '\0'; letter++, j++)
                    source[letter] = buffer[j];
                source[letter] = '\0'; // Завершение строки - null termination
                strcpy(commands[i].data.y.source, source);

                if (buffer[j] == '\0') { // Проверка
                    commands[i].type = syntax;
                    break;
                }
                else if (commands[i].data.y.source[0] == '\0') { // Проверка
                    commands[i].type = parameter;
                    break;
                }

                j += 1; // не слеш

                // dest
                char dest[SRTING_LEN] = "";
                letter = 0;
                for (; buffer[j] != '/' && buffer[j] != '\0'; letter++, j++)
                    dest[letter] = buffer[j];
                dest[letter] = '\0'; // Завершение строки - null termination
                strcpy(commands[i].data.y.dest, dest);

                if (buffer[j] == '\0') { // Проверка
                    commands[i].type = syntax;
                    break;
                }
                else if (buffer[j + 1] != '\0')
                    commands[i].type = syntax;
                else if (commands[i].data.y.dest[0] == '\0') { // Проверка
                    commands[i].type = parameter;
                    break;
                }


                if (strlen(commands[i].data.y.dest) != strlen(commands[i].data.y.source))
                    commands[i].type = parameter;

                break;
            }
        case 'i':
            if (buffer[j] != '/') { // Проверка
                commands[i].type = syntax;
                break;
            }
            else {
                j += 1; // не слеш

                // pattern
                char pattern[SRTING_LEN] = "";
                int letter = 0;
                for (; buffer[j] != '/' && buffer[j] != '\0'; letter++, j++)
                    pattern[letter] = buffer[j];
                pattern[letter] = '\0'; // Завершение строки - null termination
                strcpy(commands[i].data.i.pattern, pattern);

                if (buffer[j] == '\0') { // Проверка
                    commands[i].type = syntax;
                    break;
                }
                else if (commands[i].data.i.pattern[0] == '\0') { // Проверка
                    commands[i].type = parameter;
                    break;
                }

                j += 1; // не слеш

                // text
                char text[SRTING_LEN] = "";
                letter = 0;
                for (; buffer[j] != '/' && buffer[j] != '\0'; letter++, j++)
                    text[letter] = buffer[j];
                text[letter] = '\0'; // Завершение строки - null termination
                strcpy(commands[i].data.i.text, text);

                if (buffer[j] == '\0') // Проверка
                    commands[i].type = syntax;
                else if (buffer[j + 1] != '\0')
                    commands[i].type = syntax;
                else if (commands[i].data.i.text[0] == '\0' && buffer[j + 1] == '\0') // Проверка
                    commands[i].type = parameter;

                break;
            }
        }

        if (commands[i].type != clear) {
            print_error(buffer, commands[i].type);
            free(commands);
            return EXIT_SUCCESS;;
        }

        // Конец команд
    }

    char num_N[10];
    fgets(num_N, sizeof(num_N), stdin);
    num_N[strcspn(num_N, "\n")] = 0;
    N = atoi(num_N);

    ARRAY* strings = malloc((N) * sizeof(ARRAY));

    for (int i = 0; i < N; i++) {
        char buffer[SRTING_LEN];

        fgets(buffer, sizeof(buffer), stdin);
        buffer[strcspn(buffer, "\n")] = 0;

        strcpy(strings[i].line, buffer); // Массив с текстом
    }

    for (int i = 0; i < M; i++) { // Перебор всех команд
        if (commands[i].end == COUNT_STRING)
            commands[i].end = N;
        else if (commands[i].end > N) {
            commands[i].type = number; // Ошибка в промежутках
            print_error(commands[i].line, commands[i].type);
            free(commands);
            free(strings);
            return EXIT_SUCCESS;;
        }

        for (int index_line = commands[i].start - 1; index_line < commands[i].end; index_line++) {

            switch (commands[i].command[0]) {
            case 's':
                substitution(strings[index_line].line, commands[i].data.s.pattern, commands[i].data.s.replacement, commands[i].data.s.flag);
                break;
            case 'd':
                deletion(strings[index_line].line, commands[i].data.d.pattern);
                break;
            case 'y':
                transliteration(strings[index_line].line, commands[i].data.y.source, commands[i].data.y.dest);
                break;
            case 'i':
                insertion(strings[index_line].line, commands[i].data.i.pattern, commands[i].data.i.text);
                break;
            }
        }
    }

    for (int i = 0; i < N - 1; i++) {
        if (strings[i].line[0] != '\0')
            printf("%s\n", strings[i].line);
    }
    printf("%s", strings[N - 1].line);


    free(commands);
    free(strings);
    return EXIT_SUCCESS;;
}