x = int(input())
table = list()
for i in range(x):
  table.append(list(map(int,input().split(" "))))

Sum = 0

RealNumbers = list()

for q in range(len(table)):
    if table[q][0] == 0:
        del RealNumbers[len(RealNumbers)-1:]
    else:
        RealNumbers.append(table[q][0])
for z in range(len(RealNumbers)):
    Sum += RealNumbers[z]

print(Sum)


72142b011090bde27c200e7b76c1128592b7c3c8
<mask token>

<mask token>
for i in range(x):
    table.append(list(map(int, input().split(' '))))
<mask token>
for q in range(len(table)):
    if table[q][0] == 0:
        del RealNumbers[len(RealNumbers) - 1:]
    else:
        RealNumbers.append(table[q][0])
for z in range(len(RealNumbers)):
    Sum += RealNumbers[z]
print(Sum)

x = int(input())
table = list()
for i in range(x):
    table.append(list(map(int, input().split(' '))))
Sum = 0
RealNumbers = list()
for q in range(len(table)):
    if table[q][0] == 0:
        del RealNumbers[len(RealNumbers) - 1:]
    else:
        RealNumbers.append(table[q][0])
for z in range(len(RealNumbers)):
    Sum += RealNumbers[z]
print(Sum)

x = int(input())
table = list()
for i in range(x):
  table.append(list(map(int,input().split(" "))))

Sum = 0

RealNumbers = list()

for q in range(len(table)):
    if table[q][0] == 0:
        del RealNumbers[len(RealNumbers)-1:]
    else:
        RealNumbers.append(table[q][0])
for z in range(len(RealNumbers)):
    Sum += RealNumbers[z]

print(Sum)

