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

a,b,n = map(int,input().split())
def floor(x):
    return (a*x)//b - a*(x//b)
ans = 0
p = b-1 if b < n else 1 
for i in range(p,n+1):
    ans = max(ans,floor(i))
    #print(floor(i))
print(ans)

### FIXED CODE ###

a,b,n = map(int,input().split())
def floor(x):
    return (a*x)//b - a*(x//b)
ans = 0
if b <= n: 
    '''
    for i in range(b-1,n+1):
        ans = max(ans,floor(i))
        #print(floor(i))
    print(ans)
    '''
    print(floor(b-1))
else:
    print(floor(n))
### BUGGY CODE ###

n,E = map(int, input().split())
M = [[] for i in range(n)]
for i in range(E):
 s,t,w = map(int, input().split())
 M[s].append([t,w])
 M[t].append([s,w])

def prim():
 color = [0] * n
 d = [float("inf")] * n
 d[0] = 0
 
 while True:
  minv = float("inf")
  u = -1
  for i in range(n):
   if minv > d[i] and color[i] != 2:
    u = i
    minv = d[i]
  if u == -1: break
  color[u] = 2
  for v1, v2 in M[u]:
   if color[v1] != 2:
    if d[v1] > v2:
     d[v1] = v2
 return sum(d)
 
print(prim())

### FIXED CODE ###

import heapq as pq
n,E = map(int, input().split())
M = [[] for i in range(n)]
for i in range(E):
 s,t,w = map(int, input().split())
 M[s].append([t,w])
 M[t].append([s,w])

def prim():
 color = [0] * n
 d = [float("inf")] * n
 d[0] = 0
 H = []
 pq.heappush(H, [0, 0])
 while H:
  u2,u1 = pq.heappop(H)
  color[u1] = 1
  if d[u1] < u2: continue
  for v1, v2 in M[u1]:
   if color[v1] == 1: continue
   if d[v1] > v2:
    d[v1] = v2
    pq.heappush(H, [d[v1], v1])
 return sum(d)
 
print(prim())
### BUGGY CODE ###

H, W, M = map(int, input().split())
 
h = [0] * H
w = [0] * W
Mem = []
for _ in range(M):
  i, j = map(int, input().split())
  Mem += [(i, j)]
  h[i-1] += 1
  w[j-1] += 1

maxh = max(h)
maxw = max(w)
 
listh = [i for i, v in enumerate(h, 1) if v == maxh]
listw = [j for j, v in enumerate(w, 1) if v == maxw]

cnt = len(listh) * len(listw)

for i, j in Mem:
  if i in listh and j in listw:
    cnt -= 1
 
if cnt:
  ans = maxh + maxw
else:
  ans = maxh + maxw - 1

print(ans)

### FIXED CODE ###

h,w,m=map(int,input().split())
item=[list(map(int,input().split())) for i in range(m)]
row=[0]*h
col=[0]*w
for i in range(m):
    x,y=item[i]
    row[x-1]+=1
    col[y-1]+=1
mr,mc=max(row),max(col)
xr=set([i for i in range(h) if row[i]==mr])
xc=set([i for i in range(w) if col[i]==mc])
check=len(xr)*len(xc)
for i in range(m):
    r,c=item[i]
    if r-1 in xr and c-1 in xc:
        check-=1
print(mr+mc if check>0 else mr+mc-1)
### BUGGY CODE ###

from collections import Counter

N = int(input())
s= input()
s = s.replace("E", "0")
s = s.replace("W", "1")

D = N
for i in range(N):
    temp = 0
    for j in range (N):
        if (i < j) and (s[j] == "0" ):
            temp += 1
        if (i > j) and (s[j] == "1" ):
            temp += 1 
    if temp < D:
        D = temp
print(D)

### FIXED CODE ###

from collections import Counter
N = int(input())
s= input()

D = Counter(s[1:])["E"]
nums = [D]
for i in range (1,N):
    temp = nums[-1]
    if s[i-1] == "W":
        temp += 1
    if s[i] == "E":
        temp -= 1
    nums.append(temp)

print(min(nums))
### BUGGY CODE ###

from itertools import permutations

