```python
"""Replay a notepad editing session: type, erase, replace, undo and redo."""


def replay_notepad(commands):
    buffer = ""
    past = []
    future = []
    for command in commands:
        action = command[0]
        if action == "type":
            past.append(buffer)
            buffer += command[1]
        elif action == "erase":
            past.append(buffer)
            buffer = buffer[: len(buffer) - command[1]]
        elif action == "replace":
            old, new = command
            if old not in buffer:
                raise ValueError("Replace text not found in the buffer")
            past.append(buffer)
            buffer = buffer.replace(old, new, 1)
        elif action == "undo":
            for _ in range(command[1]):
                future.append(buffer)
                buffer = past.pop()
        elif action == "redo":
            for _ in range(command[1]):
                past.append(buffer)
                buffer = future.pop()
    return buffer
```