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

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n,q = map(int,input().split())

g = [[] for _ in range(n)]
c = [0] * n
visited = [False] * n

#根に近い順に累積和
def dfs(i):
    visited[i] = True
    for j in g[i]:
        if visited[j] == True:
            continue
        c[j]+=c[i]
        dfs(j)

for i in range(n - 1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)

for i in range(q):
    p,x = map(int,input().split())
    c[p - 1]+=x

dfs(0)
print(*c)

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n,q = map(int,input().split())

g = [[] for _ in range(n)]
c = [0] * n
visited = [False] * n

#根に近い順に累積和
def dfs(i):
    visited[i] = True
    for j in g[i]:
        if visited[j] == True:
            continue
        c[j]+=c[i]
        dfs(j)

for i in range(n - 1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)

for i in range(q):
    p,x = map(int,input().split())
    c[p - 1]+=x

dfs(0)
print(*c)
### BUGGY CODE ###

import numpy as np
from collections import deque

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == '#': continue
        q = deque()
        q.append([i, j, 0])
        visited = np.zeros((h + 5, w + 5), dtype = bool)
        dirr = [[1, 0], [0, 1], [-1, 0], [0, -1]]
        mx = 0
        while len(q) != 0:
            x, y, z = q[0]
            for p in range(4):
                nx, ny = x + dirr[p][0], y + dirr[p][1]
                if 0 <= nx < h and 0 <= ny < w:
                    if not visited[nx][ny] and s[nx][ny] == '.':
                        q.append([nx, ny, z + 1])
                        visited[x][y] = True
                        ans = max(ans, z + 1)
            q.popleft()
print(ans)


### FIXED CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
from collections import deque

h, w = [int(item) for item in input().split()]
field = []
field.append("#" * (w + 2))
for i in range(h):
    field.append("#" + input().rstrip() + "#")
field.append("#" * (w + 2))


ans = 0
for i in range(1, h+1):
    for j in range(1, w+1):
        if field[i][j] == "#":
            continue
        dist = [[-1] * (w + 2) for _ in range(h + 2)]
        dist[i][j] = 0
        dq = deque()
        dq.append((i, j, 0))
        while dq:
            x, y, d = dq.popleft()
            if field[x+1][y] == "." and dist[x+1][y] == -1:
                dist[x+1][y] = d+1
                dq.append((x+1, y, d+1))
            if field[x][y+1] == "." and dist[x][y+1] == -1:
                dist[x][y+1] = d+1
                dq.append((x, y+1, d+1))
            if field[x-1][y] == "." and dist[x-1][y] == -1:
                dist[x-1][y] = d+1
                dq.append((x-1, y, d+1))
            if field[x][y-1] == "." and dist[x][y-1] == -1:
                dist[x][y-1] = d+1
                dq.append((x, y-1, d+1))
        for line in dist:
            ans = max(ans, max(line))
        
print(ans)
### BUGGY CODE ###

import sys

def main():
    N = int(input())

    Ss = []
    for x in range(N):
        Ss.append(sorted(input()))

    ans = 0
    for i in range(0, N-1):
        for j in range(i+1, N):
            if Ss[i] == Ss[j]:
                ans += 1

    print(ans)

main()

### FIXED CODE ###

import sys
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

def main():
    N = int(input())

    Ss = {}
    for x in range(N):
        tmp = ''.join(sorted(input()))

        if tmp in Ss:
            Ss[tmp] += 1

        else:
            Ss[tmp] = 1


    ans = 0
    for i in Ss.values():
        if i != 1:
            ans += combinations_count(i, 2)

    print(ans)

main()
### BUGGY CODE ###

