input = """rotate right 1 step
swap position 2 with position 4
rotate based on position of letter g
rotate left 4 steps
swap position 6 with position 0
swap letter h with letter a
swap letter d with letter c
reverse positions 2 through 4
swap position 2 with position 4
swap letter d with letter e
reverse positions 1 through 5
swap letter b with letter a
rotate right 0 steps
swap position 7 with position 3
move position 2 to position 1
reverse positions 2 through 5
reverse positions 4 through 7
reverse positions 2 through 7
swap letter e with letter c
swap position 1 with position 7
swap position 5 with position 7
move position 3 to position 6
swap position 7 with position 2
move position 0 to position 7
swap position 3 with position 7
reverse positions 3 through 6
move position 0 to position 5
swap letter h with letter c
reverse positions 2 through 3
swap position 2 with position 3
move position 4 to position 0
rotate based on position of letter g
rotate based on position of letter g
reverse positions 0 through 2
swap letter e with letter d
reverse positions 2 through 5
swap position 6 with position 0
swap letter a with letter g
swap position 2 with position 5
reverse positions 2 through 3
swap letter b with letter d
reverse positions 3 through 7
swap position 2 with position 5
swap letter d with letter b
reverse positions 0 through 3
swap letter e with letter g
rotate based on position of letter h
move position 4 to position 3
reverse positions 0 through 6
swap position 4 with position 1
swap position 6 with position 4
move position 7 to position 5
swap position 6 with position 4
reverse positions 5 through 6
move position 0 to position 6
swap position 5 with position 0
reverse positions 2 through 5
rotate right 0 steps
swap position 7 with position 0
swap position 0 with position 2
swap position 2 with position 5
swap letter h with letter c
rotate left 1 step
reverse positions 6 through 7
swap letter g with letter a
reverse positions 3 through 7
move position 2 to position 4
reverse positions 0 through 6
rotate based on position of letter g
swap position 0 with position 6
move position 2 to position 0
rotate left 3 steps
reverse positions 2 through 5
rotate based on position of letter a
reverse positions 1 through 4
move position 2 to position 3
rotate right 2 steps
rotate based on position of letter f
rotate based on position of letter f
swap letter g with letter a
rotate right 0 steps
swap letter f with letter h
swap letter f with letter b
swap letter d with letter e
swap position 0 with position 7
move position 3 to position 0
swap position 3 with position 0
rotate right 4 steps
rotate based on position of letter a
reverse positions 0 through 7
rotate left 6 steps
swap letter d with letter h
reverse positions 0 through 4
rotate based on position of letter f
move position 5 to position 3
move position 1 to position 3
move position 6 to position 0
swap letter f with letter c
rotate based on position of letter h
reverse positions 6 through 7"""
password = "<PASSWORD>"

# input = """swap position 4 with position 0
# swap letter d with letter b
# reverse positions 0 through 4
# rotate left 1 step
# move position 1 to position 4
# move position 3 to position 0
# rotate based on position of letter b
# rotate based on position of letter d"""
# password = "<PASSWORD>"


def swap_positions(s, x, y):
    l = list(s)
    t = l[x]
    l[x] = l[y]
    l[y] = t
    return "".join(l)

def swap_letters(s, x, y):
    s = s.replace(x, ".")
    s = s.replace(y, x)
    s = s.replace(".", y)
    return s

def rotate_right(s, d):
    d = d % len(s)
    return s[-1*d:] + s[:-1*d]

def rotate_position(s, x):
    i = s.index(x)
    d = 1 + i
    if i >= 4:
        d += 1
    return rotate_right(s, d)

def reverse(s, x, y):
    r = "".join(reversed(s[x:y+1]))
    return s[:x] + r + s[y+1:]

def move(s, x, y):
    c = s[x]
    r = s[:x] + s[x+1:]
    return r[:y] + c + r[y:]


scrambled = password
for line in input.splitlines():
    words = line.split()
    if words[0] == "swap":
        if words[1] == "position":
            scrambled = swap_positions(scrambled, int(words[2]), int(words[5]))
        elif words[1] == "letter":
            scrambled = swap_letters(scrambled, words[2], words[5])
        else:
            print("unknown instruction")
            exit(1)
    elif words[0] == "rotate":
        if words[1] == "left":
            scrambled = rotate_right(scrambled, -1 * int(words[2]))
        elif words[1] == "right":
            scrambled = rotate_right(scrambled, int(words[2]))
        elif words[1] == "based":
            scrambled = rotate_position(scrambled, words[-1])
        else:
            print("unknown instruction")
            exit(1)
    elif words[0] == "reverse":
        scrambled = reverse(scrambled, int(words[2]), int(words[4]))
    elif words[0] == "move":
        scrambled = move(scrambled, int(words[2]), int(words[5]))
    else:
        print("unknown instruction")
        exit(1)

print(scrambled)

