Harryxun/stf_run huggingface dataset:
H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

li = [[]] * (H * W)
for i in range(H):
  for j in range(W):
    li[A[i][j] - 1] = [j, i]

cost = [[0 for _ in range(H * W)] for _ in range(D)]
for i in range(H * W):
  d, m = divmod(i, D)
  if d > 0:
    cost[m][d] = cost[m][d - 1] + abs(li[i][0] - li[i - D][0]) + abs(li[i][1] - li[i - D][1]) 

# print(cost)

for l, r in LR:
  print(cost[(l - 1) % D][r // D] - cost[(l - 1) % D][l // D])

### FIXED CODE ###

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

li = [[]] * (H * W)
for i in range(H):
  for j in range(W):
    li[A[i][j] - 1] = [j, i]

cost = [[0 for _ in range((H * W) // D + 1)] for _ in range(D)]
for i in range(H * W):
  d, m = divmod(i, D)
  if d > 0:
    cost[m][d] = cost[m][d - 1] + abs(li[i][0] - li[i - D][0]) + abs(li[i][1] - li[i - D][1]) 

# print(cost)

for l, r in LR:
  print(cost[(l - 1) % D][(r - 1) // D] - cost[(l - 1) % D][(l - 1) // D])
s = input()
# print(s)
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        print(i+1,i+2)
        break
    if i<=len(s)-2:
        if s[i]==s[i+2]:
            print(i+1,i+3)
            break
    if i == len(s)-3:
        print(-1,-1)

### FIXED CODE ###

s = input()
# print(s)
flag=0
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        print(i+1,i+2)
        flag = 1
        break
    if i<len(s)-2:
        if s[i]==s[i+2]:
            print(i+1,i+3)
            flag = 1
            break
if flag ==0:
    print(-1,-1)
n=int(input())
a=list(input())
ans=''
for i in range(1,n+1):
    ans+=str(a.index(str(i))+1)+' '

print(ans)


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b=[0]*n
for i in range(n):
    b[a[i]-1]=str(i+1)

print(' '.join(b))
N = input 
if 7 in N:
    print(yes)
else:
    print(no)


### FIXED CODE ###

n = input()
N = str(n)
if "7" in N:
    print("Yes")
else:
    print("No")
n, m, x, y = map(int, input().split())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
print('No War' if any(x < i <= y for i in \
	range(max(a) + 1, min(b) + 1)) else 'War')

### FIXED CODE ###

n, m, x, y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print('No War' if any(x < i <= y for i in \
	range(max(a) + 1, min(b) + 1)) else 'War')
N, S = (input() for i in 'NS')
res = 0
for i in range(N):
    s, t = set(S[:i]), set(S[i:])
    res = max(res, len(s.intersection(t)))
print(res)

### FIXED CODE ###

N, S = (input() for i in 'NS')
res = 0
for i in range(int(N)):
    s, t = set(S[:i]), set(S[i:])
    res = max(res, len(s.intersection(t)))
print(res)
n=int(input())

t=[0]*n
a=[0]*n

for i in range(n):
    t,a=map(int,input().split()) 
  
  
  
for i in range(n-1):
  while (t[i]>t[i+1]) or (a[i]>a[i+1]):
    for k in range(1,100000000000000000000000000000000):
      t[i+1]*=k
      a[i+1]*=k
      
print(a[n]+t[n])

### FIXED CODE ###

N = int(input())
x, y = 1, 1
for i in range(N):
    a, b = map(int, input().split())
    n = max((x-1 + a) // a, (y - 1 +b) // b)
    x = n * a
    y = n * b
print(x + y)
N, X = map(int, input().split())
m = []
count = N
X_new = X

for i in range(0, N):
	m.append(input())

print(N + (X - sum(m)) // min(m))

### FIXED CODE ###

N, X = map(int, input().split())
m = []
for i in range(N):
	m.append(int(input()))
print(N + (X - int(sum(m))) // int(min(m)))
a,b,c = map(int, input().slplit())

s = a * b / 2

print(s)

### FIXED CODE ###

a,b,c = map(int, input().split())

print(a * b // 2)
N=int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
res=0
for i in range(N):
  for j in range(N):
    ans+=((xy[i][0]-xy[j][0])**2+(xy[i][1]-xy[j][1])**2)**0.5
    print(res/N)

### FIXED CODE ###

'''
https://atcoder.jp/contests/abc145/tasks/abc145_c
'''
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    N = int(input())
    grid = [list(map(int, input().split())) for _ in range(N)]
    res = 0
    for path in permutations(grid, N):
        for i in range(N-1):
            res += ((path[i][0]-path[i+1][0])**2 + (path[i][1]-path[i+1][1])**2)**0.5
    a = 1
    for i in range(2, N+1):
        a *= i
    print(res/a)


if __name__ == '__main__':
    main()
import pprint

class Point:
  def __init__(self, x, y):
    self.x = x
    self.y = y
  
  def __str__(self):
    return f"Point(x={self.x}, y={self.y})"

  def __repr__(self):
    return self.__str__()

def parse():
  H, W = map(int, input().split(" "))
  grid = []
  for h in range(H):
    grid.append(list(input()))
  return H, W, grid

def print_grid(grid):
  H = len(grid)
  print("grid:")
  for y in range(H):
    print(grid[y])

def is_all_black(grid):
  H = len(grid)
  W = len(grid[0])
  for y in range(H):
    for x in range(W):
      if grid[y][x] == ".": return False
  return True

def main():
  H, W, grid = parse()
  # print(H, W, grid)

  vecs = [Point(0, -1), Point(0, 1), Point(-1, 0), Point(1, 0)]
  
  # 初期の黒色の場所を追加
  black_points = []
  for y in range(H):
    for x in range(W):
      if grid[y][x] == "#":
        black_points.append(Point(x, y))
  # print(black_points)

  ans = 0
  while not is_all_black(grid):
    next_black_points = []
    for black_point in black_points:
      # print("black_point:", black_point)
      for vec in vecs:
        target_point = Point(black_point.x + vec.x, black_point.y + vec.y)
        if target_point.x < 0: continue
        if target_point.y < 0: continue
        if target_point.x >= W: continue
        if target_point.y >= H: continue
        if grid[target_point.y][target_point.x] == "#": continue
        grid[target_point.y][target_point.x] = "#"
        next_black_points.append(target_point)
    black_points = next_black_points
    ans += 1

    # print("next_black_points:", next_black_points)
    # print_grid(grid)
    # input()
  
  print(ans)

if __name__ == "__main__":
  main()

### FIXED CODE ###


import numpy as np

def parse():
  H, W = map(int, input().split(" "))
  grid = [list(input()) for y in range(H)]
  return H, W, grid

def print_grid(grid):
  H = len(grid)
  print("grid:")
  for line in grid:
    for c in line:
      print(c, end="")
    print("")

def main():
  H, W, grid = parse()
  # print(H, W)
  # print_grid(grid)

  # visited = [[W * H if grid[y][x] == "." else 0 for x in range(W)] for y in range(H)]
  visited = np.zeros((H, W), int)
  for y in range(H):
    # visited.append([])
    for x in range(W):
      if grid[y][x] == ".":
        visited[y][x] = H * W

  for y in range(1, H):  # ↓
    # 要素ごとの処理
    # print(visited)
    visited[y][:] = np.minimum(visited[y][:], visited[y - 1][:] + 1)
    # print(visited)
    # input()
    # print(visited[y])
    # exit(0)

  for y in range(H - 2, -1, -1):
    visited[y, :] = np.minimum(visited[y, :], visited[y + 1][:] + 1)

  for x in range(1, W):
    visited[:, x] = np.minimum(visited[:, x], visited[:, x - 1] + 1)

  for x in range(W - 2, -1, -1):
    visited[:, x] = np.minimum(visited[:, x], visited[:, x + 1] + 1)

  # print(visited)
  # exit(0)

  # max_len = 0
  # for y in range(H):
  #   max_val = max(visited[y])
  #   max_len = max_len if max_len > max_val else max_val
  # print(max_len)
  print(np.max(visited))

if __name__ == "__main__":
  main()
# このコメントを外さないとTLE
# import sys
# input = sys.stdin.readline

import sys,builtins,io 
def input(): 
    r = sys.stdin.readline() 
    if not r: 
        r = builtins.input() 
        return r
x = input()

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

S = [None] * N
T = [None] * N
X = [None] * N
for i in range(N):
    S[i], T[i], X[i] = map(int, input().split())

D = [None] * Q
for i in range(Q):
    D[i] = int(input())

roadworks = [(x, s, t) for x, s, t in zip(X, S, T)]
roadworks = sorted(roadworks, key=lambda x: x[0])
#print(roadworks)
 
# Dは入力時点でソート済み
walkers = D[:]
ans = [-1] * Q
skip = [-1] * Q
 
import bisect
for x, s, t in roadworks:
    left = bisect.bisect_left(walkers, s - x)
    right = bisect.bisect_left(walkers, t - x)
    
    i = left
    while i < right:
        if skip[i] == -1:
            ans[i] = x
            skip[i] = right
            i += 1
        else:
            i = skip[i]
 
    
#print(walkers)
for a in ans:
    print(a)
    

### FIXED CODE ###

import sys
input = sys.stdin.readline
N, Q = map(int, input().split())

S = [None] * N
T = [None] * N
X = [None] * N
for i in range(N):
    S[i], T[i], X[i] = map(int, input().split())

D = [None] * Q
for i in range(Q):
    D[i] = int(input())
    
# X S T
roadworks = [(x, s, t) for x, s, t in zip(X, S, T)]
roadworks = sorted(roadworks, key=lambda x: x[0])
#print(roadworks)

# Dは入力時点でソート済み
walkers = D[:]
ans = [-1] * Q
skip = [-1] * Q

import bisect
for x, s, t in roadworks:
    left = bisect.bisect_left(walkers, s - x)
    right = bisect.bisect_left(walkers, t - x)
    
    i = left
    while i < right:
        if skip[i] == -1:
            ans[i] = x
            skip[i] = right
            i += 1
        else:
            i = skip[i]

    
#print(walkers)
for a in ans:
    print(a)
a,b,c=map(int, nput().split())
print("Yes"if a==b==c else"No")

### FIXED CODE ###

a, b, c = map(int, input().split())
if a == b == c:
    print("Yes")
else:
    print("No")
from collections import defaultdict

n, k = map(int, input().split())
A = list(map(int, input().split()))
S = [0] * (n + 1)
for i in range(n):
    S[i+1] = S[i] + A[i]

C = defaultdict(int)
ans = 0
for i in range(n+1):
    j = i - k
    mi = (S[i] - i) % k
    mj = (S[j] - j) % k
    if j >= 0:
        C[mj] -= 1
    ans += C[mi]
    C[mi] += 1

### FIXED CODE ###

from collections import defaultdict

n, k = map(int, input().split())
A = list(map(int, input().split()))
S = [0] * (n + 1)
for i in range(n):
    S[i+1] = S[i] + A[i]

f = lambda n: (S[n] - n) % k
C = defaultdict(int)
ans = 0
for i in range(n):
    C[f(i)] += 1
    j = i - k + 1
    if 0 <= j:
        C[f(j)] -= 1
    ans += C[f(i+1)]
print(ans)
N,K,*X = map(int,open(0).read().split())
S = bin(K)[2:]
L = len(S)
ls = [(a,b) for a,b in zip(*[iter(X)]*2)]
s = '0'
ans = 0
for i,c in enumerate(S):
  if c=='0':
    s += c
    continue
  j = L-i
  m = (1<<(j-1))-1
  n = int(s,2)<<j
  x = m|n
  v = 0
  for a,b in ls:
    if x==x|a:
      v += b
  ans = max(v,ans)
  s += c
v = 0
for a,b in ls:
  if K==K|a:
    v += b
ans = max(ans,v)
print(ans)

### FIXED CODE ###

N,K,*X = map(int, open(0).read().split())
S = bin(K)[2:]
L = len(S)
one = set()
ls = [(a,b) for a,b in zip(*[iter(X)]*2) if a<=K]
kls = [K]
ans = 0
for i in range(L):
  if S[i]=='1':
    m = int(S[:i]+'0'+'1'*(L-i-1),2)
    kls.append(m)
for k in kls:
  m = 0
  for a,b in ls:
    if k==k|a:
      m += b
  ans = max(ans,m)
print(ans)
N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7) 

N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)
import math

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/math.gcd(N,M))
G=math.gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)        

### FIXED CODE ###

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/gcd(N,M))
G=gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)
a=input()
b = input()
cnt = 0
for i  on range(len(a)):
    if a[i]!=b[i]: cnt+=1
print(cnt)

### FIXED CODE ###

a = input()
b = input()
cnt = 0
for i in range(len(a)):
    if a[i]!=b[i]:
        cnt+=1
print(cnt)
n = int(input())
l = input().split()
l.sort()
print(abs(int(l[0]) - int(l[-1])))

### FIXED CODE ###

n = int(input())
result = n

while True:
    if (result % n == 0) & (result % 2 == 0):
        print(result)
        break
    result = result * 2
l = [int(i) for i in input().split()] 
l_sort = np.sort(l)
l_diff = np.diff(l_sort)
 
print(np.sum(l_diff))

### FIXED CODE ###

l = [int(i) for i in input().split()] 
ans = 0

for j in range(len(l) - 1):
	for i in range(len(l) - 1):
		if l[i+1] < l[i]:
			tmp = l[i+1]
			l[i+1] = l[i]
			l[i] = tmp
        
for i in range(len(l) - 1):
	ans += l[i+1] - l[i]
    
print(ans)
r=int(input())
print(3*a**2)

### FIXED CODE ###

r=int(input())
print(3*(r**2))
# ==================================================== #
# -*- coding: utf-8 -*
n, m = map(int, input().split())

arr = [0] * n
for i in range(m):
    a, b = map(int, input().split())
    arr[a] += 1
    arr[b] += 1
for i in range(n):
    print(arr[i])

### FIXED CODE ###

# ==================================================== #
# -*- coding: utf-8 -*
n, m = map(int, input().split())
arr = [0] * 114514

for i in range(m):
    a, b = map(int, input().split())
    arr[a-1] += 1
    arr[b-1] += 1

for i in range(n):
    print(arr[i])
while(True):
    n = map(int, raw_input().split())
    if n[0] == -1 and n[1] == -1 and n[-2] == -1:
        break
    if n[0] == -1 or n[1] == -1:
        print "F"
    elif n[0]+n[1] >= 80:
        print "A"
    elif n[0]+n[1] >= 65:
        print "B"
    elif n[0]+n[1] >= 50:
        print "C"
    elif n[0]+n[1] >= 30:
        if n[2] >= 50:
            print "C"
        else:
            print "D"
    else:
        print "F"
GINOPC-003M-no-MacB

### FIXED CODE ###

while(True):
    n = map(int, raw_input().split())
    if n[0] == -1 and n[1] == -1 and n[-2] == -1:
        break
    if n[0] == -1 or n[1] == -1:
        print "F"
    elif n[0]+n[1] >= 80:
        print "A"
    elif n[0]+n[1] >= 65:
        print "B"
    elif n[0]+n[1] >= 50:
        print "C"
    elif n[0]+n[1] >= 30:
        if n[2] >= 50:
            print "C"
        else:
            print "D"
    else:
        print "F"
import itertools

def resolve():
    n, m = map(int, input().split())
    S = [[0]*m for i in range(m)]

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

    ans = 0
    for p in itertools.permutations(range(n)):
        if p[0] != 0:
            break;
        tmp = 1
        for i, v in enumerate(p):
            if i > m:
                tmp *= S[[i+1]][p[i+1]]
        ans += tmp

    print(ans)

if __name__ == '__main__':
    resolve()

### FIXED CODE ###

# サンプルコード計算量お試し

import itertools

N, M = map(int, input().split())
adj_matrix = [[0]* N for _ in range(N)]

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

cnt = 0

for each in itertools.permutations(range(N)):
    if each[0] != 0:
        break
    factor = 1
    for i in range(N-1):
        factor *= adj_matrix[each[i]][each[i+1]]
    cnt += factor

print(cnt)
import functools
import math

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

def lcm(x, y):
    return (x * y) // math.gcd(x, y)
def lcm_list(numbers):
    return functools.reduce(lcm, numbers, 1)

lcm_a = lcm_list(a)

b = [lcm_a//a[i] for i in range(n)]

print(sum(b) % ((10**9)+7))

### FIXED CODE ###

import functools

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

def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n

def lcm(m, n):
    return m//gcd(m, n)*n

def lcm_list(nums):
        return functools.reduce(lcm, nums)

lcm_a = lcm_list(a)

b = [lcm_a//a[i] for i in range(n)]

print(sum(b) % ((10**9)+7))
n = int(input())
a = list(map(int,input().split()))

h =[]

if a[0] > a[1]:
    h.append(a[0] - a[1])
    a[1] = a[0]
else:
    h.append(0)
    
for i in range(2,n):
    if a[i-1] > a[i]:
        h.append(a[i-1]- a[i])
        a[i] = a[i-1]
    else:
        h.append(0)
        
print(sum(h))
    

### FIXED CODE ###

import sys

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

h =[]

if n == 1:
    print(0)
    sys.exit()

if a[0] > a[1]:
    h.append(a[0] - a[1])
    a[1] = a[0]
else:
    h.append(0)
    
for i in range(2,n):
    if a[i-1] > a[i]:
        h.append(a[i-1]- a[i])
        a[i] = a[i-1]
    else:
        h.append(0)
        
print(sum(h))
n = int(input())
hash = [0 for i in range(n)]
a = [int(i) for i in input().split()]
for i in a:
	hash[i-1] += 1
for i in hash:
	print(i

### FIXED CODE ###

n = int(input())
hash = [0 for i in range(n)]
a = [int(i) for i in input().split()]
for i in a:
	hash[i-1] += 1
for i in hash:
	print(i)
n,m=list(map(int,input().split()))

from collections import defaultdict

par=[i for i in range(n)]
ro=defaultdict(int)
for i in range(n):
    ro[i]=1
def root(i):
    if par[i]==i:
        return i
    else:
        return root(par[i])

def unite(x,y):
    rx=root(x)
    ry=root(y)
    if rx!=ry:
        par[rx]=ry
        ro[ry]+=ro[rx]
        ro[rx]=0

for i in range(m):
    a,b=list(map(int,input().split()))
    a-=1
    b-=1
    unite(a,b)


# for i in range(n):
#     ro[root(i)]+=1

print(max(ro.values()))

### FIXED CODE ###

n,m=list(map(int,input().split()))
par=[i for i in range(n)]
ro=dict()

for i in range(n):
    ro[i]=1
def root(i):
    if par[i]==i:
        return i
    else:
        a=root(par[i])
        par[i]=a
        return a

def unite(x,y):
    rx=root(x)
    ry=root(y)
    if rx!=ry:
        par[rx]=ry
        ro[ry]+=ro[rx]
        ro[rx]=0

for i in range(m):
    a,b=list(map(int,input().split()))
    a-=1
    b-=1
    unite(a,b)

print(max(ro.values()))
x,y=input().split()
x=int(x)
y=int(y)

def lcm(x, y):
    return (x * y)/math.gcd(x, y)

print(lcm(x, y))

### FIXED CODE ###

a,b=input().split()
a=int(a)
b=int(b)
x = a * b

if (a<b):
  tmp = a
  a = b;
  b = tmp

r = a % b
while(r!=0):
  a = b
  b = r
  r = a % b

print(int(x/b))
s = input()
print(s.split(',')

### FIXED CODE ###

s = input()
print(*s.split(","))
N=int(input())

x %= 1000

Otsuri = 1000-x

print(Otsuri)

### FIXED CODE ###

N=int(input())

x = N % 1000

Otsuri = 1000-x

if Otsuri == 1000:
  print(0)
else:
  print(Otsuri)
import math
a = int(raw_input())
print "%.5f" % (a*a*math.pi)
print "%.5f" % (2*a*math.pi)

### FIXED CODE ###

import math
a = float(raw_input())
print "%.5f" % (a*a*math.pi)
print "%.5f" % (2*a*math.pi)
lis = input().split()
N = int(lis[0])
M = int(lis[1])
H = input().split()
T = list(range(N+1))

for i in range(M):
    R = input().split()
    r1 = int(R[0])-1
    r2 = int(R[1])-1
    #print("r1=",r1,"r2=",r2)
    if int(H[r1]) >= int(H[r2]):
        if int(r2+1) in T:
            T.remove(int(r2+1))
    if int(H[r1]) < int(H[r2]):
        if int(r1+1) in T:
            T.remove(int(r1+1))

print(len(T) - 1)


### FIXED CODE ###

a = input()
 
if a == "ABC":
  print("ARC")
if a == "ARC":
  print("ABC")
from math import gcd
from functools import reduce
import sys

def lcm(p,q):
    return p*q//gcd(p,q)
sys.setrecursionlimit(3001)
N=int(input())
a=list(map(int,input().split(' ')))
m=reduce(lcm,a)-1
result=0
for i in a:
    result+=m%i
print(result)


### FIXED CODE ###

def gcd(m,n):
    while n!=0:
        l=n
        n=m%n
        m=l
    return m

def lcm(p,q):
    return p*q//gcd(p,q)

N=int(input())
a=list(map(int,input().split(' ')))
lcm_=lcm(a[0],a[1])
for i in range(2,len(a)):
    lcm_=lcm(lcm_,a[i])
result=0
m=lcm_-1
for i in a:
    result+=m%i
print(result)
n, m = map(int, input().split())
ans = []
ans_0 = []
for i in range(m):
    ans_0 = list(map(str, input().split()))
    ans.append(ans_0)
list0 = []
for j in range(n):
    list0.append(0)
sei = 0
pen = 0
for lis in ans:
    if list0[int(lis[0])] == 0:
        if lis[1] == "AC":
            sei += 1
            list0[int(lis[0])] = 1
        else:
            pen += 1
    else:
        if lis[1] == "AC":
            pass
        else:
            pen += 1
print(str(sei)+" "+str(pen))



### FIXED CODE ###

n, m = map(int, input().split())
ans = []
ans_0 = []
for i in range(m):
    ans_0 = list(map(str, input().split()))
    ans.append(ans_0)
list0 = []
listpen = []
for j in range(n):
    list0.append(0)
for k in range(n):
    listpen.append(0)
sei = 0
pen = 0
for lis in ans:
    if list0[int(lis[0])-1] == 0:
        if lis[1] == "AC":
            sei += 1
            list0[int(lis[0])-1] = 1
        else:
            listpen[int(lis[0])-1] += 1
    else:
        pass
for l in range(n):
    if list0[l] == 1:
        pen += listpen[l]
print(str(sei)+" "+str(pen))
N=int(input())
S=input()
S=S+'9'
ans=0
for i in range(N):
    if S[i]=='#' and S[i+1]=='.':
        cntj=0
        for j in range(i,-1,-1):
            if S[j]=='#':
                cntj+=1
            else:
                break
        cntk=0
        for k in range(i+1,N+1):
            if S[k]=='.':
                cntk+=1
            else:
                break
        if cntj < cntk:
            ans += cntj
            S = S[:i-cntj]+'.'*cntj+S[i+1:]
        else:
            ans += cntk
            S = S[:i]+'#'*cntk+S[i+1+cntk:]
print(ans)

### FIXED CODE ###

n=int(input())
s=input()
w=s.count('.')
b=0
ans=w
for i in range(n):
    if s[i]=='#':
        b+=1
    else:
        w-=1
    ans=min(ans,w+b)
print(ans)
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
que2=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,data[i])
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
for i in range(k,n):
    if data[i][1]in neta:
        pass
    else:
        heapq.heappush(que2,data[i])
mx=x+p*p
while que1:
    a=heapq.heappop(que1)
    b==None
    while que2:
        b=heapq.heappop(que2)
        if b[1] not in neta:
            break
        b=None
    if b==None:
        break
    x=x+a[0]-b[0]
    p+=1
    neta.add(b[1])
    mx=max(mx,x+p*p)
print(mx)


### FIXED CODE ###

import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,(-data[i][0],data[i][1]))
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
mx=x+p*p
for i in range(k,n):
    if data[i][1] in neta:
        pass
    else:
        if que1:
            a=heapq.heappop(que1)
            x=x-a[0]-data[i][0]
            p+=1
            neta.add(data[i][1])
            mx=max(mx,x+p*p)
        else:
            break
print(mx)
n, *b = map(int, open(0).read().split())
a = [0]*n
b += float("inf")
for i in range(n):
  a[i] = min(b[i-1], b[i])
print(sum(a))

### FIXED CODE ###

n, *b = map(int, open(0).read().split())
a = [0]*n
b.append(float("inf"))
for i in range(n):
  a[i] = min(b[i-1], b[i])
print(sum(a))