def main():
    N = int(input())
    X = input()
    MOD = sum(map(lambda x: x == '1',X))
    if MOD == 0:
        print("1" + "\n1" * (N-1))
        return 0
    if MOD == 1 and X[-1] == '1':
        print("2\n" * (N-1) + "0")
        return 0
    if MOD == 1 and X[-1] == '0':
        ans = ''
        for i in range(N):
            if X[i] == '1':
                ans += '0'
            elif i != N-1:
                ans += '1'
            else:
                ans += '2'
            if i != N-1:
                ans += '\n'
        print(ans)
        return 0
    MODP1 = MOD + 1
    MODM1 = MOD - 1
    POWEROFTWOP1 = [0] * 200001
    POWEROFTWOM1 = [0] * 200001
    POWEROFTWOP1[0] = 1
    POWEROFTWOM1[0] = 1
    for i in range(1,N):
        POWEROFTWOP1[i] = (POWEROFTWOP1[i-1] * 2) % MODP1
        POWEROFTWOM1[i] = (POWEROFTWOM1[i-1] * 2) % MODM1
    xMODP1 = 0
    xMODM1 = 0
    for i in range(N):
        if X[i] == '1':
            xMODP1 += POWEROFTWOP1[N - i - 1]
            xMODM1 += POWEROFTWOM1[N - i - 1]
            xMODP1 %= MODP1
            xMODM1 %= MODM1
    ans = ""
    for i in range(N):
        if X[i] == '0':
            anscur = xMODP1
            anscur += POWEROFTWOP1[N - i - 1]
            anscur %= MODP1
        if X[i] == '1':
            anscur = xMODM1
            anscur -= POWEROFTWOM1[N - i - 1]
            anscur %= MODM1
        cnt = 1
        while anscur != 0:
            ans2 = bin(anscur)[2:]
            oneCount = sum(map(lambda x: x == '1',ans2))
            anscur %= oneCount
            cnt += 1
        ans += str(cnt)
        if i != N-1:
            ans += '\n'
    print(ans)
                    
main()


### FIXED CODE ###

def base10toN(num, base):
    converted_string, modstring = "", ""
    currentnum = num
    if not num:
        return '0'
    while currentnum:
        mod = currentnum % base
        currentnum = currentnum // base
        converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
    return converted_string
n=int(input())
pop=[0]*(2*10**5+1)
for i in range(1,2*10**5+1):
  temp=base10toN(i,2)
  ans=0
  for j in range(len(temp)):
    if temp[j]=='1':
      ans+=1
  r=i%ans
  pop[i]=pop[r]+1
x=input()
ones=0
ans1=[]
for i in range(n):
  if x[i]=='1':
    ones+=1
dividemore=0
divideless=0
for i in range(n):
  if x[i]=='1':
    dividemore=(pow(2,n-1-i,ones+1)+dividemore)%(ones+1)
    if ones>=2:
      divideless=(pow(2,n-1-i,ones-1)+divideless)%(ones-1)
for i in range(n):
  counter=0
  if x[i]=='0':
    temp=(dividemore+pow(2,n-1-i,ones+1))%(ones+1)
  else:
    if ones==1:
      ans1.append('0')
      continue
    else:
      temp=(divideless-pow(2,n-1-i,ones-1))%(ones-1)
  ans1.append(str(pop[temp]+1))
print(' '.join(ans1))
### BUGGY CODE ###

N, M = map(int, input().split())
H = list(map(int, input().split()))
J = [list(map(int, input().split())) for _ in range(M)]

res = []
for n in range(1, N+1):
    res.append(n)

for j in J:
    n1 = j[0]
    n2 = j[1]
    h1 = H[n1-1]
    h2 = H[n2-1]
    if h1 < h2:
        if n1 in res:
            res.remove(n1)
    elif h2 < h1:
        if n2 in res:
            res.remove(n2)
    else:
        if n1 in res:
            res.remove(n1)
        if n2 in res:
            res.remove(n2)

print(len(res))


### FIXED CODE ###

N, M = map(int, input().split())
H = list(map(int, input().split()))
J = [list(map(int, input().split())) for _ in range(M)]

res = {}
for n in range(1, N+1):
    res[n] = True

for j in J:
    n1 = j[0]
    n2 = j[1]
    h1 = H[n1-1]
    h2 = H[n2-1]
    if h1 < h2:
        if res[n1]:
            res[n1] = False
    elif h2 < h1:
        if res[n2]:
            res[n2] = False
    else:
        if res[n1]:
            res[n1] = False
        if res[n2]:
            res[n2] = False

l = 0
for v in res.values():
    if v:
        l+=1
print(l)
### BUGGY CODE ###

#! -*- coding: utf-8 -*-

n = int(raw_input())
x = n
print 0
status_0 = raw_input()


while 1:
    print x / 2


### FIXED CODE ###

#! -*- coding: utf-8 -*-

import sys

