Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
#mod = 10**9 + 7
mod = 998244353

N, M, K = rl()

def combmod(n, k):
	p, q = 1, 1
	for i in range(1, k+1):
		p = p * (n-i+1) % mod
		q = q * i % mod
	return p * pow(q, mod-2, mod) % mod

ans = 0
for k in range(K+1):
	ans = (ans + M * pow(M-1, N-1-k, mod) * combmod(N-1, k)) % mod
print(ans)


### FIXED CODE ###

import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
#mod = 10**9 + 7
mod = 998244353

N, M, K = rl()

class CombMod:
	def __init__(self, N):
		self.fact = [1]
		self.rfact = [1]
		for i in range(1, N+1):
			self.fact.append(self.fact[i-1]*i % mod)
			self.rfact.append(pow(self.fact[i], mod-2, mod))

	def comb(self, n, k):
		return self.fact[n]*self.rfact[k]*self.rfact[n-k] % mod

cm = CombMod(N-1)

ans = 0
for k in range(K+1):
	ans = (ans + M * pow(M-1, N-1-k, mod) * cm.comb(N-1, k)) % mod
print(ans)
### BUGGY CODE ###

N = int(input())
A_list = []
for i in range(1,N+1):
    A = int(input())
    A_list.append([i,A])

pop_que =[]

for i in range(N):
    pop_que =A_list.pop(0)
    pop_que.append(max(A_list,key=lambda x:x[1]))
    A_list.append(pop_que)

for i in range(N):
    print(A_list[i][2][1])


### FIXED CODE ###

N = int(input())
A_list = []
for i in range(1,N+1):
    A = int(input())
    A_list.append(A)
A_list_sort = sorted(A_list)

max_a = A_list_sort[-1]
max_b = A_list_sort[-2]


for i in range(N):
    if A_list[i] == max_a:
        print(max_b)
    else:
        print(max_a)
### BUGGY CODE ###

from sys import stdin, stdout

def sol2():
	H, N = (int(x) for x in stdin.readline().split())
	dp = [float('inf') for _ in range(H+1)]
	dp[0] = 0
	for line in stdin:
		dmg, cost = [int(x) for x in line.split()]
		for i in range(len(dp)):
			new = min(H, i + dmg)
			dp[new] = min(dp[new], dp[i] + cost)
	stdout.write(str(dp[-1]))

if __name__ == "__main__":
	sol2()

### FIXED CODE ###

from sys import stdin, stdout	

def sol2():
	H, N = (int(x) for x in stdin.readline().split())
	dp = [float('inf') for _ in range(H+1)]
	dp[0] = 0
	for line in stdin:
		dmg, cost = [int(x) for x in line.split()]
		for i in range(len(dp)):
			new = min(H, i + dmg)
			dp[new] = min(dp[new], dp[i] + cost)
	stdout.write(str(dp[-1]))

if __name__ == "__main__":
	sol2()
### BUGGY CODE ###

from itertools import combinations
MOD = 10**9 + 7
n=int(input())
a=list(map(int,input().split()))
sum=0
for x,y in combinations(a,2):
   sum+=x*y%MOD
   sum=sum%MOD
print(sum)

### FIXED CODE ###

mod = 10**9 + 7
n=int(input())
s=list(map(int,input().split()))
s2=sum(map(lambda x: x ** 2,s))
ans=(((sum(s))**2)-(s2))//2%mod
print(ans)
### BUGGY CODE ###

from sys import stdin, setrecursionlimit
from math import factorial

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


def main():
    N, M, K = map(int, input().split())
    MOD = 998244353
    fac = [0] * (N + 10)
    finv = [0] * (N + 10)
    inv = [0] * (N + 10)
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, N + 2):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    def COM(n, k):
        if n < k:
            return 0
        if n < 0 or k < 0:
            return 0
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

    ans = 0
    for k in range(K + 1):
        ans += COM(N - 1, k) * ((M % MOD) * ((M - 1) % MOD) ** (N - k - 1)) % MOD

    print(ans % MOD)

    return


if __name__ == '__main__':
    main()


### FIXED CODE ###

from sys import stdin, setrecursionlimit
from math import factorial

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


def main():
    N, M, K = map(int, input().split())
    MOD = 998244353
    fac = [0] * (N + 10)
    finv = [0] * (N + 10)
    inv = [0] * (N + 10)
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, N + 2):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    def COM(n, k):
        if n < k:
            return 0
        if n < 0 or k < 0:
            return 0
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

    ans = 0
    for k in range(K + 1):
        ans += COM(N - 1, k) * ((M % MOD) * pow(M - 1, (N - k - 1), MOD)) % MOD

    print(ans % MOD)

    return