def hoge():
  N, M, R = map(int, input().split())
  r = map(int, input().split())
  r = [x - 1 for x in r]
  inf = float('inf')
  d = [[inf] * N for i in range(N)]

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

  for i in range(M):
      a, b, c = map(int, input().split())
      d[a-1][b-1] = c
      d[b-1][a-1] = c


  for k in range(N):
      for i in range(N):
          for j in range(N):
              t = d[i][k] + d[k][j]
              d[i][j] = min(d[i][j], t)


  ans = inf
  for i in permutations(r):
      tmphoge = len(i) - 1
      temp = 0
      for j in range(tmphoge):
          temp += d[i[j]][i[j+1]]
          if temp > ans:
              break
      ans = min(ans, temp)

  print(ans)
hoge()


### FIXED CODE ###

from itertools import permutations

def input_list():
    return list(map(int, input().split()))

def main():
    n, m, r = input_list()
    rlist = input_list()
    rlist = [r-1 for r in rlist]
    d = [[float('inf')]*n for i in range(n)]
    for i in range(n):
        d[i][i] = 0
    
    for i in range(m):
        a, b, c = input_list()
        d[a-1][b-1] = c
        d[b-1][a-1] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                distance = d[i][k]+d[k][j]
                if d[i][j] > distance:
                    d[i][j] = distance

    ans = float('inf')
    for p in permutations(rlist):
        distance = 0
        for i in range(r-1):
            distance += d[p[i]][p[i+1]]

        if ans > distance:
            ans = distance

    print(ans)

main()
### BUGGY CODE ###

import copy
def op(children, S):
    ret = [0]*len(S)
    for i in range(len(S)):
        if S[i] == 'R':
            ret[i+1] += children[i]
        else:
            ret[i-1] += children[i]
    return ret

def solve():
    S = input()
    children = [1] * len(S)
    old_2 = [1] * len(S)
    old_1 = [1] * len(S)
    for i in range(1,10**5+1):
        children = op(children, S) 
        # print(children)
        if old_2 == children and i%2 == 0:
            break
        old_2 = copy.copy(old_1)
        old_1 = copy.copy(children)
    print(' '.join(map(str, children)))

solve()

### FIXED CODE ###

def solve():
    S = input()
    result = [0] * len(S)
    children_position = []
    even_odd_sum = []
    even_sum = 0
    odd_sum = 0
    R_i = 0
    L_i = -2
    
    for idx, s in enumerate(S):
        
        if s == 'R':
            R_i = idx
            if L_i+1 == R_i:
                even_odd_sum.append((even_sum, odd_sum))
                even_sum = 0
                odd_sum = 0
        elif s == 'L':
            L_i = idx
            if R_i+1 == idx:
                children_position.append((R_i, L_i))
                
        even_sum += idx % 2 == 0
        odd_sum += idx % 2 == 1
                
    even_odd_sum.append((even_sum, odd_sum))
    # print(children_position, even_odd_sum)
    for idx in range(len(children_position)):
        t = children_position[idx]
        even_odd_R = t[0] % 2
        even_odd_L = t[1] % 2
        
        result[t[0]] = even_odd_sum[idx][0] if even_odd_R == 0 else even_odd_sum[idx][1]
        result[t[1]] = even_odd_sum[idx][0] if even_odd_L == 0 else even_odd_sum[idx][1]
        
    
    print(' '.join(map(str, result)))
solve()
### BUGGY CODE ###

def main():
    N, Q = map(int, input().split())
    query = [None] * (2 * N + Q)
    for i in range(N):
        S, T, X = map(int, input().split())
        query[i*2] = (S-X, 1, X)
        query[i*2+1] = (T-X, 0, X)
    for i in range(Q):
        D = int(input())
        query[2*N+i] = (D, 2, 0)
    query.sort()
    mn = float("inf")
    construction = set()
    for t, q, x in query:
        if q == 2:
            if mn == float("inf"):
                print(-1)
            else:
                print(mn)
        elif q == 1:
            construction.add(x)
            if x < mn:
                mn = x
        elif q == 0:
            construction.remove(x)
            if mn == x:
                if len(construction):
                    mn = min(construction)
                else:
                    mn = float("inf")


if __name__ == "__main__":
    main()


### FIXED CODE ###

from bisect import bisect_left, bisect_right
from operator import itemgetter
import sys
input = sys.stdin.readline