n = int(raw_input())
print 0
sys.stdout.flush()
status_0 = raw_input()
start = 0
end = n-1
check = end
i = 1
if status_0 == "Vacant":
    pass
else:
    while 1:
        print check
        sys.stdout.flush()
        status = raw_input()
        if status == "Vacant":
            break
        elif status == status_0:
            if check % 2 == 0:
                start = check
            else:
                end = check
        else:
            if check % 2 == 0:
                end = check
            else:
                start = check
        check = start+(end-start)/2

quit()
### BUGGY CODE ###

def main():
    from sys import stdin
    input = stdin.readline

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

    # dijkstra
    from heapq import heapify, heappush, heappop

    distance = [[-1] * n for _ in range(n)]
    for start in range(n):
        todo = [(0, start)]
        heapify(todo)

        while len(todo) > 0:
            cost, node = heappop(todo)
            if distance[start][node] == -1:
                distance[start][node] = cost

                for i in edge[node]:
                    if distance[start][i[0]] == -1:
                        heappush(todo, (cost + i[1], i[0]))

    # full search
    # 8! = 40320
    from itertools import permutations

    answer = float('inf')
    for i in permutations(l):
        ans = 0
        for j in range(r-1):
            ans += distance[i[j]][i[j+1]]
        if ans < answer:
            answer = ans

    print(answer)

main()

### FIXED CODE ###

def main():
    from sys import stdin
    input = stdin.readline

    n, m, r = map(int, input().split())
    l = list(map(int, input().split()))
    for i in range(r):
        l[i] -= 1
    d = [[float('inf')] * n for _ in range(n)]
    for _ in range(m):
        i, j, k = map(int, input().split())
        d[i-1][j-1] = k
        d[j-1][i-1] = k

    # Warshall-Floyd algorithm
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]

    # full search
    # 8! = 40320
    from itertools import permutations

    answer = float('inf')
    for i in permutations(l):
        ans = 0
        for j in range(r-1):
            ans += d[i[j]][i[j+1]]
        if ans < answer:
            answer = ans

    print(answer)

main()
### BUGGY CODE ###

Q = int(input())

order = list(map(int, input().split()))
A = [order[1]]
len_A = 1
B = order[2]