if __name__ == '__main__':
    main()
### BUGGY CODE ###

N,X,Y=list(map(int,input().split()))
G=[]
for i in range(1,N):
    G.append([i,i+1])
G.append([X,Y])
graph=[[] for i in range(N+1)]
for i in range(N):
    u,*v=G[i]
    v.sort()
    for j in v:
        graph[u].append(j)
        graph[j].append(u)
counter=[[] for i in range(N+1)]
for j in range(1,N+1):
    stack=[j]
    visited=[-1 for i in range(N+1)]
    visited[j]=0
    while stack:
        n=stack.pop(0)
        for i in graph[n]:
            n_n=i
            if visited[n_n]<0:
                visited[n_n]=visited[n]+1
                stack.append(n_n)
    for i,value in enumerate(visited):
        for k in range(1,N):
            if value==k:
                counter[k].append([j,i])
for i,value in enumerate(counter):
    if not (i==0 or i==N):
        print(int(len(value)/2))

### FIXED CODE ###

N,X,Y=list(map(int,input().split()))
A=[[] for i in range(N+1)]
for i in range(1,N):
    for j in range(i,N+1):
        A[int(min(j-i,abs(X-i)+abs(Y-j)+1))].append(1)
        
for i in range(N):
    if i!=0:
        print(sum(A[i]))
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse=True)

K_list = [0] * N
K_sum = 0

A_sum = 0

for i in range(N):
    A_sum += A[i]

if A_sum <= K:
    print(0)

else:
    left = 0
    right = A[N-1] * F[0]

    while right - left > 1:
        K_sum = 0
        mid = (right + left) // 2
        for i in range(N):
            if A[i] * F[i] > mid:
                K_list[i] = A[i] - mid // F[i]
                K_sum += K_list[i]

        if K_sum > K:
            left = mid
        else:
            right = mid
    print(right)


### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse=True)

K_list = [0] * N
K_sum = 0

A_sum = 0

for i in range(N):
    A_sum += A[i]

if A_sum <= K:
    print(0)

else:
    left = 0
    right = A[N-1] * F[0]

    while right - left > 1:
        K_sum = 0
        mid = (right + left) // 2
        for i in range(N):
            if A[i] * F[i] > mid:
                K_list[i] = A[i] - mid // F[i]
                K_sum += K_list[i]

        if K_sum > K:
            left = mid
        else:
            right = mid
    print(right)
### BUGGY CODE ###

n, k, q = map(int, input().split())
a = [int(input()) for _ in range(q)]
for i in range(1, n+1):
    if k - q + a.count(i) > 0:
        print('Yes')
    else:
        print('No')


### FIXED CODE ###

from collections import Counter
n, k, q = map(int, input().split())
a = [int(input()) for _ in range(q)]
ans = ['No'] * n
c = Counter(a)
for i in range(1, n+1):
    if c[i] > q - k:
        ans[i-1] = 'Yes'
for k in ans:
    print(k)
### BUGGY CODE ###

import heapq

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [[c for c in input()] for _ in range(H)]


h = []
heapq.heappush(h, (0, Ch, Cw))

while len(h) != 0:
    d, nh, nw = heapq.heappop(h)
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] == ".":
                    S[newh][neww] = newd
                    heapq.heappush(h, (newd, newh, neww))
                elif S[newh][neww] == "#":
                    continue
                else:
                    if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        heapq.heappush(h, (newd, newh, neww))
print(-1)


### FIXED CODE ###

from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)
### BUGGY CODE ###

from collections import Counter
from functools import reduce

def prime_factorization(n):
    '''
    Trial division.
    Input: 
        n : a positive integer
    Output:
        a dictionary of #(each prime factors) of n,
    '''
    result = Counter()
    if n < 2:
        return result
    while n % 2 == 0:
        result[2] += 1
        n //= 2
    p = 3
    while p*p <= n:
        while n % p == 0:
            n //= p
            result[p] += 1
        p += 2
    if n > 1:
        result[n] += 1
    return result

MOD = 10**9 + 7

N = int(input())
*A, = map(int, input().split())
ans = 0
pf_A = [prime_factorization(a) for a in A]
lcm = reduce(lambda x,y: x | y, pf_A)
for fact in pf_A:
    temp = lcm - fact
    t = 1
    if temp.items():
        for x, y in temp.items():
            t *= pow(x, y, MOD)
        ans += t
        ans %= MOD
    else:
        ans += 1
        ans %= MOD
print(ans)

### FIXED CODE ###

from collections import Counter

