class Exp
	def initialize(*args)
		@children = args
	end
	def to_s
		@children.map(&:to_s).join
	end
	def size
		@children.map(&:size).inject(:+)
	end

	def [](k)
		s = @children[0].size
		if k < s
			@children[0][k]
		else
			@children[1][k-s]
		end
	end
end

class Term
	def initialize(*args)
		@children = args
	end
	def to_s
		if @children[0].class == Num
			@children[1].to_s * @children[0].value.to_i
		else
			@children[0].to_s
		end
	end
	def size
		if @children[0].class == Num
			@children[1].size * @children[0].value.to_i
		else
			@children[0].size
		end
	end

	def [](k)
		if @children[0].class == Num
			@children[1][k % @children[1].size]
		else
			@children[0].to_s
		end
	end
end

class Alph
	def initialize(value)
		@value = value
	end
	def to_s
		@value
	end
	def size
		1
	end
	def [](k)
		@value
	end
end

class Num
	def initialize(value)
		@value = value
	end

	def size
		@value.size
	end
	attr_reader :value
end

def get_exp(tokens, i)
	return [nil, i] if i >= tokens.size
	t, i = get_term(tokens, i)
	return [nil, i] unless t

	e, i = get_exp(tokens, i)
	if e
		return [Exp.new(t, e), i]
	else
		return [Exp.new(t), i]
	end
end

def get_term(tokens, i)
	return [nil, i] if i >= tokens.size

	a, j = get_alph(tokens, i)
	if a
		return [Term.new(a), j]
	end

	n, j = get_num(tokens, i)
	return [nil, i] unless n

	a, k = get_alph(tokens, j)
	if a
		return [Term.new(n, a), k]
	end

	e, k = get_exp(tokens, j+1)
	if tokens[j] == ?( && e && tokens[k] == ?)
		return [Term.new(n, e), k+1]
	else
		return [nil, i]
	end
end

def get_alph(tokens, i)
	return [nil, i] if i >= tokens.size
	if ('A'..'Z') === tokens[i] 
		return [Alph.new(tokens[i]), i+1]
	end
	[nil, i]
end

def get_num(tokens, i)
	return [nil, i] if i >= tokens.size
	if tokens[i] =~ /^\d+$/
		return [Num.new(tokens[i]), i+1]
	end
	[nil, i]
end

loop {
	s, n = gets.chomp.split
	break if s == ?0
	n = n.to_i

	tokens = s.scan(/\d+|[()\w]/)
	e, _ = get_exp(tokens, 0)
	if n < e.size
		puts e[n]
	else
		puts 0
	end
}