for i in range(Q-1):
        order = list(map(int, input().split()))
        if order[0] == 1:
                B += order[2]
                idx = 0
                while idx < len_A and A[idx] < order[1]:
                        idx += 1
                A = A[:idx] + [order[1]] + A[idx:]
                len_A += 1
        else:
                if len_A % 2 == 1:
                        min_x = A[(len_A - 1) // 2]
                        min_f = B
                        for i in range(len_A):
                                min_f += abs(min_x - A[i])
                        print('{} {}'.format(min_x, min_f))
                else:
                        min_x = A[-1 + len_A // 2]
                        min_f = B
                        for i in range(len_A):
                                min_f += abs(min_x - A[i])
                        print('{} {}'.format(min_x, min_f))


### FIXED CODE ###

import sys
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()

def main():
    Left = [10**10] # queri_num == 1の時はこっちが１多いことにする
    Right = [10**10]
    queri_num = 0 # mod 2
    const = 0
    min_val = 0

    """
    [a1, a2, ..., ai]の中央値を高速に求めないといけない所で分からず諦めた。。。

    発想は単純で、左側パートと右側パートを分けて管理しておいて、新しいクエリが来るごとに
    aを左に入れるか右に入れるか決めれば良い。
    ただし左右のパートをリストで管理するとaの挿入にO(N)かかってしまうので、ここでは
    ヒープによって管理する。なので挿入はO(logN)で可能。

    あと中央値における関数の最小値も求めないといけないが、これは基本的には単に|a - median|
    増えるだけなので、これもaの挿入時に逐次O(1)で決定可能。
    """

    Q = int(input())
    for _ in range(Q):
        queri = list(map(int, input().split()))
        if queri[0] == 2:
            x = -heappop(Left)
            print("{} {}".format(x, min_val + const))
            heappush(Left, -x)
        elif queri_num == 1:
            queri_num = 0
            const += queri[2]
            a = queri[1]
            mid = -heappop(Left)
            if a < mid:
                min_val += mid - a
                heappush(Left, -a)
                heappush(Right, mid)
            else:
                min_val += a - mid
                heappush(Left, -mid)
                heappush(Right, a)
        elif queri_num == 0:
            queri_num = 1
            const += queri[2]
            a = queri[1]
            left = -heappop(Left)
            right = heappop(Right)
            if a < left:
                min_val += left - a
                heappush(Left, -a)
                heappush(Left, -left)
                heappush(Right, right)
            elif a < right:
                # min_val is constant
                heappush(Left, -left)
                heappush(Left, -a)
                heappush(Right, right)
            else:
                min_val += a - right
                heappush(Left, -left)
                heappush(Left, -right)
                heappush(Right, a)


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

N, K = map(int, input().split())
h = list(map(int, input().split()))
 
def cost(a, b):
    return abs(h[a] - h[b])
 
c = [0] * N
for i in range(1, N):
    c[i] = min(cost(i, i-k)+c[i-k] for k in range(1, min(K, i)+1))
 
print(c[-1])
 


### FIXED CODE ###

N, K = map(int, input().split())
h = list(map(int, input().split()))
 
def cost(a, b):
    return abs(h[a] - h[b])
 
c = [0] * N
for i in range(1, N):
    c[i] = min(cost(i, i-k)+c[i-k] for k in range(1, min(K, i)+1))
 
print(c[-1])
### BUGGY CODE ###

def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  ab = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [INF]*(H+1)
  mi_a = ab[0]
  mi_b = ab[1]
  dp[0] = 0
  for i in range(1,H+1):
    if i<=mi_a:
      dp[i]=mi_b
    else:
      for a,b in AB:
        tmp = max(0,i-a)
        new = dp[tmp]+b
        if dp[i]>new:
          dp[i] = new

  print(dp[H])
  
main()

### FIXED CODE ###

H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
INF = float('inf')
dp = [INF]*(H+1)
dp[0] = 0
for i in range(1,H+1):
  for k in AB:
    a = k[0]
    b = k[1]
    tmp = max(0,i-a)
    dp[i] = min(dp[i],dp[tmp]+b)
    
print(dp[H])
### BUGGY CODE ###

import math
#=input()
N=int(input())
#=map(int,input().split())
#=list(map(int,input().split()))
#=[list(map(int,input().split())) for _ in range()]

for n in range(1,N+1):
    x = 1
    cnt = 0
    while True:
        y = 1
        while True:
            root = - 3 * (x ** 2) - 3 * (y ** 2) - 2 * x * y + 4 * n
            if root < 0:
                break
            zz = 1
            if math.sqrt(root) % 1 == 0:
                zz = (- y - x + int(math.sqrt(root)))
                #print(n,x,y,zz)
                if (zz % 2 == 0) and (zz > 0):
                    cnt += 1
                    #print("tsuuka!",n,x,y)
            if (zz < 0) or (root < 0):
                break
            y += 1
        if x ** 2 > n:
            break
        x += 1
    print(cnt)

### FIXED CODE ###

#import math
n = int(input())
l = [0 for i in range(n)]
for x in range(1, 101):
    for y in range(1, 101):
        for z in range(1, 101):
            s = x**2 + y**2 + z**2 + x*y + y*z + z*x;
            if s <= n:
                l[s-1] += 1
for i in range(n):
    print(l[i])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
from bisect import bisect_left

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

p = []
m = []
zero = 0
for i in range(N) :
  if A[i] > 0 :
    p.append(A[i])
  elif A[i] < 0 :
    m.append(-A[i])
  else :
    zero += 1

p.sort()
m.sort()

ng, ok = -(10 ** 18)-1, 10 ** 18+1
while ok - ng > 1 :
  mid = (ok + ng) // 2
  s = 0
    
  if mid < 0 :
    for i in range(len(m)) :
      x = (-mid+m[i]-1) // (m[i])
      s += len(p) - bisect_left(p, x)
    
  else :
    for i in range(len(p)) :
      x = mid // p[i]
      s += max(0, bisect_left(p, x + 1) - 1 - i)
    for i in range(len(m)) :
      x = mid // m[i]
      s += max(0, bisect_left(m, x + 1) - 1 - i)
    s += (len(p) + len(m)) * zero
    s += len(p) * len(m)
    s += zero * (zero - 1) // 2
  
  if s < K :
    ng = mid
  else :
    ok = mid
    
print(ok)

### FIXED CODE ###

import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

p = []
m = []
zero = 0
for i in range(N) :
  if A[i] > 0 :
    p.append(A[i])
  elif A[i] < 0 :
    m.append(-A[i])
  else :
    zero += 1

p.sort()
m.sort()

ng, ok = -(10 ** 18)-1, 10 ** 18+1
while ok - ng > 1 :
  mid = (ok + ng) // 2
  s = 0 
  if mid < 0 :
    j = 0
    for i in range(len(m) - 1, -1, -1) :
      while j < len(p) and m[i] * p[j] < -mid :
        j += 1
      s += len(p) - j
      
  else :
    j = len(p) - 1
    for i in range(len(p)) :
      while j >= 0 and p[i] * p[j] > mid :
        j -= 1
      s += max(0, j - i)
      
    j = len(m) - 1
    for i in range(len(m)) :
      while j >= 0 and m[i] * m[j] > mid :
        j -= 1
      s += max(0, j - i)
      
    s += (len(p) + len(m)) * zero + len(p) * len(m) + zero * (zero - 1) // 2

  if s < K :
    ng = mid
  else :
    ok = mid
    
print(ok)
### BUGGY CODE ###

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

def count(N,A):
    for i in range(N):
        print(A.count(i+1))

count(N,A)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
Ansbox = [0]*(N+1)

def count(N,A,Ansbox):

    for i in range(N- 1):
        Ansbox[A[i] - 1] += 1
        
    for i in range(N):
        print(Ansbox[i])
        
count(N,A,Ansbox)
### BUGGY CODE ###

import queue

INF = 10 ** 9

H, W, K = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())

sentinels = [-1] * (W+2)
MAP = [sentinels]
for _ in range(H):
    MAP.append( [-1] + list(map(lambda c: INF if c == '.' else -1, input())) + [-1] )
MAP.append(sentinels)

q = queue.Queue()
q.put((sx, sy))
MAP[sx][sy] = 0
while not q.empty():
    x, y = q.get()
    for d in range(1, K+1):
        if MAP[x+d][y] > MAP[x][y] + 1:
            MAP[x+d][y] = MAP[x][y] + 1
            q.put((x+d, y))
        elif MAP[x+d][y] < MAP[x][y] + 1:
            break
    for d in range(1, K+1):
        if MAP[x-d][y] > MAP[x][y] + 1:
            MAP[x-d][y] = MAP[x][y] + 1
            q.put((x-d, y))
        elif MAP[x-d][y] < MAP[x][y] + 1:
            break
    for d in range(1, K+1):
        if MAP[x][y+d] > MAP[x][y] + 1:
            MAP[x][y+d] = MAP[x][y] + 1
            q.put((x, y+d))
        elif MAP[x][y+d] < MAP[x][y] + 1:
            break
    for d in range(1, K+1):
        if MAP[x][y-d] > MAP[x][y] + 1:
            MAP[x][y-d] = MAP[x][y] + 1
            q.put((x, y-d))
        elif MAP[x][y-d] < MAP[x][y] + 1:
            break

if MAP[gx][gy] == INF:
    print(-1)
else:
    print(MAP[gx][gy])

### FIXED CODE ###

import queue

INF = 10 ** 9

H, W, K = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())

