Harryxun/stf_run huggingface dataset:
N=int(input())
while True:
    try:
        listA=list(map(int,input().split()))
        #listA<-[1,2,2,3,1]
        #ここに逐次処理を書けばいいと思う。
    except:
        break;

product=1
for i in listA:
  if product<10**18+1:
  	product=product*i
    
if listA.count(0)>0:
  print(0)
    
elif product<10**18+1:
  print(product)
  
elif product>=10**18+1:
  print(-1)


### FIXED CODE ###

x,y = map(int,input().split()) 	
print(x*y)
N = int(input())
V = list(map(int,input().split()))
C = list(map(int,input().split()))

cnt = 0
for i in range(N):
  if V[i]-C[i] >0:
    cnt += V[i]-C[i]
print(cnt)    

### FIXED CODE ###

A,B,T = map(int,input().split())
cnt = 0
n = A
while n <= T:
  n += A 
  cnt += B
print(cnt)
n = int(input())
hlist = list(map(int, input().split()))
delta = [hlist[i + 1] - hlist[i] for i in range(len(hlist) - 1)]

counter = 0
for i in range(len(delta)):
    if delta[i] <= 0:
        counter += 1
    else:
        ans = counter
        counter = 0
ans = max(ans, counter)
print(ans)


### FIXED CODE ###

n = int(input())
h = list(map(int, input().split()))

counter, ans = 0, 0
for i in range(len(h)-1):
    if h[i] >= h[i+1]:
        counter += 1
    else:
        if ans <= counter:
            ans = counter
        counter = 0
ans = max(ans, counter)


print(ans)
def comb(n,k,p):
  from math import factorial
  if n<0 or k<0 or n<k: return 0
  if n==0 or k==0: return 1
  a=factorial(n)%p
  b=factorial(k)%p
  c=factorial(n-k)%p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    return d*d %p
  if b%2==1:
    return (a*power_func(a,b-1,p))%p
h,w,a,b=map(int,input().split())
mod=10**9+7
if h-a<w-b:
  h,w=w,h
  a,b=b,a
t=0
for i in range(w-b):
  t+=comb((h-a-i)*(w-b+1+i),w-b+1+i)*comb((w-b-i)*(a+1+i),a+1+i)
print(t%mod)

### FIXED CODE ###