def extended_euclid(a, b):
    x1, y1, m = 1, 0, a
    x2, y2, n = 0, 1, b
    while m % n != 0:
        q, r = divmod(m, n)
        x1, y1, m, x2, y2, n = x2, y2, n, x1 - q * x2, y1 - q * y2, r
    return (x2, y2, n)

def modular_inverse(a, mod):
    x, _, g = extended_euclid(a, mod)
    if g != 1:
        return None # Modular inverse of a does not exist
    else:
        return x % mod

def prime_factorization(n):
    '''
    Trial division.
    Input: 
        n : a positive integer
    Output:
        a dictionary of #(each prime factors) of n,
    '''
    result = []
    app = result.append
    if n < 2:
        return Counter()
    while n % 2 == 0:
        app(2)
        n //= 2
    p = 3
    while p*p <= n:
        while n % p == 0:
            app(p)
            n //= p
        p += 2
    if n > 1:
        app(n)
    return Counter(result)

MOD = 10**9 + 7

N = int(input())
*A, = map(int, input().split())
ans = 0
lcm = {}
for a in A:
    for x, y in prime_factorization(a).items():
        if x not in lcm.keys():
            lcm[x] = y
        else:
            lcm[x] = max(lcm[x], y)
L = 1
for x, y in lcm.items():
    L *= pow(x, y, MOD)
    L %= MOD
for a in A:
    ans += L * modular_inverse(a, MOD)
    ans %= MOD
print(ans)
### BUGGY CODE ###

from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    def dfs(level=0, u=0):
        if len(to[(level, u)]) == 1:
            cnt_tree[l - level] += 1
        for kl, ku in to[(level, u)]:
            dfs(kl, ku)

    to = defaultdict(set)
    n, l = map(int, input().split())
    ss = [input()[:-1] for _ in range(n)]

    # 各sをパスと見て、辺を張っていく
    for s in ss:
        now_l = 0
        now_u = 0
        for c in s:
            nxt_l = now_l + 1
            nxt_u = (now_u << 1) + int(c)
            to[(now_l, now_u)].add((nxt_l, nxt_u))
            now_l, now_u = nxt_l, nxt_u
    # print(to)

    # パスで分離された木のレベルと個数を数える
    cnt_tree = defaultdict(int)
    dfs()
    #print(cnt_tree)

    # Grundy数をlv & -lvで求め、xorをとる
    sum_xor = 0
    for lv, cnt in cnt_tree.items():
        if cnt % 2 == 0: continue
        sum_xor ^= lv & -lv

    if sum_xor == 0:
        print("Bob")
    else:
        print("Alice")

main()


### FIXED CODE ###

from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n, l = map(int, input().split())
    ss = [input()[:-1] for _ in range(n)] + ["$"]
    ss.sort()
    # print(ss)

    # 隣同士のsを先頭から比べて、異なるところでそのlevelの木を消して、それ以降の木を追加する
    cnt_tree = defaultdict(int)
    cnt_tree[l] = 2
    for s0, s1 in zip(ss, ss[1:]):
        for depth, (c0, c1) in enumerate(zip(s0, s1)):
            if c0 == c1: continue
            cnt_tree[l - depth] -= 1
            for lv in range(l - depth - 1, l - len(s1), -1):
                cnt_tree[lv] += 1
            # print(cnt_tree)
            break

    # Grundy数をlv & -lvで求め、xorをとる
    sum_xor = 0
    for lv, cnt in cnt_tree.items():
        if cnt % 2 == 0: continue
        sum_xor ^= lv & -lv

    if sum_xor:
        print("Alice")
    else:
        print("Bob")

main()
### BUGGY CODE ###

from collections import deque
n, x, y = map(int, input().split())

def push(v, d):
    if Dist[v] == -1:   #距離未更新
        Dist[v] = d
        Queue.append(v)
    else:   #距離が更新済み
        pass
    return 0
    
Ans = [0] * n
for i in range(n):
    Queue = deque([i])
    Dist = [-1] * n
    Dist[i] = 0 
    while len(Queue) != 0:
        v = Queue[0]
        d = Dist[v]
        #辺を通して次の頂点へ

        #-方向
        if v-1 >= 0:
            push(v-1, d+1)
        else:
            pass

        #+方向
        if v+1 < n:
            push(v+1, d+1)
        else:
            pass

        #XとYの間
        if v == x-1 :
            push(y-1, d+1)
        elif v == y-1:
            push(x-1, d+1)
        else:
            pass
        Queue.popleft()
    for i in Dist:
        Ans[i] += 1

for i in range(len(Ans)):
    Ans[i] *= 0.5

for i in range(1, n):
    print(int(Ans[i]))