def main():
    N, Q = map(int, input().split())
    constructions = [None] * N
    for i in range(N):
        S, T, X = map(int, input().split())
        constructions[i] = (S, T, X)
    constructions.sort(key=itemgetter(2))
    D = [int(input()) for _ in range(Q)]
    ans = [-1] * Q
    nxt = list(range(1, Q+1))
    for S, T, X in constructions:
        l = bisect_left(D, S-X)
        r = bisect_right(D, T-X-1)
        idx = l
        while idx < r:
            if ans[idx] == -1:
                ans[idx] = X
            nidx = nxt[idx]
            nxt[idx] = max(nxt[idx], r)
            idx = nidx
    for a in ans:
        print(a)


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

n,m=map(int,input().split())

a = list(map(int, input().split()))

for i in range(m):
    b,c = map(int, input().split())
    a.extend([c]*b)    
a.sort(reverse=True)
print(sum(a[:n]))

### FIXED CODE ###

n,m=map(int,input().split())
a = list(map(int, input().split()))
bc=[list(map(int,input().split())) for _ in range(m)]
#print(bc)
bc=sorted(bc,key= lambda x:x[1],reverse=True)
#print(bc)

bc2=[]
for i in range(m):
    bc2.extend([bc[i][1]]*bc[i][0])
    if len(bc2)>n: break
#print(bc2)
a.extend(bc2)
a.sort(reverse=True)

print(sum(a[:n]))
### BUGGY CODE ###

from collections import deque
def main():
    S = input()
    Q = int(input())
    ans = deque(S)

    for q in range(Q):
        T, *arg = input().split()
        T = int(T)

        if T == 1:
            ans.reverse()
        else:
            if int(arg[0]) == 1:
                ans.appendleft(arg[1])
            else:
                ans.append(arg[1])

    print("".join(ans))
if __name__ == "__main__":
    main()

### FIXED CODE ###

from collections import deque
def main():
    S = input()
    Q = int(input())
    ans = deque(S)
    switch = 1
    count = 0

    for q in range(Q):
        T, *arg = input().split()
        T = int(T)

        if T == 1:
            count += 1
            if switch == 1:
                switch = 2
            elif switch == 2:
                switch = 1
        else:
            if switch == 1:
                if int(arg[0]) == 1:
                    ans.appendleft(arg[1])
                else:
                    ans.append(arg[1])
            else:
                if int(arg[0]) == 1:
                    ans.append(arg[1])
                else:
       
                    ans.appendleft(arg[1])

    if count % 2 == 0:
        print("".join(ans))
    else:
        ans.reverse()
        print("".join(ans))
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,m = na()
a = na()
for i in range(m):
    b,c = na()
    a += ([c]*b)

a = sorted(a)
print(sum(a[len(a)-n:]))

### FIXED CODE ###

import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,m = na()
a = na()

bc = [na() for i in range(m)]
bc.sort(key=lambda x:x[1])

for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break

a.sort(reverse=True)
print(sum(a[:n]))
### BUGGY CODE ###

n = int(input())
s = list(input())

my_dict = {}
for i, c in enumerate(s):
    if not c in my_dict:
        my_dict[c] = []
    my_dict[c].append(i)

lst = []
for key, value in my_dict.items():
    lst.append(value)

if 3 > len(lst):
    print(0)
    exit(0)

cnt = 0
for i in lst[0]:
    for j in lst[1]:
        for k in lst[2]:
            i_, j_, k_ = sorted([i, j, k])
            if j_ - i_ != k_ - j_:
                cnt += 1
print(cnt)


### FIXED CODE ###

n = int(input())
s = input()

cnt = s.count("R") * s.count("G") * s.count("B")

for i in range(len(s)-2):
    for j in range(i + 1, len(s)-1):
        k = 2 * j - i
        if k < n and (s[k] != s[j] and s[j] != s[i] and s[i] != s[k]):
            cnt -= 1
print(cnt)
### BUGGY CODE ###

import heapq

X, Y, Z, K = map(int, input().split(" "))

A = sorted(map(int, input().split(" ")), reverse=True)
B = sorted(map(int, input().split(" ")), reverse=True)
C = sorted(map(int, input().split(" ")), reverse=True)

anslist = []
flag = [[[False for _ in range(K)] for _ in range(K)] for _ in range(K)]

heapq.heappush(anslist, (-(A[0]+B[0]+C[0]), "000"))
count = 0

