$t=gets.chop
def f c,a,b,p=0
a=a.to_i
b=b.to_i
case c
when 'print'
puts $t[a..b]
$t
when 'reverse'
$t[0...a]+$t[a..b].reverse+$t[b+1..-1]
when 'replace'
$t[0...a]+p+$t[b+1..-1]
end
end
gets.to_i.times do
$t=f *gets.split
end