### FIXED CODE ###

from collections import deque
n, x, y = map(int, input().split())

def push(v, d):
    if Dist[v] == -1:   #距離未更新
        Dist[v] = d
        Queue.append(v)
    else:   #距離が更新済み
        pass
    return 0
    
Ans = [0] * n
for i in range(n):
    Queue = deque([i])
    Dist = [-1] * n
    Dist[i] = 0 
    while len(Queue) != 0:
        v = Queue[0]
        d = Dist[v]
        #辺を通して次の頂点へ

        #-方向
        if v-1 >= 0:
            push(v-1, d+1)
        else:
            pass

        #+方向
        if v+1 < n:
            push(v+1, d+1)
        else:
            pass

        #XとYの間
        if v == x-1 :
            push(y-1, d+1)
        elif v == y-1:
            push(x-1, d+1)
        else:
            pass
        Queue.popleft()
    for i in Dist:
        Ans[i] += 1

for i in range(len(Ans)):
    Ans[i] *= 0.5

for i in range(1, n):
    print(int(Ans[i]))
### BUGGY CODE ###

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

dp = [float('inf')] * n

dp[0] = 0
for i in range(n):
  for j in range(k+1):
    if i-j>=0:
      dp[i] = min(dp[i], dp[i-j] + abs(h[i]-h[i-j]))
    
    
print(dp[n-1])

### FIXED CODE ###

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

dp = [float('inf')] * n

dp[0] = 0
for i in range(1, n):
  dp[i] = min(dp[j]+abs(h[i]-h[j]) for j in range(max(0, i-k), i))

    
print(dp[n-1])
### BUGGY CODE ###

n,k = (int(x) for x in input().split())
if k == 1:
  print(0)
elif k == 2:
  if n%2==0:
    print(0)
  else:
    print(1)
elif k == 3:
  if n%3==0:
    print(0)
  else:
    print(1)
else:
  while abs(n-k) < n:
    n = abs(n-k)
  print(n)

### FIXED CODE ###

n,k = (int(x) for x in input().split())
if n%k < k-(n%k):
  print(n%k)
else:
  print(k-(n%k))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

class BIT:
    def __init__(self, size):
        self.bit = [0] * (size + 1)
        self.size = size

    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.bit[i] += x
            i += i & -i

class RangeBIT:
    def __init__(self, size):
        self.bit0 = BIT(size)
        self.bit1 = BIT(size)

    def sum(self, i):
        return self.bit0.sum(i) * i + self.bit1.sum(i)

    # end points are inclusive
    def add(self, l, r, x):
        self.bit0.add(l, x)
        self.bit0.add(r+1, -x)
        self.bit1.add(l, -x*(l-1))
        self.bit1.add(r+1, x*r)


N, K = map(int, input().split())
A = list(map(int, input().split()))
while K > 0 and not all(a == N for a in A):
    rb = RangeBIT(N)
    for i, a in enumerate(A):
        rb.add(max(0, i - a), min(N - 1, i + a), 1)
    for i in range(N):
        A[i] = rb.sum(i) - (rb.sum(i-1) if i > 0 else 0)
    K -= 1
print(*A)


### FIXED CODE ###

from itertools import accumulate
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(int, input().split()))
while K > 0 and not all(a == N for a in A):
    imos = [0] * (N + 1)
    for i, a in enumerate(A):
        imos[max(0, i - a)] += 1
        imos[min(N, i + a + 1)] -= 1
    A = list(accumulate(imos))[:-1]
    K -= 1
print(*A)
### BUGGY CODE ###

import functools
import operator

N = int(input())
A = list(map(int, input().split()))
li = [2,5,7,9]
num = functools.reduce(operator.mul, A)

if num>10**18:
    print(-1)
else:
    print(num)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
num = 1
for i in range(0,N):
    if A[i]==0:
      	num=0
      	break
for i in range(0,N):
    if num>10**18:
        break
    else:
        num *=A[i]
if num>10**18:
    print(-1)
else:
    print(num)
### BUGGY CODE ###

N,M=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
xy=[list(map(lambda x:int(x)-1,input().split())) for _ in [0]*M]
z=[[i,1] for i in range(N)]

def root_find(a):
    while(a!=z[a][0]):
        a=z[a][0]
    return a

def union(a,b):
    ra=root_find(a[0])
    rb=root_find(b[0])
    if z[ra][1]<=z[rb][1]:
        z[ra][0]=rb
        z[rb][1]=max(z[rb][1],z[ra][1]+1)
    else:
        z[rb][0]=ra
        z[ra][1]=max(z[ra][1],z[rb][1]+1)
    
