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

public class Turing {
    static class Step {
	String s1, s2;
	char c1, c2;
	int dir;

	Step (String s1, char c1, String s2, char c2, int dir) {
	    this.s1 = s1;
	    this.s2 = s2;
	    this.c1 = c1;
	    this.c2 = c2;
	    this.dir = dir;
	}
    }

    static String state = "start", tape = " ";
    static int pos = 0;
    static Vector<Step> steps = new Vector<Step>();

    public static void main (String[] args) {
	BufferedReader in
	    = new BufferedReader(new InputStreamReader(System.in));

	while (true) {
	    String[] line = null;
	    try {
		line = in.readLine().split("\\s+");
	    } catch (Exception e) {
		System.out.println(e);
	    }

	    if (line.length == 0 || line[0].equals(""))
		continue;
	    if (line[0].equals("help")) {
		System.out.println("input INPUT_STRING");
		System.out.println("step STATE1 CHAR1 STATE2 CHAR2 +|-");
		System.out.println("list");
		System.out.println("dump");
		System.out.println("help");
		System.out.println("quit");
	    }
	    else if (line[0].equals("input")) {
		state = "start";
		pos = 0;
		if (line.length >= 2)
		    tape = line[1];
		else
		    tape = " ";
		print();
	    }
	    else if (line[0].equals("step")) {
		String s1 = line[1];
		char c1 = _toSpace(line[2].charAt(0));
		String s2 = line[3];
		char c2 = _toSpace(line[4].charAt(0));
		char d = line[5].charAt(0);
		int dir = 1;
		if (d == '+')
		    dir = 1;
		else if (d == '-')
		    dir = -1;
		else
		    System.out.println("bad direction:  " + line[5]);
		Step step = find(s1, c1);
		if (step != null) {
		    step.s2 = s2;
		    step.c2 = c2;
		    step.dir = dir;
		}
		else
		    steps.add(new Step(s1, c1, s2, c2, dir));
	    }
	    else if (line[0].equals("list") || line[0].equals("dump"))
		for (int i = 0; i < steps.size(); i++) {
		    Step step = steps.elementAt(i);
		    System.out.print("step " + step.s1 +
				     " " + spaceTo_(step.c1) +
				     " " + step.s2 +
				     " " + spaceTo_(step.c2));
		    if (step.dir == 1)
			System.out.println(" +");
		    else
			System.out.println(" -");
		}
	    else if (line[0].equals("quit"))
		return;
	    else
		System.out.println("unknown command:  " + line[0]);

	    run();
	}
    }

    static char _toSpace (char c) {
	if (c == '_')
	    return ' ';
	else
	    return c;
    }

    static char spaceTo_ (char c) {
	if (c == ' ')
	    return '_';
	else
	    return c;
    }

    static void run () {
	while (step())
	    print();
    }

    static boolean step () {
	String s1 = state;
	char c1 = tape.charAt(pos);
	Step step = find(s1, c1);
	if (step == null)
	    return false;
	state = step.s2;
	tape = tape.substring(0, pos) + step.c2 + tape.substring(pos+1);
	pos += step.dir;
	if (pos == -1) {
	    tape = " " + tape;
	    pos = 0;
	}
	else if (pos == tape.length())
	    tape = tape + " ";
	if (pos == 1 && tape.charAt(0) == ' ') {
	    pos = 0;
	    tape = tape.substring(1);
	}
	if (pos == tape.length()-2 && tape.charAt(tape.length()-1) == ' ')
	    tape = tape.substring(0, tape.length()-1);
	return true;
    }

    static Step find (String s1, char c1) {
	for (int i = 0; i < steps.size(); i++) {
	    Step step = steps.elementAt(i);
	    if (s1.equals(step.s1) && c1 == step.c1)
		return step;
	}
	return null;
    }

    static void print () {
	System.out.println(tape.substring(0, pos) +
			   "[" + state + "]" +
			   tape.substring(pos));
    }
}