while count < K:
    p = heapq.heappop(anslist)
    print(p[0]*(-1))
    count += 1
    # push
    a, b, c = map(int, p[1])
    for i, j, k in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]:
        if a+i >= X or b+j >= Y or c+k >= Z:
            continue
        if not flag[a+i][b+j][c+k]:
            flag[a+i][b+j][c+k] = True
            tmp = str(a+i)+str(b+j)+str(c+k)
            heapq.heappush(anslist, (-(A[a+i]+B[b+j]+C[c+k]), tmp))


### FIXED CODE ###

import heapq


class Delicious (object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def total(self):
        return(A[self.x] + B[self.y] + C[self.z])

    def __lt__(self, other):
        return self.total() > other.total()


X, Y, Z, K = map(int, input().split(" "))
A = sorted(map(int, input().split(" ")), reverse=True)
B = sorted(map(int, input().split(" ")), reverse=True)
C = sorted(map(int, input().split(" ")), reverse=True)

ds = []
heapq.heappush(ds, Delicious(0, 0, 0))

check = {}

for _ in range(K):
    d = heapq.heappop(ds)
    while (d.x, d.y, d.z) in check:
        d = heapq.heappop(ds)
    print(d.total())
    check[d.x, d.y, d.z] = True

    if d.x < X-1:
        heapq.heappush(ds, Delicious(d.x+1, d.y, d.z))
    if d.y < Y-1:
        heapq.heappush(ds, Delicious(d.x, d.y+1, d.z))
    if d.z < Z-1:
        heapq.heappush(ds, Delicious(d.x, d.y, d.z+1))
### BUGGY CODE ###

from collections import Counter
n = int(input())
l = [int(i) for i in input().split()]

c = Counter(l)

for i in l:
    ans = 0
    for k ,v in c.items():
        if k == i:
            ans += (v-1) * (v-2) // 2
        else:
            ans += v * (v-1) //2
    print(ans)



### FIXED CODE ###

from collections import Counter
n = int(input())
l = [int(i) for i in input().split()]

c = Counter(l)

total = sum([v * (v-1) // 2 for v in c.values()])
for i in l:
    print(total - c[i] + 1)
### BUGGY CODE ###

# coding: utf-8
N,M,K=map(int,input().split())
FB=[[1 for i in range(N+1)] for j in range(N+1)]

par = [i for i in range(N+1)]
rank = [0 for i in range(N+1)]
 
def find(x):
    if x==par[x]:
        return x
    else:
        par[x]=find(par[x])
        return par[x]
 
 
def unite(x,y):
    rx=find(x)
    ry=find(y)

    if rx==ry:
        return
    
    
    if rank[rx]<rank[ry]:
        par[rx]=ry
    else:
        par[ry]=rx

    if rank[rx]==rank[ry]:
        rank[rx]+=1
 

def some(x,y):
    return find(x)==find(y)
 
for i in range(M):
    A,B=map(int,input().split())
    unite(A,B)
    FB[A][B]=0
    FB[B][A]=0

for i in range(K):
    C,D=map(int,input().split())
    FB[C][D]=0
    FB[D][C]=0

ans=[0 for i in range(N)]

for i in range(1,N+1):
    for j in range(1,N+1):
        if i==j:
            FB[i][j]=0
        if FB[i][j]==0:
            continue
        else:
            if not(some(i,j)):
                FB[i][j]=0
                FB[j][i]=0

for i in range(N):
    ans[i]=sum(FB[i+1])-1

print(*ans)    


### FIXED CODE ###

# coding: utf-8
import collections

N,M,K=map(int,input().split())
F=[[] for i in range(N+1)]
Bl=[[] for i in range(N+1)]

par = [i for i in range(N+1)]
rank = [0 for i in range(N+1)]
 
def find(x):
    if x==par[x]:
        return x
    else:
        par[x]=find(par[x])
        return par[x]
 
 
def unite(x,y):
    rx=find(x)
    ry=find(y)

    if rx==ry:
        return
    
    
    if rank[rx]<rank[ry]:
        par[rx]=ry
    else:
        par[ry]=rx

    if rank[rx]==rank[ry]:
        rank[rx]+=1
 

def some(x,y):
    return find(x)==find(y)
 
for i in range(M):
    A,B=map(int,input().split())
    unite(A,B)
    F[A].append(B)
    F[B].append(A)

for i in range(K):
    C,D=map(int,input().split())
    Bl[C].append(D)
    Bl[D].append(C)

ans=[0 for i in range(N)]
for i in range(N):
    find(i+1)
c = collections.Counter(par)

for i in range(N):
    ans[i]+=c[find(i+1)]-1
    ans[i]-=len(F[i+1])
    for j in range(len(Bl[i+1])):
        if some(i+1,Bl[i+1][j]):
            ans[i]-=1
print(*ans)
### BUGGY CODE ###

import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
for _ in range(m):
    b, c = map(int, input().split())
    for _ in range(b):
        tmp = heapq.heappop(a)
        if tmp >= c:
            heapq.heappush(a, tmp)
            break
        else:
            heapq.heappush(a, c)
print(sum(a))


### FIXED CODE ###

n, m = map(int, input().split())
A = list(map(int, input().split()))
d = {}
for a in A:
    d[a] = 1 if a not in d else d[a] + 1
for _ in range(m):
    b, c = map(int, input().split())
    d[c] = b if c not in d else d[c] + b
ans = 0
cnt = 0
L = sorted(d, reverse=True)
for k in L:
    if d[k] + cnt < n:
        ans += k * d[k]
        cnt += d[k]
    else:
        ans += k * (n - cnt)
        break
print(ans)
### BUGGY CODE ###

import sys
input=sys.stdin.readline
from collections import deque

def main():
    N = int(input())
    Q = [deque(map(int, input().split())) for _ in range(N)]
    games = N*(N-1)//2

    days = 0
    while 1:
        days += 1
        gamed = [0]*N
        for i in range(N):
            if gamed[i] or not len(Q[i]):
                continue
            j = Q[i][0] - 1
            if gamed[j] or not len(Q[j]):
                continue
            if Q[j][0] == i+1:
#                print("game:", j+1, i+1, days)
                Q[i].popleft()
                Q[j].popleft()
                gamed[i] = gamed[j] = 1
                games -= 1
        if not 1 in gamed:
            print(-1)
            return
        if games == 0:
            break

    print(days)

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
input=sys.stdin.readline
from collections import deque

def main():
    N = int(input())
    Q = [deque(map(int, input().split())) for _ in range(N)]
    games = N*(N-1)//2

    days = 0
    nxt = [i for i in range(N)] # 翌日に対戦相手を更新する選手のリスト
    opp = [0]*N # 選手毎の、次の対戦相手のリスト
    while 1:
        days += 1
        for i in nxt:
            opp[i] = Q[i].popleft() - 1 if Q[i] else -1

        tmp = set() # 次の日の更新のため、試合をした選手をメモ
        for i in nxt:
            if i not in tmp and i == opp[opp[i]]:
#                print("game:", i+1, opp[i]+1, days)
                tmp.add(i)
                tmp.add(opp[i])
                games -= 1
        if games == 0:
            break
        elif not tmp:
            print(-1)
            return
        nxt = tmp
    print(days)

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

def get_subgraph(node):
  subgraph = [node]
  for i, j in edges:
    if node == i:
      subgraph.extend(get_subgraph(j))
  return subgraph

n, q = map(int, input().split())
edges = []
for i in range(n-1):
  e, f = map(int, input().split())
  edges.append((e, f))

tasks = []
for i in range(q):
  p, x = map(int, input().split())
  tasks.append((p, x))

counter = list(map(int, list('0'*n)))

for p, x in tasks:
  subgraph = get_subgraph(p)
  for i in subgraph:
    counter[i-1] += x

print(' '.join(map(str, counter)))

### FIXED CODE ###

from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())
G = defaultdict(list)
for i in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1  # 0-indexed
    G[a].append(b)
    G[b].append(a)

# print('G', G)

V = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p = p - 1  # 0-indexed
    V[p] += x


def dfs(v, p):
    if p != -1:
        V[v] += V[p]
    for idx in G[v]:
        if idx == p:
            continue
        dfs(idx, v)


dfs(0, -1)

# print('V', V)

print(' '.join(map(str, V)))
### BUGGY CODE ###

X = int(input())
a = 1
b = 1
while True:
  if(a**5 - b**5 == X):
    print(a , b)
    break
  a += 1
  b += 1
while True:
  if(a**5 - b**5 == X):
    print(a , b)
    break
  a += 1
  b -= 1

### FIXED CODE ###

X = int(input())
 
for a in range(-119 , 120 , 1):
  A = a**5
  for b in range(-119 , 120 , 1):
    B = b**5
    if(A-B==X):
      print(a, b)
      exit()
    else:
      continue
### BUGGY CODE ###

#coding:utf-8

from collections import defaultdict
import itertools

if __name__ == "__main__":
    N, W = map(int, raw_input().split(" "))

    items = defaultdict(list)
    item_num = defaultdict(int)
    for i in range(N):
        w, v = map(int, raw_input().split(" "))
        if i == 0:
            w_list = range(w, w + 4)
        items[w].append(v)
        item_num[w] += 1

    for k in items.keys():
        items[k].sort(reverse = True)

    max_val = 0
    weights = items.keys()
    kinds = len(items.keys())
    for element in itertools.product(range(N + 1), repeat = kinds):
        w_sum = 0
        for i in range(kinds):
            w_sum += weights[i] * element[i]
        if w_sum > W:
            continue
        val = 0
        for i in range(kinds):
            val += sum(items[weights[i]][:element[i]])
        if val > max_val:
            max_val = val

    print max_val


### FIXED CODE ###

#coding:utf-8

N, W = map(int, raw_input().split(" "))
items = [tuple(map(int, raw_input().split(" "))) for _ in range(N)]
dic = {}

def search(i, j):
    if i == N:
        return 0
    if (i, j) not in dic:
        dic[(i, j)] = search(i + 1, j)
        w, v = items[i]
        if j + w <= W:
            dic[(i, j)] = max(dic[(i, j)] , search(i + 1, j + w) + v)
    return dic[(i, j)]

print search(0, 0)
### BUGGY CODE ###

n=int(input())
a=[int(input()) for _ in range(n)]

ans=[]
for i in range(n):
    if a[i] in ans:
        ans.remove(a[i])
    else:
        ans.append(a[i])
print(len(ans))


### FIXED CODE ###

from collections import Counter
n=int(input())
a=[int(input()) for _ in range(n)]

dic=dict(Counter(a))
ans=0
for i,k in dic.items():
    if k%2==1:
        ans+=1
print(ans)
### BUGGY CODE ###

# D - String Formation
# 2020-05-21 08:17


def main():
    s = input()
    n = int(input())
    for _ in range(n):
        query = input().split()
        if query[0] == '1':
            s = s[::-1]
        else:
            if query[1] == '1':
                s = query[2] + s
            else:
                s += query[2]
    return s


if __name__ == '__main__':
    print(main())


### FIXED CODE ###

# D - String Formation
# 2020-05-21 08:17
from collections import deque


def main():
    chars = deque(input())
    chars_append = chars.append
    chars_appendleft = chars.appendleft
    n = int(input())
    direction = 1
    for _ in range(n):
        query = input().split()
        if query[0] == '1':
            direction *= (-1)  # reverse
        else:
            if query[1] == '1':
                # if head-adding: 1, tail: -1
                head = direction
            else:
                head = (-direction)
            if head == 1:
                chars_appendleft(query[2])
            else:
                chars_append(query[2])

    if direction == -1:
        chars.reverse()
    return ''.join(chars)


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

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
 

N, Q = map(int, input().split())

F = [0 for i in range(N)]
T = [[]for i in range(N)]

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

X = [0 for i in range(N)]
for i in range(Q):
    p, x = map(int, input().split())
    X[p-1] += x

ans = [X[i] for i in range(N)]

def dfs(x):
    if F[x] == 1:
        return
    F[x] = 1
    for j in range(len(T[x])):
        if F[T[x][j]] == 1:
            continue
        ans[T[x][j]] += ans[x]
        dfs(T[x][j])

dfs(0)

for i in range(N):
    print(ans[i], end =" ")


### FIXED CODE ###

N, Q = map(int, input().split())

F = [0 for i in range(N)]
T = [[]for i in range(N)]

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

X = [0 for i in range(N)]
for i in range(Q):
    p, x = map(int, input().split())
    X[p-1] += x

ans = [X[i] for i in range(N)]
for i in range(N):
    F[i] = 1
    for j in range(len(T[i])):
        if F[T[i][j]] == 1:
            continue
        ans[T[i][j]] += ans[i]

for i in range(N):
    print(ans[i], end =" ")