for i in range(M):
    union(z[xy[i][0]],z[xy[i][1]])

for i in range(N):
    z[i][0]=root_find(z[i][0])

dic=dict()

for i in range(N):
    if z[i][0] not in dic.keys():
        dic[z[i][0]]=[i]
    else:
        dic[z[i][0]].append(i)
        
list1=list(dic.values())
list2=[[] for _ in [0]*len(list1)]

for i in range(len(list1)):
    for a in list1[i]:
        list2[i].append(p[a])

cnt=0
for i in range(len(list1)):
    for a in list1[i]:
        if a in list2[i]:
            cnt+=1
print(cnt)

### FIXED CODE ###

N,M=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
xy=[list(map(lambda x:int(x)-1,input().split())) for _ in [0]*M]
z=[[i,1] for i in range(N)]

def root_find(a):
    while(a!=z[a][0]):
        a=z[a][0]
    return a

def union(a,b):
    ra=root_find(a[0])
    rb=root_find(b[0])
    if z[ra][1]<=z[rb][1]:
        z[ra][0]=rb
        z[rb][1]=max(z[rb][1],z[ra][1]+1)
    else:
        z[rb][0]=ra
        z[ra][1]=max(z[ra][1],z[rb][1]+1)
    
for i in range(M):
    union(z[xy[i][0]],z[xy[i][1]])

for i in range(N):
    z[i][0]=root_find(z[i][0])

dic=dict()

for i in range(N):
    zz=z[i][0]
    if zz not in dic.keys():
        dic[zz]=[i]
    else:
        dic[zz].append(i)

list1=list(dic.values())
list2=[[p[a] for a in list1[i]] for i in range(len(list1))]

cnt=sum(len(set(list1[i])&set(list2[i])) for i in range(len(list1)))
print(cnt)
### BUGGY CODE ###

n = int(input())
arr = [int(x) for x in input().split(' ')]

ans = 1
for x in arr:
    ans *= x
    
if ans > int(1e18):
    print(-1)
else:
    print(ans)

### FIXED CODE ###

n = int(input())
arr = []

z=False
for x in input().split(' '):
    if x == '0':
        z = True
    arr.append(int(x))
if z == True:
    print(0)

else:
    ans = 1
    for x in arr:
        ans *= x
        if ans > int(1e18):
            print(-1)
            exit(0)
    
    print(ans)
### BUGGY CODE ###

class Deque:
    #??????????????????
    class Node:
        def __init__(self, x, y = None, z = None):
            self.key = x
            self.next = y
            self.prev = z
    def __init__(self):
        head = Deque.Node(None)
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head

    #insert x:??£??????????????????????????????x?????????????´???????????¶????
    def insert(self,x):
        h = self.head
        q = h.next
        p = Deque.Node(x,q,h)
        h.next = p
        q.prev = p
        self.size += 1

    #deleteFirst:??£????????????????????????????´???????????????????
    def deleteFirst(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1

    #deleteLast:??£???????????????????°????????´???????????????????
    def deleteLast(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1

    #???????????????????????¢?´¢
    def listSearch(self, x):
        h = self.head
        cur = h.next
        while cur != None and cur.key != x:
            cur = cur.next
        return cur

    #delete:x???????????????????´??????????????????°?????¢?´¢???????§?
    def delete(self, x):
        if self.size == 0: raise IndexError
        if x == None: return
        h = self.listSearch(x)
        q = h.next
        p = h.prev
        p.next = q
        q. prev = p
        self.size -=1

    #??????
    def printList(self):
        h = self.head
        cur = h.next
        isf = 0
        while 1:
            if cur == h: break
            if isf > 0: print(" ", end="")
            print(cur.key, end="")
            isf += 1
            cur = cur.next
        print("")

N = int(input())
CL=[]
d = Deque()
for i in range(N):
    CL.append(input().split())
for i in CL:
    if i[0] == "insert":
        x = i[1]
        d.insert(x)
    if i[0] == "delete":
        x = i[1]
        d.delete(x)
    if i[0] == "deleteFirst":
        d.deleteFirst()
    if i[0] =="deleteLast":
        d.deleteLast()
d.printList()

### FIXED CODE ###

from collections import deque
n = int(input())
L = deque()

for i in range(n):
    cmdline = input().split()

    if cmdline[0] == "insert":
        L.appendleft(cmdline[1])
    elif cmdline[0] == "delete":
        try:
            L.remove(cmdline[1])
        except ValueError: pass
    elif cmdline[0] == "deleteFirst":
        L.popleft()
    elif cmdline[0] == "deleteLast":
        L.pop()

print(" ".join(L))