sentinels = [-1] * (W+2)
MAP = [sentinels]
for _ in range(H):
    MAP.append( [-1] + list(map(lambda c: INF if c == '.' else -1, input())) + [-1] )
MAP.append(sentinels)

q = queue.Queue()
q.put((sx, sy))
MAP[sx][sy] = 0
while not q.empty():
    x, y = q.get()
    for d in range(1, K+1):
        if MAP[x+d][y] > MAP[x][y] + 1:
            MAP[x+d][y] = MAP[x][y] + 1
            q.put((x+d, y))
        elif MAP[x+d][y] < MAP[x][y] + 1:
            break
    for d in range(1, K+1):
        if MAP[x-d][y] > MAP[x][y] + 1:
            MAP[x-d][y] = MAP[x][y] + 1
            q.put((x-d, y))
        elif MAP[x-d][y] < MAP[x][y] + 1:
            break
    for d in range(1, K+1):
        if MAP[x][y+d] > MAP[x][y] + 1:
            MAP[x][y+d] = MAP[x][y] + 1
            q.put((x, y+d))
        elif MAP[x][y+d] < MAP[x][y] + 1:
            break
    for d in range(1, K+1):
        if MAP[x][y-d] > MAP[x][y] + 1:
            MAP[x][y-d] = MAP[x][y] + 1
            q.put((x, y-d))
        elif MAP[x][y-d] < MAP[x][y] + 1:
            break

