$VERBOSE = nil

def r(s)
	s.reverse	
end

class String
	def transform!
		tr!("+", "!")
		gsub!(/[\w\]](?!$|\])/){|c| "#{c} + "}
		gsub!(/\w/){|c| "?#{c}"}
		0 while gsub!(/\[(.+?)\]/){|x| "r(#{$1})"}
	end

	def !@
		(?A.ord + (ord - ?A.ord + 1) % 26).chr
	end
	def -@
		(?A.ord + (ord - ?A.ord + 25) % 26).chr
	end
end

loop do
	s = gets.chomp
	break if s == ?.
	0 while s.gsub!("+-", "")
	0 while s.gsub!("-+", "")
	0 while s.gsub!("+-", "")
	0 while s.gsub!("-+", "")
	0 while s.gsub!("+-", "")
	0 while s.gsub!("-+", "")

	k = s.count(??)
	if k == 0
		s.transform!
		puts eval(s)
	else
		a = []
		(?A..?Z).to_a.repeated_permutation(k) do |perm|
			e = perm.inject(s){|t, c| t.sub(??, c)}
			e0 = e.dup
			e.transform!
			begin 
				a << eval(e)
			rescue Exception
			end
		end	
		puts a.min
	end
end