expand-it-1

Ma5termind and Subway are bored of their normal life.They want to do something interesting so that they can enjoy their last sem of college life.As usual Ma5termind comes up with a simple and interesting game.

Ma5termind gives Subway a compressed string. A compressed String is composed of characters and numbers.Every character in a compressed string contains a number. Ex a3b1c2 it represents aaabcc. Other compressed string could be a22b2a1 , z5c2x36. Each number in the compressed string indicates the number of times the corresponding character occurs in the string. The final string is 1 indexed.  Ma5termind wants Subway to sort the compressed string and tell him the kth character in the sorted compressed string.Since subway got speed Ma5termind asks him various such questions.

Help Subway in answering Ma5termind's question.

Input:

The first line contains the compressed string.This is followed by Q that indicates the number of questions Ma5termind shoots on Subway.Follows Q lines each line contain an integer K.

Output:

For every question output the Kth  character if it exists else print -1 .

Constraints:

2 ≤ Length of Compressed String ≤ 10^4

1 ≤ Length of Final String ≤ 10^18

1 ≤ Q ≤ 10^5

1 ≤ K ≤ 10^18

Every character in the string is lowercase English letter.

Note:

The number for a character may contain leading zeroes.

SAMPLE INPUT
a2b3c2a1
4
2
5
1
8

SAMPLE OUTPUT
a
b
a
c

Explanation

The final sorted string is aaabbbcc.

Source: hackerearth | Difficulty: EASY


Solutions:
compressed = input().strip()
import re
#uncompressed = ""

match = re.findall("([a-z])(\d+)",compressed)

freq_char = {}
for each_tuple in match:
	freq_char[each_tuple[0]] = 0

for each_tuple in match:
	freq_char[each_tuple[0]] += int(each_tuple[1])

chars = sorted(freq_char.keys())

#print chars
#print freq_char

prev = 0
for char in chars:
	freq_char[char] += prev
	prev = freq_char[char]

#print freq_char


def find_char(chars,freq_dict,index):

	prev = 0
	for char in chars:
		if index >= prev and index <= freq_dict[char] :
			return char
		prev = freq_char[char]
	return -1


Q = int(input())
while Q!=0:
	index = int(input().strip())
	print(find_char(chars,freq_char,index))
	Q -= 1


class Stack:
	def __init__(self):
	    self.container = []  # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`.  You want your stack to *have* a list, not *be* a list.

	def isEmpty(self):
	    return self.size() == 0   # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it.  And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.

	def push(self, item):
	    self.container.append(item)  # appending to the *container*, not the instance itself.

	def pop(self):
	    return self.container.pop()  # pop from the container, this was fixed from the old version which was wrong

	def size(self):
	    return len(self.container) 

def start_counting(counter):
	s = Stack()
	counter += 1
	   while len(input_string) > counter and (ord(input_string[counter]) >= ord('0') and ord(input_string[counter]) <= ord('9')):
		s.push(int(input_string[counter]))
		counter += 1
	   multiplier = 1
	   result = 0
	   while (not s.isEmpty()):
			 result += multiplier*s.pop()
			 multiplier *= 10
	   counter -= 1
	return result, counter-1

input_string = input('')
Q = int(input(''))
alphabet_count = [0]*26
for counter in range(0, len(input_string)):
	if (input_string[counter] >= 'a' and input_string[counter] <= 'z'):
		temp = input_string[counter]
		count, counter = start_counting(counter)
		alphabet_count[ord(temp) - 97] += count
for counter1 in range(0, Q):
	K = int(input(''))
	present = 0
	result = -1
	for counter in range(0, 26):
		if present+alphabet_count[counter] >= K:
			result = chr(ord('a')+counter)
			break
		else:
			present += alphabet_count[counter]
	print(result)



import re
compr = input()
parts = re.findall('[a-z]+\d+', compr)
parts = sorted(parts)
buckets = {}
string_length = 0
for p in parts:
	if p[0] in buckets:
		buckets[p[0]] += int(p[1:])
		string_length += int(p[1:])
	else:
		buckets[p[0]] = int(p[1:])
		string_length += int(p[1:])