if MAP[gx][gy] == INF:
    print(-1)
else:
    print(MAP[gx][gy])
### BUGGY CODE ###

if __name__ == "__main__":
    n, k = map(int, input().split())
    arr = list(map(int,input().split()))

    tmp = 0
    tmp_arr = []
    sum_tmp = [sum(arr[i:i+k]) for i in range(n - (k - 1))]
    max_v = max(sum_tmp)
    max_i = sum_tmp.index(max_v)
    ans = 0
    for j in arr[max_i:max_i+k]:
        tmp_sum = 0
        for m in range(1, j+1):
            tmp_sum += m
        ans += tmp_sum / j
    print(ans)

### FIXED CODE ###

n, k = [int(x) for x in input().split()]
arr = list(map(int,input().split()))

m = sum(arr[0:k])
sump = m

for i in range(1, n-k+1):
    sump = sump - arr[i-1] + arr[i+k-1]
    if m < sump:
        m = sump

ans = (m + k) / 2

print(ans)
### BUGGY CODE ###

n,m=map(int,input().split())
bridge=[[] for i in range(m)]
iland=[[i+1] for i in range(n)]
for i in range(m):
    bridge[i]=list(map(int,input().split()))
ans=[0 for i in range(m+1)]
ans[0]=int((n-1)*n/2)
k=1
for i in reversed(bridge):
    ans[k]=ans[k-1]
    for j in range(len(iland)):
        if i[0] in iland[j]:
            temp=j
            break
    for j in range(len(iland)):
        if i[1] in iland[j]:
            if temp!=j:
                ans[k]=ans[k-1]-len(iland[j])*len(iland[temp])
                iland[j].extend(iland[temp])
                del iland[temp]
                break
    if ans[k]==0:
        ans[k:]=[0 for l in range(m-k)]
        break
    k+=1
for i in range(m):
    print(ans[m-1-i])

### FIXED CODE ###

n,m=map(int,input().split())
bridge=[[] for i in range(m)]
leader=[i for i in range(n+1)]
member=[1 for i in range(n+1)]
for i in range(m):
    bridge[i]=list(map(int,input().split()))
ans=[0 for i in range(m+1)]
ans[0]=int((n-1)*n/2)
k=1
for i in reversed(bridge):
    le0=leader[i[0]]
    le1=leader[i[1]]
    while le0!=leader[le0]:
        le0=leader[le0]
        leader[i[0]]=le0
        
    while le1!=leader[le1]:
        le1=leader[le1]
        leader[i[1]]=le1
        
    if le0!=le1:
        ans[k]=ans[k-1]-member[le0]*member[le1]
        if member[le0]>=member[le1]:
            member[le0]+=member[le1]
            leader[le1]=le0
        else:
            member[le1]+=member[le0]
            leader[le0]=le1
    else:
        ans[k]=ans[k-1]
    if ans[k]==0:
        ans[k:]=[0 for l in range(m-k)]
        break
    k+=1
for i in range(m):
    print(ans[m-1-i])
### BUGGY CODE ###

def main():
    S = input()
    Q = int(input())
    S_reverse = S[::-1]

    for i in range(Q):
        q = input()
        if len(q.split(' ')) > 1:
            T, F, C = q.split(' ')
            if F == '1':
                S = C + S
                S_reverse = S_reverse + C 
            elif F == '2':
                S = S + C
                S_reverse = C + S_reverse
        elif q == '1':
            temp = S_reverse
            S_reverse = S
            S = temp

    print(S)

if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import deque

def main():
    S = input()
    Q = int(input())

    t = deque(S)
    reverse = False
    for _ in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        elif query[0] == '2':
            _, F, C = query.split()
            if F == '1':
                if reverse:
                    t.append(C)
                else:
                    t.appendleft(C)
            elif F == '2':
                if reverse:
                    t.appendleft(C)
                else:
                    t.append(C)
    
    result = ''.join(t)
    if reverse:
        result = result[::-1]

    print(result)

if __name__ == '__main__':
    main()