n=200000
mod=10**9+7
def pf(x,y,p):
  if y==0: return 1
  if y%2==0:
    d=pf(x,y//2,p)
    return d*d%p
  if y%2==1:
    return (x*pf(x,y-1,p))%p
facl=[1]
for i in range(1,n+2):
  facl.append(facl[i-1]*i%mod)
invl=[1]*(n+2)
for i in range(1,n+2):
  invl[i]=pf(facl[i],mod-2,mod)
def comb(x,k):
  if x<0 or k<0 or x<k: return 0
  if x==0 or k==0: return 1
  return (facl[x]*invl[k]*invl[x-k])%mod
h,w,a,b=map(int,input().split())
if h-a<w-b:
  h,w=w,h
  a,b=b,a
t=0
for i in range(w-b):
  t+=comb(h-a+b-1,b+i)*comb(w-b+a-1,a+i)
print(t%mod)
n = int(input())
a = list(map(int, input().split()))
ans = DENIED

for i in range(n):
  if a[i]%2 == 1:
    continue
  else:
    if a[i]%3==0 or a[i]%5==0:
      continue
    else:
      break
else:
  ans = APPROVED

print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
ans = 'DENIED'
 
for i in range(n):
  if a[i]%2 == 1:
    continue
  else:
    if a[i]%3==0 or a[i]%5==0:
      continue
    else:
      break
else:
  ans = 'APPROVED'
 
print(ans)
N=int(input())
a=str(N)
if int(a[1])==7:
  print("Yes")
elif int(a[-2])==7:
  print("Yes")
elif int(a[3])==7:
  print("Yes)
else:
  print("No")
   

### FIXED CODE ###

N=int(input())
a=str(N)
if '7' in a:
  print("Yes")
else:
  print("No")
import collections as c
d=c.defaultdict(list)
d2=c.defaultdict(list)
a,b,*q=map(int,open(0).read().split())
for s in range(b):
    g,j=q[2*s],q[2*s+1]
    d[g].append(j)
    d[g]=sorted(d[g])
for t in d.keys():
    for k,v in enumerate(d[t]):
        d2[t].append((v,k+1))
for s in range(b):
    g,j=q[2*s],q[2*s+1]
    for sss in d2[g]:
        if sss[0]==j:
            print('{:06d}'.format(g)+'{:06d}'.format(sss[1]))

### FIXED CODE ###

a,b=map(int,input().split());print(a+b//2)
s=input()
s=s.reverse()
voc=['dream','dreamer','erase','eraser']
for i in range(len(voc)):
  voc[i]=voc[i][::-1]
while True:
  flag=False
  for v in voc:
    if s[:len(v)]==v:
      s=s[len(v):]
      flag=True
  if s=='':
    print('Yes')
    break
  elif not flag:
    print('No')
    break

### FIXED CODE ###

S=input()
 
S=S.replace('eraser','')
S=S.replace('erase','')
S=S.replace('dreamer','')
S=S.replace('dream','')
 
if S=='':
    print('YES')
else:
    print('NO')
x = raw_input()
x**3

### FIXED CODE ###

x = int(input())
print(x**3)
A, B, C =map(int, input().split())
if A + B => C:
  print("Yes")
else:
  print("No")


### FIXED CODE ###

A, B, C =map(int, input().split())
if A + B < C:
  print("No")
else:
  print("Yes")
L,N=map(int,input().split())
l=[]
for i in range(N):
    S=input()
    l.append(S)
#print(l)
l2=sorted(l)
#print(l2)
ans="".join(l2)
print(ans)

### FIXED CODE ###

N,L=map(int,input().split())
l=[]
for i in range(N):
    S=input()
    l.append(S)
#print(l)
l2=sorted(l)
#print(l2)
ans="".join(l2)
print(ans)
def dfs(node, distance):
    for next_node, edge_size in G[node]:
        if ans[next_node] == -1:
            ans[next_node] = ((distance+edge_size) % 2)
            dfs(next_node, distance+edge_size)


N = int(input())

G = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    G[u-1].append([v-1, w])
    G[v-1].append([u-1, w])

ans = [-1]*N
dfs(0, 0)
print(*ans, sep='\n')


### FIXED CODE ###

import sys
sys.setrecursionlimit(20000000)
#input = sys.stdin.readline
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
    u,v,w = map(int,input().split())
    u-=1;v-=1
    g[u].append([v,w])
    g[v].append([u,w])
ans = [-1]*n
def dfs(x,y):
    for i,j in g[x]:
        if ans[i] == -1:
            ans[i] = (y+j)%2
            dfs(i,y+j)
dfs(0,0)
for i in ans:
    print(i)
S=input()
T=input()
a=0
for i in range(3):
    if S[i]==T[i]:
        a+=1
print(a)

### FIXED CODE ###

A,B=map(int,input().split())
a=0
t=1
while t<B:
    t+=A-1
    a+=1
print(a)
from collections import defaultdict
from heapq import heappush, heappop

def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf] * node

    dist[start] = 0
    heap = [(0, start)]
    while heap:
        cost, thisNode = heappop(heap)
        for NextCost, NextNode in graph[thisNode]:
            dist_cand = dist[thisNode] + NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode], NextNode))
    return dist
    # dist = [costs to nodes]


n, m = map(int, input().split())
graph = defaultdict(list)
edges = []
for i in range(m):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    edges.append((l, r, d))
    graph[l].append((d, r))
    graph[r].append((d, l))


dist = dijkstra(graph, n, edges[0][0])
possible = True
for l, r, d in edges:
    if d != abs(dist[l] - dist[r]):
        possible = False
        break

if possible:
    print("Yes")
else:
    print("No")


### FIXED CODE ###

from collections import deque

def topological_sort(graph: list, n_v: int) -> list:
    # graph[node] = [(cost, to)]
    indegree = [0] * n_v # 各頂点の入次数
    for i in range(n_v):
        for c, v in graph[i]:
            indegree[v] += 1

    cand = deque([i for i in range(n_v) if indegree[i] == 0])
    res = []
    while cand:
        v1 = cand.popleft()
        res.append(v1)
        for c, v2 in graph[v1]:
            indegree[v2] -= 1
            if indegree[v2] == 0:
                cand.append(v2)

    return res




n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for i in range(m):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    edges[l].append((d, r))

ts = topological_sort(edges, n)

if len(ts) != n:
    ans = "No"
    # print("Not DAG")
else:
    # DAG
    dist = [-1] * n
    ans = "Yes"
    for i in range(n):
        v = ts[i]
        if dist[v] == -1:
            dist[v] = 0

        for c, u in edges[v]:
            if dist[u] == -1:
                dist[u] = dist[v] + c
            elif dist[u] != dist[v] + c:
                ans = "No"
                break

print(ans)
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        if len(edge) != 2:
            exit(0)
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            try: 
                if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                    tmp_color += 1
                colors[edge[1]] = tmp_color
            except Exception as e:
                print(0)
            paint(edge[0], edge[1])
            tmp_color += 1

    

