https://www.acmicpc.net/problem/1927
import heapq
n = int(input())
heap = []
for i in range(n):
x = int(input())
if x == 0:
if heap:
low =heapq.heappop(heap)
else:
low = 0
print(low)
else:
heapq.heappush(heap,x)
import heapq
n = int(input())
heap = []
ans = []
for i in range(n):
x = int(input())
if x == 0:
if heap:
low =heapq.heappop(heap)
else:
low = 0
ans.append(str(low))
else:
heapq.heappush(heap,x)
result = '\n'.join(ans)
print(result)