bucket_keys = sorted(buckets)
for N in range(int(input())):
	index = int(input())
	sum_index = 0
	if index > string_length:
		print(-1)
		continue
	found = False
	for k in bucket_keys:
		v = buckets[k]
		if (sum_index + v) >= index:
			print(k)
			found = True
			break
		else:
			sum_index +=v
	if not found:
		print(-1)


comp_string=input()
no='0123456789'
char=comp_string[0]
times='0'
#final_string=''

dic={0:'a',1:'b',2:'c',3:'d',4:'e', 5:'f', 6:'g', 7:'h', 8:'i', 9:'j', 10:'k', 11:'l', 12:'m', 13:'n', 14:'o', 15:'p', 16:'q', 17:'r', 18:'s', 19:'t', 20:'u', 21:'v', 22:'w', 23:'x', 24:'y', 25:'z'}
lst=[0]*26
dic_alph={'a':0, 'b':0, 'c':0, 'd':0, 'e':0, 'f':0, 'g':0, 'h':0, 'i':0, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0, 'o':0, 'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':0, 'v':0, 'w':0, 'x':0, 'y':0, 'z':0}
di={'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8, 'j':9, 'k':10, 'l':11, 'm':12, 'n':13, 'o':14, 'p':15, 'q':16, 'r':17, 's':18, 't':19, 'u':20, 'v':21, 'w':22, 'x':23, 'y':24, 'z':25}

for i in comp_string:
	#print 'i:',i
	if i in no:
		times+=i
		#print 'in if, times:',times
	else:
		#final_string+=char*int(times)
		lst[di[char]]+=int(times)
		char=i
		times='0'
		#print 'in else, lst,char,times:',lst,char,times
lst[di[char]]+=int(times) #for adding last char to final_string
#print 'lst:',lst        

#sorted_final_string=sorted(final_string)
#print sorted_final_string




tc=eval(input())
while tc:
	tc=tc-1
	index=eval(input())
	if index>sum(lst):
		print(-1)
	else:
		summ=0
		for i in range(26):
			summ+=lst[i]
			if summ>=index:
				print(dic[i])
				break



s= str(input())
n= len(s)
alp= [0]*26
i=0
while i<n:
	if 'a'<=s[i]<='z':
		ch= s[i]
		i=i+1
		su=0
		while i<n and '0'<=s[i]<='9':
			su= su*10+ int(s[i])
			i+=1 
		ind= ord(ch)-ord('a')
		alp[ind]+= su

#print alp 
summ= sum(alp) 
q= int(eval(input()))
for qq in range(q):
	k= int(eval(input()))
	#print k
	if k> summ:
		print("-1")
		continue 
	x=0 
	for j in range(26):
	    x+= alp[j]
	    if k<=x:
	        print(chr(97+j))
	        break 
		


s = input()
q = int(input())
l = [] #find characters first
for i in range(len(s)):
	if ord(s[i]) <= 122 and ord(s[i]) >= 97:
		l.append(i)


chars = [s[i] for i in l] # list of chars

vals = [] #lengths of characters
for i in range(len(l)-1):
	vals.append(int(s[l[i]+1:l[i+1]]))
vals.append(int(s[l[-1]+1:]))

# print vals
# print chars
length_map = {}
for i in range(len(vals)):
	length_map[chars[i]] = 0
for i in range(len(vals)):
	length_map[chars[i]]+=vals[i]
# print length_map
sort_keys = sorted(length_map)
# print sort_keys
sort_keys = [[i,length_map[i]] for i in sort_keys]
# print sort_keys
total_length = sum(length_map.values())
# print total_length

for i in range(q):
	k = int(input())
	if k > total_length:
		print(-1)
	else:
		# print k
		for i in sort_keys:
			# print k,i
			k = k-i[1]
			if k <= 0:
				print(i[0])
				break


countArr = {}
str = input()
length = len(str)

#count distinct characters
st = []
currChar = ''
num = 0
flag = False
for i in range(length):

	if str[i].isalpha():

		#update previous count
		if(flag):
			if currChar not in countArr:
				countArr[currChar] = 0
				st.append(currChar)
				
			countArr[currChar] = countArr[currChar] + num
				
				
		#start new count
		currChar = str[i]
		num = 0
		flag = True
	else:
		num = num*10+int(str[i])


#### update last letter count
if currChar not in countArr:
	countArr[currChar] = 0
	st.append(currChar)

countArr[currChar] = countArr[currChar] + num
st.sort()


T = int(input())
for i in range(T):
	K = int(input())
	ans = '-1'
	for letter in st:
		if K <= countArr[letter]:
			ans = letter
			break

		K = K - countArr[letter]

	print(ans)    

	


from string import ascii_lowercase
str1=input()
dic={}
q=int(input())
i=0
while i<len(str1):
	l=i
	num=0
	while(l<len(str1) and str1[l]>='0' and str1[l]<='9'):
	    num=num*10 + int(str1[l])
	    l=l+1
	if str1[i]>='0' and str1[i]<='9':

		if str1[i-1] not in dic:
		    dic[str1[i-1]]=num
		else:
		    dic[str1[i-1]] = dic[str1[i-1]]+num
		i=l
		continue
	i=i+1

while(q>0):
  k=int(input())

  for i in ascii_lowercase:
	if i in dic:
	    k=k-dic[i]
	if(k<=0):
	    print(i)
	    break
  if(k>0):
  	print('-1')

  q=q-1




import collections
s=input()
n=int(input())
d=""
c=""
f=0
dict={}
for i in range(len(s)):
	if s[i].isalpha():
		c=s[i]
		d=""
		for j in range(i+1,len(s)):
			if s[j].isalpha():
				break
			if s[j].isdigit():
				d=d+s[j]
		e=int(d)
		if c in dict:
			f=dict[c]
			dict[c]=f+e
		else:
			dict[c]=e
od = collections.OrderedDict(sorted(dict.items()))
sum=0
len=0
for key, value in od.items():
		len=len+value
while n:
	k=int(input())
	if k>len:
		print(-1)
	else:
		sum=0
		for key, value in od.items():
			sum=sum+value
			if sum>=k:
				print(key)
				break

	n-=1




import re
from collections import defaultdict

# s = "a2b3c2a1"
s = input()
n = int(input())

tokens = re.split('(\d+)',s)
d = defaultdict(int)
# d = [d[k]+=int(v) for k,v in zip(tokens[::2], tokens[1::2])]
for k,v in zip(tokens[::2], tokens[1::2]):
	d[k] += int(v)
# print sorted(d.items())
ds = sorted(d.items())
while n>0:
	q = int(input())

	sm = 0
	for k,v in ds:
		# print v, q
		if v >= q:
			q = 0
			print(k)
			break
		q -= v
	if q > 0:
		print(-1)
	n -= 1


str1 = input()
keyss = []
a = {}
j=0
while j<len(str1):
	if str1[j].isalpha():
		kk = str1[j]
		keyss.append(kk)
		j+=1
		val = ''
		while(not(str1[j].isalpha()) ):
			val+=str1[j]
			j+=1
			if j==len(str1):
			    break
		try:
		    a[kk] += int(val)
		except:
		    a[kk] = int(val)
		
keyss =list(set(keyss))
keyss.sort()
#print a, keyss

q = int(eval(input()))
for i in range(q):
	x = eval(input())
	cnt=0
	flag=0
	for gg in keyss:
	    #print gg
	    cnt+=a[gg]
	    if(cnt>=x):
	        flag=1
	        break
	    
	if flag==1:
	    print(gg)
	else:
	    print(-1)
	    
	        


str1 = input()
keyss = []
a = {}
j=0
while j<len(str1):
	if str1[j].isalpha():
		kk = str1[j]
		keyss.append(kk)
		j+=1
		val = ''
		while(not(str1[j].isalpha()) ):
			val+=str1[j]
			j+=1
			if j==len(str1):
			    break
		try:
		    a[kk] += int(val)
		except:
		    a[kk] = int(val)
		
keyss =list(set(keyss))
keyss.sort()
#print a, keyss

q = int(eval(input()))
for i in range(q):
	x = eval(input())
	cnt=0
	flag=0
	for gg in keyss:
	    #print gg
	    cnt+=a[gg]
	    if(cnt>=x):
	        flag=1
	        break
	    
	if flag==1:
	    print(gg)
	else:
	    print(-1)
	    
	        


s = input()
q = input()
map = {}

for i in range(26):
	map[i] = 0

i = 0
while i < len(s):
	j = i+1;
	while j<len(s) and s[j].isdigit():
		j = j+1;
	frq = int(s[i+1:j])
	map[ord(s[i])-ord('a')]+=frq
	i = j


for i in range(26):
	if i>0:
		map[i]+=map[i-1]

i = 0					
while i < int(q):
	query = input()
	ans = '-1'
	for key in map:
		if map[key] > int(query):
			ans = chr(ord('a')+key)
			break
		elif map[key] == int(query):
			ans = chr(ord('a')+key)
			break
		elif map[key] < int(query) and key < 25:
			ans = chr(ord('a')+key)
		elif map[key] < int(query) and key == 25:
			ans = '-1'
	i+=1
	print(ans)


class Stack:
	def __init__(self):
	    self.container = []  # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`.  You want your stack to *have* a list, not *be* a list.

	def isEmpty(self):
	    return self.size() == 0   # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it.  And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.

	def push(self, item):
	    self.container.append(item)  # appending to the *container*, not the instance itself.

	def pop(self):
	    return self.container.pop()  # pop from the container, this was fixed from the old version which was wrong

	def size(self):
	    return len(self.container) 

def start_counting(counter):
	s = Stack()
	counter += 1
	   while len(input_string) > counter and (ord(input_string[counter]) >= ord('0') and ord(input_string[counter]) <= ord('9')):
		s.push(int(input_string[counter]))
		counter += 1
	   multiplier = 1
	   result = 0
	   while (not s.isEmpty()):
			 result += multiplier*s.pop()
			 multiplier *= 10
	   counter -= 1
	return result, counter-1

input_string = input('')
Q = int(input(''))
alphabet_count = [0]*26
for counter in range(0, len(input_string)):
	if (input_string[counter] >= 'a' and input_string[counter] <= 'z'):
		temp = input_string[counter]
		count, counter = start_counting(counter)
		alphabet_count[ord(temp) - 97] += count
for counter1 in range(0, Q):
	K = int(input(''))
	present = 0
	result = -1
	for counter in range(0, 26):
		if present+alphabet_count[counter] >= K:
			result = chr(ord('a')+counter)
			break
		else:
			present += alphabet_count[counter]
	print(result)



def precompute():
#	print length
	nextindex=0
	while(nextindex<length):
		global index
		global previndex
		nextindex,count=findnextchar(index+1)
#		print nextindex,count
	
		power=0
		sum=lista[ord(stra[previndex])-97]
		for x in reversed(count):
			sum=sum+x*10**power
			power=power+1
		lista[ord(stra[previndex])-97]=sum
		previndex=nextindex
		index=nextindex
#	print lista
	
	
def findnextchar(index):

	count=[]
	while(ord(stra[index])<97):
		count.append(int(stra[index]))
		index=index+1
#		print "index",index
		if index==length:
			break
		
	return index,count
	

stra=input()
length=len(stra)
lista=[0]*26
previndex=0
index=0
precompute()


Queries=int(input())
length=len(lista)
for q in range(Queries):
	query=int(input())
	sum=0
	index=0
	flag=0
	
	for x in range(length):
		sum=sum+lista[x]
		if sum>=query:
			index=x
			flag=1
			break
	if flag:
		print(chr(97+index))
	else:
		print("-1")
	


s= str(input())
n= len(s)
alp= [0]*26
i=0
while i<n:
	if 'a'<=s[i]<='z':
		ch= s[i]
		i=i+1
		su=0
		while i<n and '0'<=s[i]<='9':
			su= su*10+ int(s[i])
			i+=1 
		ind= ord(ch)-ord('a')
		alp[ind]+= su

#print alp 
summ= sum(alp) 
q= int(eval(input()))
for qq in range(q):
	k= int(eval(input()))
	#print k
	if k> summ:
		print("-1")
		continue 
	x=0 
	for j in range(26):
	    x+= alp[j]
	    if k<=x:
	        print(chr(97+j))
	        break 
		


s = input()
q = int(input())
l = [] #find characters first
for i in range(len(s)):
	if ord(s[i]) <= 122 and ord(s[i]) >= 97:
		l.append(i)


chars = [s[i] for i in l] # list of chars

vals = [] #lengths of characters
for i in range(len(l)-1):
	vals.append(int(s[l[i]+1:l[i+1]]))
vals.append(int(s[l[-1]+1:]))

# print vals
# print chars
length_map = {}
for i in range(len(vals)):
	length_map[chars[i]] = 0
for i in range(len(vals)):
	length_map[chars[i]]+=vals[i]
# print length_map
sort_keys = sorted(length_map)
# print sort_keys
sort_keys = [[i,length_map[i]] for i in sort_keys]
# print sort_keys
total_length = sum(length_map.values())
# print total_length

for i in range(q):
	k = int(input())
	if k > total_length:
		print(-1)
	else:
		# print k
		for i in sort_keys:
			# print k,i
			k = k-i[1]
			if k <= 0:
				print(i[0])
				break


s = input()
q = int(input())
li = []
di = {}
tlen = 0

number = 0
key = s[0]
di[key] = 0
li.append(key)

for k in range(1, len(s)):
	i = s[k]
	if not i.isdigit():			# i is a character
		di[key] += number
		tlen += number
		number = 0
		key = i
		if di.get(i) is None:	# i was not present already
			li.append(i)
			di[i] = 0
			
	else:						# i is a digit
		if not s[k-1].isdigit():	# prev char was a char
			number += int(i)
		else:	# prev char was a digit
			number = number*10 + int(i)

li.sort()

di[key] += number
tlen += number

while q > 0:
	x = int(input()) 
	index = 0
	
	q -= 1

	if x < 1:
		print(-1)
		continue

	if x > tlen:
		print(-1)
		continue

	for i in li:
		index += di[i]
		if index >= x:
			print(i)
			break
	


from string import ascii_lowercase
str1=input()
dic={}
q=int(input())
i=0
while i<len(str1):
	l=i
	num=0
	while(l<len(str1) and str1[l]>='0' and str1[l]<='9'):
	    num=num*10 + int(str1[l])
	    l=l+1
	if str1[i]>='0' and str1[i]<='9':

		if str1[i-1] not in dic:
		    dic[str1[i-1]]=num
		else:
		    dic[str1[i-1]] = dic[str1[i-1]]+num
		i=l
		continue
	i=i+1

while(q>0):
  k=int(input())

  for i in ascii_lowercase:
	if i in dic:
	    k=k-dic[i]
	if(k<=0):
	    print(i)
	    break
  if(k>0):
  	print('-1')

  q=q-1




import collections
s=input()
n=int(input())
d=""
c=""
f=0
dict={}
for i in range(len(s)):
	if s[i].isalpha():
		c=s[i]
		d=""
		for j in range(i+1,len(s)):
			if s[j].isalpha():
				break
			if s[j].isdigit():
				d=d+s[j]
		e=int(d)
		if c in dict:
			f=dict[c]
			dict[c]=f+e
		else:
			dict[c]=e
od = collections.OrderedDict(sorted(dict.items()))
sum=0
len=0
for key, value in od.items():
		len=len+value
while n:
	k=int(input())
	if k>len:
		print(-1)
	else:
		sum=0
		for key, value in od.items():
			sum=sum+value
			if sum>=k:
				print(key)
				break

	n-=1




s = input()
q = int(input())
l = [0]*26
count = 0
chr1 = 'a'
s = s.strip()
for c in s:
	if c >= '0' and c<='9':
		count = count*10 + int(c)
	elif c>='a' and c<='z':
		if (count > 0):
			l[chr1] += count
		count = 0
		chr1 = ord(c) - ord('a')
l[chr1] += count
for i in range(1, 26):
	l[i] += l[i-1]
while q>0:
	q -= 1
	k = int(input())
	i = 0
	for i in range(0, 26):
		if k<=l[i]:
			print(chr(i+ord('a')))
			break;
	else:
		print("-1")


def is_alphabet(char):
	return char.isalpha()

def is_digit(char):
	return char.isdigit()

def get_number(num_str):
    return int(num_str)

def generate_cumulative(alphacount):
    sum = 0
    for i in range(26):
        alphacount[i]=sum+alphacount[i]
        sum=alphacount[i]
        i+=1
    return  alphacount

def get_num_in_str(place, alphacount):
    for i in range(26):
        if alphacount[i]>=place:
            return chr(i+ord('a'))
    return -1

compressed_string=input()
def process_input(compressed_string):
    i=0
    compressed_string_length=len(compressed_string)
    alphacount = [0]*26
    while i<compressed_string_length:

        if is_alphabet(compressed_string[i]):
                alphabet = compressed_string[i].lower()
                i+=1
                current_alpha_count = 0
                while i<compressed_string_length and is_digit(compressed_string[i]):
                    current_alpha_count =current_alpha_count*10+int(compressed_string[i])
                    i+=1
                alphacount[ord(alphabet)-ord('a')]+=current_alpha_count
    return alphacount
alphacount = process_input(compressed_string)
alphacount = generate_cumulative(alphacount)
no_of_inputs = input()
for i in range(int(no_of_inputs)):
    position = input()
    print(get_num_in_str(int(position),alphacount))




from string import ascii_lowercase

input1 = input()
freq = {}
for i in ascii_lowercase:
	freq[i] = 0

cur_character='a'
cur_frequency=0
temp = input1+"z"
for c in temp:
	if c >= 'a' and c <= 'z':
		# print c
		freq[cur_character] += int(cur_frequency)
		cur_character = c
		cur_frequency = 0
	else:
		cur_frequency = 10*cur_frequency + int(c)

T = int(input())
while T > 0:
	k = int(input())
	done = False
	for cr in ascii_lowercase:
		if k - freq[cr] <= 0:
			print(cr)
			done = True
			break
		else:
			k = k - freq[cr]
	if not done:
		print('-1')
	T -= 1


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''

compressed_string = input()
num_questions = int(input())
uncompressed = {}
running_char = ''
total = 0

for char in compressed_string:
	if char.isalpha():
		if running_char != "":
			cnt = uncompressed.get(running_char, 0)
			cnt += int(running_num)
			total += int(running_num)
			uncompressed[running_char] = cnt

		running_char = char
		running_num = ""
	else:
		running_num += char

cnt = uncompressed.get(running_char, 0)
cnt += int(running_num)
total += int(running_num)
uncompressed[running_char] = cnt

chars = list(uncompressed.keys())
chars.sort()

for _ in range(0, num_questions):
	q = int(input(''))
	if q > total:
		print(-1)
	else:
		for char in chars:
			if q <= uncompressed[char]:
				print(char)
				break
			else:
				q = q - uncompressed[char]



'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
s = input().lower()
q = int(input())

def sortedString(s):
	count = [0]*26
	x = 0
	while x<len(s):
		if 97<=ord(s[x])<=122:
			x+=1
			continue
		else:
			num = s[x]
			prevChar = x-1
			x+=1
			while x<len(s) and 48<=ord(s[x])<=57:
				num+=s[x]
				x+=1
				# print num
			count[ord(s[prevChar])-ord('a')]+=int(num)
	# returnString = ''
	# for i in xrange(len(count)):
	# 	if(count[i]>0):
	# 		returnString+=chr(i+97)*count[i]
	return count

count = sortedString(s)
# print count
for x in range(1,len(count)):
	count[x]+=count[x-1]
# def binarySearch(c,low,high):
# 	mid = (low+high)>>1
# 	if(count[mid-1]count[mid]
while q>0:
	i = int(input())
	found = False
	for x in range(len(count)):
		if(i<=count[x]):
			print(chr(97+x))
			found = True
			break
	if(not found):
		print(-1)
	q-=1


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
keylist = []
RangeList = []
totalcharacters= 0
totallength=0
def SortOrder(S):
	dic=dict() ## Hashing
	length = len(S)
	global totalcharacters
	global keylist
	global RangeList
	global totallength
	val =0
	char =''
	for i in range(0,length):
		if ord(S[i])>=97 and ord(S[i])<=122:
			if(char!=''):
				if char in dic:
					dic[char]+=val
				else:
					dic[char] = val
			char = S[i]
			val =0
		else:
			val= val*10+int(S[i])
	if char in dic:
					dic[char]+=val
	else:
					dic[char] = val
	keylist = list(dic.keys())
	keylist.sort()
	
	for key in keylist:
		RangeList.append(dic[key])
	totalcharacters = len(keylist)
	totallength = sum(RangeList)
def FindCharacter(K):
	total = 0
	if K == 0 or K > totallength:
		return -1
	i =0
	while i<totalcharacters:
		total+=RangeList[i]
		if total <K:
			i+=1
		else :
			return keylist[i]
String = input()
Q = int(input())
SortOrder(String)
for _ in range(Q):
	K = int(input())
	print(FindCharacter(K))

	




comp_string=input()
no='0123456789'
char=comp_string[0]
times='0'
#final_string=''

dic={0:'a',1:'b',2:'c',3:'d',4:'e', 5:'f', 6:'g', 7:'h', 8:'i', 9:'j', 10:'k', 11:'l', 12:'m', 13:'n', 14:'o', 15:'p', 16:'q', 17:'r', 18:'s', 19:'t', 20:'u', 21:'v', 22:'w', 23:'x', 24:'y', 25:'z'}
lst=[0]*26
dic_alph={'a':0, 'b':0, 'c':0, 'd':0, 'e':0, 'f':0, 'g':0, 'h':0, 'i':0, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0, 'o':0, 'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':0, 'v':0, 'w':0, 'x':0, 'y':0, 'z':0}
di={'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8, 'j':9, 'k':10, 'l':11, 'm':12, 'n':13, 'o':14, 'p':15, 'q':16, 'r':17, 's':18, 't':19, 'u':20, 'v':21, 'w':22, 'x':23, 'y':24, 'z':25}

for i in comp_string:
	#print 'i:',i
	if i in no:
		times+=i
		#print 'in if, times:',times
	else:
		#final_string+=char*int(times)
		lst[di[char]]+=int(times)
		char=i
		times='0'
		#print 'in else, lst,char,times:',lst,char,times
lst[di[char]]+=int(times) #for adding last char to final_string
#print 'lst:',lst        

#sorted_final_string=sorted(final_string)
#print sorted_final_string




tc=eval(input())
while tc:
	tc=tc-1
	index=eval(input())
	if index>sum(lst):
		print(-1)
	else:
		summ=0
		for i in range(26):
			summ+=lst[i]
			if summ>=index:
				print(dic[i])
				break



import collections
s=input()
n=int(input())
d=""
c=""
f=0
dict={}
for i in range(len(s)):
	if s[i].isalpha():
		c=s[i]
		d=""
		for j in range(i+1,len(s)):
			if s[j].isalpha():
				break
			if s[j].isdigit():
				d=d+s[j]
		e=int(d)
		if c in dict:
			f=dict[c]
			dict[c]=f+e
		else:
			dict[c]=e
od = collections.OrderedDict(sorted(dict.items()))
sum=0
len=0
for key, value in od.items():
		len=len+value
while n:
	k=int(input())
	if k>len:
		print(-1)
	else:
		sum=0
		for key, value in od.items():
			sum=sum+value
			if sum>=k:
				print(key)
				break

	n-=1




a=input()
c=[0]*200
#p=[]
i=0;x=len(a);y=0
while (i<x-1):
	#print "shit"
	p=[]
	j=i
	while(i<x-1 and ord(a[i+1])<97):
		d=ord(a[i+1])-48
		p.append(d)
		i+=1
	#print p
	#i-=1
	if i<x-1:
		i+=1
	h=''.join(map(str,p))
	#print int(h)
	c[ord(a[j])]+=int(h)
	y+=int(h)
		
#print p
#print c[90:150]
q=int(input())
for _ in range(q):
	n=int(input())
	v=k=0
	#if(n==y):
	#	print 
	for i in range(90,150):
		v+=c[i]
		if(n<=v):
			print(chr(i))
			k=1
			break
	if k==0:
		print(-1)


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''

def get_frequency(compressed_string):
	frequency = [0]*27
	i = 0 
	for i in range(len(compressed_string)):
		character = compressed_string[i]
		if character.isalpha():
			freq = ""
			j = i+1
			for j in range(i+1, len(compressed_string)):
				if compressed_string[j].isalpha():
					i = j-1
					break
				else:
					freq += compressed_string[j]
			index = ord(character) - 97
			frequency[index] += int(freq)
	return frequency
	
compressed_string = input()
no_of_questions = eval(input())
frequency = get_frequency(compressed_string)
for i in  range(no_of_questions):
	index = eval(input())
	temp_index = 0
	success = 0
	if index:
		for j in range(len(frequency)):
			if index <= temp_index:
				print(chr(j+96))
				success = 1
				break
			else:
				temp_index += frequency[j]
	if not success:
		print(-1)