paint(tmp[0][0], -1)
print(tmp[0][1]) # color num 
for i in range(N-1):
    print(colors[i])

### FIXED CODE ###

import sys
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
sys.setrecursionlimit(10**7)
paint(tmp[0][0], -1)
print(tmp[0][1]) # color num 
for i in range(N-1):
    print(colors[i])
from collections import deque
import math

n,m,k = map(int,input().split())
li_a = deque(list(map(int,input().split())))
li_b = deque(list(map(int,input().split())))
ans,cnt = 0,0

for i in range(n+m+1):
    if n != 0:
        a_num = li_a[0]
    else:
        a_num = math.inf

    if m != 0:
        b_num = li_b[0]
    else:
        b_num = math.inf

    if a_num <= b_num:
        temp = a_num
        li_a.popleft()
        n -= 1
    else:
        temp = b_num
        li_b.popleft()
        m -= 1

    if cnt + temp > k:
        break
    else:
        cnt += temp
        ans += 1

print(ans)

### FIXED CODE ###

import numpy

n,m,k = map(int,input().split())
li_a = list(map(int,input().split()))
li_b = list(map(int,input().split()))

li_A = [0] + list(numpy.cumsum(li_a))
li_B = [0] + list(numpy.cumsum(li_b))

ans, j = 0,m
for i in range(n+1):
    if li_A[i] > k:
        break
    while li_B[j] + li_A[i]> k:
        j -= 1
    ans = max(ans, i + j)
print(ans)
if __name__ == "__main__":
    K = int(input())
    if (K == 0):
        print (2)
        print (0, 0)
        exit()
    if (K == 1):
        print (2)
        print (2, 0)

    if (K <= 50):
        print (K)
        res = []
        for x in range(K):
            res.append(x + 1)
        print (" ".join(map(str, res)))
        exit()
    assert(False)


### FIXED CODE ###

if __name__ == "__main__":
    K = int(input())
    N = 50
    result = list(range(N))
    for x in range(N):
        result[x] += K // N
    K %= N

    for x in range(K):
        result[x] += N + 1
        for i in range(N):
            result[i] -= 1
    print (len(result))
    print (" ".join(map(str, result)))
a =int(input()) 
b= int(input())
if a <= 8 and b <= 8:
    print('Yay!')
else:
    print(':(')

### FIXED CODE ###

a,b = map(int,input().split())
if a <= 8 and b <= 8:
    print('Yay!')
else:
    print(':(')
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


lim = 2*10**5   #必要そうな階乗の限界を入力

#階乗#
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod

#階乗の逆元#
fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod


H, W, A, B = MAP()

ans = 0
for n in range(B, W):
	way = C(H-A-1+n, n)*C(W-n-1+A-1, A-1)%mod
	ans = (ans+way)%mod

print(ans)


### FIXED CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

lim = 2*10**6
fact = [1]*(lim+1)
for n in range(1, lim+1):
	fact[n] = n*fact[n-1]%mod

fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod

H, W, A, B = MAP()

ans = 0
for n in range(B, W):
	way = C(H-A-1+n, n)*C(W-n-1+A-1, A-1)%mod
	ans = (ans+way)%mod

print(ans)
S = input()
i = 0
for a in 'AKIHABARA':
  if i >= len(S) and a == 'A':
    S = S + 'A'
    i += 1
  elif S[i] != a:
    if a == 'A':
      S = S[:i] + 'A' + S[i:]
      i += 1
    else:
      print('NO')
      exit()
  else:
    i += 1
print('YES')  


### FIXED CODE ###

S = input()
akb = 'AKIHABAR'
if len(S) <= len(akb)+1:
  i = 0
  for a in akb:
    if i >= len(S):
      print('NO')
      exit()
    if S[i] == a: i+=1
    elif S[i] != a and a == 'A': continue
    else:
      print('NO')
      exit()
  print('YES')
else:
  print('NO')
l = (int(input()) for i in range(1, 5))
print(min(l[0], l[1]) + min(l[2], l[3]))


### FIXED CODE ###

l = [int(input()) for i in range(1, 5)]
print(min(l[0], l[1]) + min(l[2], l[3]))
import math
from functools import reduce


def gcd_list(numbers):
    return reduce(math.gcd, numbers)
n,x=map(int,input().split())
l=[abs(c-x) for c in list(map(int,input().split()))]
print(gcd_list(l) if len(l)>1 else l[0])

### FIXED CODE ###

def gcd(a,b):
        while b:
            a,b=b,a%b
        return a
