def process_commands(n, commands):
# 初始化撲克牌順序,將 T 用 10 取代
deck = [f"{suit}{rank}" for suit in 'SHDF' for rank in 'A23456789TJQK']
deck = [card.replace('T', '10') for card in deck]
# 處理每個命令
for command in commands:
parts = command.split()
cmd = int(parts[0])
if cmd == 1:
a = int(parts[1]) - 1
b = int(parts[2])
# 將第 a 張到第 b 張牌移動到最上層
selected_cards = deck[a:b]
deck = selected_cards + deck[:a] + deck[b:]
elif cmd == 2:
a = int(parts[1]) - 1
b = int(parts[2])
# 將第 a 張到第 b 張牌移動到最下層
selected_cards = deck[a:b]
deck = deck[:a] + deck[b:] + selected_cards
elif cmd == 3:
k = int(parts[1])
# 將最底下 k 張牌移動到最上層
selected_cards = deck[-k:]
deck = selected_cards + deck[:-k]
elif cmd == 4:
k = int(parts[1])
# 將最上面 k 張牌移動到最下層
selected_cards = deck[:k]
deck = deck[k:] + selected_cards
# 輸出最上面的 5 張牌
result = deck[:5]
# 將 T 替換為 10
result = [card.replace('T', '10') for card in result]
return result
# 讀取輸入
import sys
input = sys.stdin.read
data = input().strip().split('\n')
n = int(data[0])
commands = data[1:]
# 處理命令並輸出結果
result = process_commands(n, commands)
print(' '.join(result))