def solve(lis,x):
    if len(lis)==1:
        return abs(lis[0]-x)
    lis=[abs(c-x) for c in lis]
    ans=gcd(lis[0],lis[1])
    for x in lis[2:]:
        if x!=0:
            ans=gcd(ans,x)
    return ans 
n,x=map(int,input().split())
l=list(map(int,input().split()))
print(solve(l,x))
n,m=map(int,input().split())
a=list(int(input()) for i in range (m))
x=[0]*n
if a[0]==1:
    x[0]=0
else:
    x[0]=1

if m>=2:
	if a[0]!=2 and a[1]!=2:
		x[1]=x[0]+1

if m==1:
    if a[0]!=2:
	       x[1]=x[0]+1


for k in range(2,n):
    for z in range(m):
        x[a[z]-1]=0
    x[k]=x[k-2]+x[k-1]

print(x[n-1]%1000000007)

### FIXED CODE ###

import sys

n,m=map(int,input().split())
broken=set(map(int,sys.stdin))

if n==1:
  print (1)
  sys.exit()
if n==2:
  if 1 not in broken and 2 not in broken:
    print (2)
  elif 2 in broken:
    print(0)
  elif 1 in broken and 2 not in broken:
    print(1)
  sys.exit()

way = [0]*n
if 1 not in broken:
  way[0]=1
if 1 not in broken and 2 not in broken:
  way[1]=2
elif 1 in broken and 2 not in broken:
  way[1]=1
for i in range(2,n):
  if i+1 in broken:
    way[i]=0
    continue
  way[i]=(way[i-1]+way[i-2])%1000000007
print(way[n-1])
c=0
while True:
    n=input()
    if n==0:
        break
    c+=1
    x0, y0=map(int,raw_input().split())
    prev_x, prev_y=x0, y0
    tmp=0
    for i in range(n-1):
        x,y=map(int,raw_input().split())
        tmp+=prev_x*y - prev_y*x
        prev_x, prev_y=x, y
    tmp+=prev_x*y0 - prev_y*x0
    print c, 0.5*abs(tmp)
    skip=input()
    
   

### FIXED CODE ###

c=0
while True:
    n=input()
    if n==0:
        break
    c+=1
    x0, y0=map(int,raw_input().split())
    prev_x, prev_y=x0, y0
    tmp=0
    for i in range(n-1):
        x,y=map(int,raw_input().split())
        tmp+=prev_x*y - prev_y*x
        prev_x, prev_y=x, y
    tmp+=prev_x*y0 - prev_y*x0
    print c, 0.5*abs(tmp)
    skip=raw_input()
n, k = map(int, input().split())
l = list(map(int, input().split()))

l.sort(reversed=True)
print(sum(l[:k]))

### FIXED CODE ###

n, k = map(int, input().split())
l = list(map(int, input().split()))

l.sort(reverse=True)
print(sum(l[:k]))
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
p.sort()
p.reverse()
q = list(map(int, input().split()))
q.sort()
q.reverse()
r = list(map(int, input().split()))
r.sort()
r.reverse()

p = p[:x]
q = q[:y]


arr = p + q
arr.sort()
#print(arr)
#print(r)

for i in range(len(r)):
    if arr[i] <= r[i]:
        arr[i] = r[i]

print(sum(arr))



### FIXED CODE ###

x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
p.sort()
p.reverse()
q = list(map(int, input().split()))
q.sort()
q.reverse()
r = list(map(int, input().split()))
r.sort()
r.reverse()

p = p[:x]
q = q[:y]


arr = p + q
arr.sort()
#print(arr)
#print(r)

for i in range(min(len(arr),len(r))):
    if arr[i] <= r[i]:
        arr[i] = r[i]

print(sum(arr))
input_num = int(input())
input_color = input()
r_num = 0
b_num = 0
for color in input_color:
    if color == "R":
        r_num += 1
    if color == "B":
        b_num += 1
if r_num > b_num:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

input_number = input().rstrip().split(' ')
if (input_number[0] == input_number[1])and (input_number[1] == input_number[2]):
    print("Yes")
else:
    print("No")
s = input()
if s[0] == s[1] or s[1] == s[2]: print('No')
  else: print('Yes')

### FIXED CODE ###

S = input()
if S[0] == S[1] and S[1] == S[2]:
  print('No')
else: 
  print('Yes')
N =input()
A =list(map(input().split()))
l =[0*N]
for i in range A:
  A[i]+=1
print(l, sep ="\n")


### FIXED CODE ###

N =int(input())
A =list(map(int,input().split()))
l =[0]*N
for i in A:
  l[i-1]+=1
print(*l,sep="\n")
X,Y,A,B,C = list(map(int, input().split()))
red = list(map(int, input().split()))
green = list(map(int, input().split()))
white = list(map(int, input().split()))

red = sorted(red, reverse=True)[:X]
green = sorted(green, reverse=True)[:Y]
ans = sum(red) + sum(green)

white = sorted(white, reverse=True)
for i in range(len(white)):
    current = white[i]
    if red[-1] < green[-1] and current > red[-1] and len(red) != 0:
        ans += current - red[-1]
        red.pop(-1)
    elif red[-1] >= green[-1] and current > green[-1] and len(green) != 0:
        ans += current - green[-1]
        green.pop(-1)
    else:
        break

print(ans)

### FIXED CODE ###

X,Y,A,B,C = list(map(int, input().split()))
red = list(map(int, input().split()))
green = list(map(int, input().split()))
white = list(map(int, input().split()))

red = sorted(red, reverse=True)[:X]
green = sorted(green, reverse=True)[:Y]
ans = sum(red) + sum(green)

white = sorted(white, reverse=True)
for i in range(len(white)):
    current = white[i]
    if len(red) != 0 and len(green) != 0:
        if red[-1] < green[-1] and current > red[-1]:
            ans += current - red[-1]
            red.pop(-1)
        elif red[-1] >= green[-1] and current > green[-1]:
            ans += current - green[-1]
            green.pop(-1)
        else:
            break
    elif len(red) != 0:
        if current > red[-1]:
            ans += current - red[-1]
            red.pop(-1)
    elif len(green) != 0:
        if current > green[-1]:
            ans += current - green[-1]
    else:
        break

print(ans)
from collections import deque
A=deque(list(input()+"A"))
B=deque(list(input()+"B"))
C=deque(list(input()+"C"))
x=A.pop()
while(True):
  if x == "a":
    x = A.pop()
  elif x == "b":
    x = B.pop()
  elif x == "c:
    x = C.pop()
  elif x == "A":
    x = a
    break
  elif x == "B":
    x = b
    break
  elif x == "C":
    x = c
    break
print(x)
    
    

### FIXED CODE ###

from collections import deque
A=deque(list(input()+"A"))
B=deque(list(input()+"B"))
C=deque(list(input()+"C"))
x=A.popleft()
while(True):
  if x == "a":
    x = A.popleft()
  elif x == "b":
    x = B.popleft()
  elif x == "c":
    x = C.popleft()
  else:
    break
print(x)
N, M = map(int, input().split())
A = list(map(int, input().split()))
honsuu = [0,2,5,5,4,5,6,3,7,6]
aval = []
for a in A:
  aval += [honsuu[a]]
aval.sort()

dp = [0]+ [-float("INF")]*N 
# i 本のマッチで作れる最高の桁数
for i in range(1,N+1):
  for b in aval:
    if i < b:
      continue
    else:
      dp[i] = dp[i-b] + 1
      break
      
#作れる桁数はdp[-1]=dp[N] で, 最高桁にi が使えるかどうかの条件は, 
#a in A かつ dp[N- honsuu[a]] = dp[N] -1
A.sort(reverse=True)
ans = []
for i in range(dp[N],1,-1):
  for a in A:
    if N - honsuu[a] < 0:continue
    if dp[N - honsuu[a]] == dp[N] - 1:
      N -= honsuu[a]
      ans += [a]
      break
      
final = 0
for i in range(10):
  if i in A and honsuu[i] == N:
    final = i
ans += [final]
print(''.join(map(str, ans)))

### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
honsuu = [0,2,5,5,4,5,6,3,7,6]
aval = []
for a in A:
  aval += [honsuu[a]]
aval.sort()

dp = [0]+ [-float("INF")]*N 
# i 本のマッチで作れる最高の桁数
for i in range(1,N+1):
  for b in aval:
    if i < b:
      continue
    elif dp[i-b] >= 0:
      dp[i] = dp[i-b] + 1
      break
      
#作れる桁数はdp[-1]=dp[N] で, 最高桁にi が使えるかどうかの条件は, 
#a in A かつ dp[N- honsuu[a]] = dp[N] -1
A.sort(reverse=True)
ans = []
for i in range(dp[N],1,-1):
  for a in A:
    if N - honsuu[a] < 0:continue
    if dp[N - honsuu[a]] == dp[N] - 1:
      N -= honsuu[a]
      ans += [a]
      break
      
final = 0
for i in range(10):
  if i in A and honsuu[i] == N:
    final = i
ans += [final]
print(''.join(map(str, ans)))