from sys import stdin, stdout

# RL, RRL, RLL, RRLL
#
#
#
def shift(c_a):
    c = c_a[0]
    for i in range(n-1):
        c_a[i] = c_a[i+1]
    c_a[n-1] = c

def omkar_and_bed_wars(n, s):

    c_a = list(s)

    r1 = cal_dp(n, c_a)
    shift(c_a)
    r2 = cal_dp(n, c_a)
    shift(c_a)
    r3 = cal_dp(n, c_a)
    shift(c_a)
    r4 = cal_dp(n, c_a)

    return min(r1, r2, r3, r4)

def cal_dp(n, c_a):
    dp = [10**9] * n
    for i in range(1, n):
        # RL
        rl = get_cnt(i-1, c_a, 'RL')
        if i > 1:
            rl += dp[i-2]

        rrl = 10 ** 9
        rll = 10 ** 9
        if i > 1:
            rrl = get_cnt(i - 2, c_a, 'RRL')
            rll = get_cnt(i - 2, c_a, 'RLL')
            if i > 2:
                rrl += dp[i - 3]
                rll += dp[i - 3]

        rrll = 10 ** 9
        if i > 2:
            rrll = get_cnt(i - 3, c_a, 'RRLL')
            if i > 3:
                rrll += dp[i - 4]

        dp[i] = min(rl, rrl, rll, rrll)

    #print(''.join(c_a))
    #print(dp)
    return dp[n-1]

def get_cnt(idx, c_a, s):
    cnt = 0
    for i in range(len(s)):
        if c_a[idx+i] != s[i]:
            cnt += 1
    return cnt

t = int(stdin.readline())
for _ in range(t):
    n = int(stdin.readline())
    s = stdin.readline().strip()
    stdout.write(str(omkar_and_bed_wars(n, s)) + '\n')


from sys import stdin, stdout

# 0   0   0   0
# 2^3 2^4 2^5 2^6
# 0   0   0   0
# 2^5 2^6 2^7 2^8
#

def construct(n):
    p_a = [[0 for _ in range(n)] for _ in range(n)]
    v = 1 << 3

    for i in range(1, n, 2):
        for j in range(n):
            p_a[i][j] = v
            v = v << 1
        v = v >> (n-2)
    return p_a

def omkar_and_duck(n, p_a, k):

    c = 0
    b = 1 << 3
    for i in range(n//2):
        while k & b == 0:
            stdout.write(str(2*i + 1) + ' ' + str(c + 1) + '\n')

            b = b << 1
            c += 1

        stdout.write(str(2 * i + 1) + ' ' + str(c + 1) + '\n')
        stdout.write(str(2 * i + 2) + ' ' + str(c + 1) + '\n')

        while k & (b << 1) != 0:
            c += 1
            stdout.write(str(2 * i + 2) + ' ' + str(c + 1) + '\n')
            b = b << 1

        b = b << 2

    if n % 2 == 1:
        for i in range(c, n):
            stdout.write(str(n) + ' ' + str(i + 1) + '\n')

    return

n = int(stdin.readline())
p_a = construct(n)
for p in p_a:
    stdout.write(' '.join(map(str, p)) + '\n')
stdout.flush()

q = int(stdin.readline())
for _ in range(q):
    k = int(stdin.readline())
    omkar_and_duck(n, p_a, k)
    stdout.flush()


from sys import stdin, stdout

# 0   0   0   0
# 2^3 2^4 2^5 2^6
# 0   0   0   0
# 2^5 2^6 2^7 2^8
#

def construct(n):
    p_a = [[0 for _ in range(n)] for _ in range(n)]
    v = 1 << 3

    for i in range(1, n, 2):
        for j in range(n):
            p_a[i][j] = v
            v = v << 1
        v = v >> (n-2)
    return p_a

def omkar_and_duck(n, p_a, k):

    c = 0
    b = 1 << 3
    for i in range(n//2):
        while k & b == 0:
            stdout.write(str(2*i) + ' ' + str(c) + '\n')

            b = (b << 1)
            c += 1
        stdout.write(str(2 * i) + ' ' + str(c) + '\n')
        stdout.write(str(2 * i+1) + ' ' + str(c) + '\n')

        b = b << 2

    if n % 2 == 1:
        for i in range(c, n):
            stdout.write(str(n-1) + ' ' + str(c) + '\n')

    return

n = int(stdin.readline())
p_a = construct(n)
for p in p_a:
    stdout.write(' '.join(map(str, p)) + '\n')
stdout.flush()

q = int(stdin.readline())
for _ in range(q):
    k = int(stdin.readline())
    omkar_and_duck(n, p_a, k)
    stdout.flush()


# HEY STALKER
n = int(input())
l = [[0 for ti in range(n+2)]for yi in range(n+2)]
for i in range(1, n+2):
    for j in range(1, n+2):
        if not (i % 2):
            l[i][j] = i+j
for i in range(1, n+1):
    for j in range(1, n+1):
        if not l[i][j]:
            print(l[i][j], end=" ")
        else:
            print((1 << (l[i][j])), end=" ")
    print()
q = int(input())
for _ in range(q):
    p = int(input())
    print("1 1")
    i = 1
    j = 1
    for t in range(2, 2*n):
        if l[i+1][j] != 0 and (p & (1 << l[i+1][j])):
            i += 1
        elif l[i+1][j] == 0 and not (p & (1 << l[i][j+1])):
            i += 1
        else:
            j += 1
        print(i, j)


import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def Rtest(T):
    case,err=0,0
    for i in range(T):
        inp=INP()
        a1,ls=naive(*inp)
        a2=solve(*inp)
        if a1!=a2:
            print((a1,a2),inp)
            err+=1
        case+=1
    print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
    #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

ans=0

def solve(x):
    h,w=1,1
    print(h,w)
    sys.stdout.flush()
    for i in range(2*n-2-1,-1,-1):
        d=(x>>i)&1
        if d==((h+1)%2):
            h+=1
        else:
            w+=1
        print(h,w)
        sys.stdout.flush()
    return

n=I()

for i in range(n):
    if i%2==0:
        p=[]
        for j in range(2*n-2-i,2*n-2-n-i,-1):
            p+=1<<j,
        print(*p)
        sys.stdout.flush()
    else:
        print(*[0]*n)
        sys.stdout.flush()

q=I()
for i in range(q):
    x=I()
    solve(x)

exit()


from sys import stdout
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())
ans=['0']
for i in range(n-1):
    ans.append(str(pow(2,i)))
print(' '.join(ans))
stdout.flush()
for i in range(1,n):
    if i%2==1:
        ans=['0']*(n)
        print(' '.join(ans))
        stdout.flush()
    else:
        ans=[]
        for j in range(n):
            ans.append(str(pow(2,i+j-1)))
        print(' '.join(ans))
        stdout.flush()
q=int(input())
for i in range(q):
    k=int(input())
    kstr=base10toN(k,2)
    klist=[]
    for i in kstr:
        klist.append(i)
    klist.reverse()
    for i in range(2*n-2-len(kstr)):
        klist.append('0')
    xpos=1
    ypos=1
    ans=[]
    ans.append(str(xpos))
    ans.append(str(ypos))
    print(' '.join(ans))
    stdout.flush()
    pointer=0
    while xpos<n or ypos<n:
        ans=[]
        if ypos%2==1:
            if klist[pointer]=='1':
                xpos+=1
            else:
                ypos+=1
        else:
            if klist[pointer]=='0':
                xpos+=1
            else:
                ypos+=1
        pointer+=1
        ans.append(str(xpos))
        ans.append(str(ypos))
        print(' '.join(ans))
        stdout.flush()


from sys import stdout
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())
ans=['0']
for i in range(n-1):
    ans.append(str(pow(2,i)))
print(' '.join(ans))
stdout.flush()
for i in range(1,n):
    if i%2==1:
        ans=['0']*(n)
        print(' '.join(ans))
        stdout.flush()
    else:
        ans=[]
        for j in range(n):
            ans.append(str(pow(2,i+j-1)))
        print(' '.join(ans))
        stdout.flush()
q=int(input())
for i in range(q):
    k=int(input())
    kstr=base10toN(k,2)
    klist=[]
    for i in kstr:
        klist.append(i)
    klist.reverse()
    for i in range(2*n-2-len(kstr)):
        klist.append('0')
    xpos=1
    ypos=1
    ans=[]
    ans.append(str(ypos))
    ans.append(str(xpos))
    print(' '.join(ans))
    stdout.flush()
    pointer=0
    while xpos<n or ypos<n:
        ans=[]
        if ypos%2==1:
            if klist[pointer]=='1':
                xpos+=1
            else:
                ypos+=1
        else:
            if klist[pointer]=='0':
                xpos+=1
            else:
                ypos+=1
        pointer+=1
        ans.append(str(ypos))
        ans.append(str(xpos))
        print(' '.join(ans))
        stdout.flush()


from sys import stdout
n = int(input())
steps = 2 * (n - 1)

for x in range(n):
    for y in range(n):
        d = x + y - 1
        print((1 << d) if x % 2 == 1 else 0, end=' ')
    print()

stdout.flush()

q = int(input())
for t in range(q):
    k = int(input())

    x = y = 0
    print(x + 1, y + 1)

    for st in range(steps):
        nextD = x + y
        nextBit = (k >> nextD) & 1
        if x % 2 == 0:
            if nextBit:
                x += 1
            else:
                y += 1
        else:
            if nextBit:
                y += 1
            else:
                x += 1
        print(x + 1, y + 1)

    stdout.flush()


import sys
n = int(input())
for i in range(n):
    if i%2 == 0:
        row = [2**(i+j) for j in range(n)]
    else:
        row = [0] * n
    print(" ".join(map(str, row)))

sys.stdout.flush()

q = int(input())
for __ in range(q):
    k = int(input())
    k_b = "{0:b}".format(k).zfill(n*2 - 1)[::-1]
    prev = k_b[0]
    x, y = 1, 1
    print(x, y)
    for c in k_b[1:]:
        if c != prev:
            x = x+1
            prev = c
        else:
            y = y+1
        print(x, y)
    sys.stdout.flush()


def dfs(n, i, j, vis, prevDir, num):
    if (i, j) in vis:
        return
    if i >= n or j >= n:
        return
    rans = 0
    if j % 2 == 1:
        rans = (1 << (i + j))
    vis[(i, j)] = rans
    dfs(n, i + 1, j, vis, 0, num)
    dfs(n, i, j + 1, vis, 1, num)
def dfs2(n, i, j, vis, visOrd, prevDir, num):
    if i == n - 1 and j == n - 1:
        return True
    if i >= n or j >= n:
        return None
    a = (1 << (i + j)) & num
    if j % 2 != min(a, 1):
        return None
    r = dfs2(n, i + 1, j, vis, visOrd, 0, num)
    s = dfs2(n, i, j + 1, vis, visOrd, 1, num)
    if r:
        visOrd.append((i+1, j))
    elif s:
        visOrd.append((i, j+1))
    else:
        return None
    return True

n = int(input())
vis = {}
dfs(n, 0, 0, vis, 0, 0)
for i in range(0, n):
    for j in range(0, n):
        print(vis[(i, j)], end=' ')
    print()

q = int(input())
for _ in range(q):
    k = int(input())
    visOrd = []
    dfs2(n, 0, 0, {}, visOrd, 0, k)
    print(1, 1)
    for val in reversed(visOrd):
        print(1+val[0], 1+val[1])


n = int(input())
a = [[0] * n for i in range(n)]
for i in range(n - 2, -1, -1):
    cur = sum(a[i + 1]) + sum(a[k][n - 1] for k in range(i + 2, n))
    a[i][1] = cur + 1 - sum(a[k][1] for k in range(i + 1, n))
    for j in range(2, n):
        cur += a[i][j - 1] - a[i + 1][j - 2]
        a[i][j] = cur + 1 - sum(a[k][j] for k in range(i + 1, n)) - sum(a[i])
res = ''
for i in range(n):
    res += ' '.join(map(str, a[i])) + '\n'
print(res)
dp = [[0] * (n + 1) for i in range(n + 1)]
dp[n - 1][n] = 1
for i in range(n - 1, -1, -1):
    for j in range(n - 1, -1, -1):
        dp[i][j] = dp[i + 1][j] + dp[i][j + 1]
q = int(input())
for _ in range(q):
    k = int(input())
    ans = [(0, 0)]
    x, y = 0, 0
    for i in range(2 * n - 2):
        if y < n - 1 and k >= dp[x + 1][y]:
            k -= dp[x + 1][y]
            y += 1
        else:
            x += 1
        ans.append((x, y))
    res = ''
    for i in ans:
        res += str(i[0] + 1) + ' ' + str(i[1] + 1) + '\n'
    print(res)


from sys import stdout

n = int(input())

grid = [[0]*n for _ in range(n)]
ming = [[0]*n for _ in range(n)]
maxg = [[0]*n for _ in range(n)]

for i in range(n):
    # Diagonal i, counting from bottom right.
    for j in range(i + 1):
        # Filling grid[n - 1 - j][n - 1 - i + j]
        k, l = n - 1 - j, n - 1 - i + j
        if j == 0:
            grid[k][l] = 0
        elif j == 1:
            grid[k][l] = 1
        else:
            # Get the largest value that you can make from k+1, l-1,
            # and make sure that your lowest value is above that.
            largest_prev = sum(grid[k+1][l-1:]) + \
                    sum(grid[i][n-1] for i in range(k+2, n))
            my_add = sum(grid[i][l] for i in range(k+1, n))
            grid[k][l] = largest_prev - my_add + 1
        if k < n - 1 or l < n - 1:
            if k < n - 1:
                ming[k][l] = ming[k+1][l] + grid[k][l]
            else:
                ming[k][l] = ming[k][l+1] + grid[k][l]
            if l < n - 1:
                maxg[k][l] = maxg[k][l+1] + grid[k][l]
            else:
                maxg[k][l] = ming[k+1][l] + grid[k][l]

for i in reversed(range(n-1)):
    # Filling the diagonal grid[k][l] where k + l == i
    for l in range(i+1):
        k = i - l
        if l == 0:
            grid[k][l] = 0
        else:
            # Get the largest value that you can make from k+1, l-1,
            # and make sure that your lowest value is above that.
            largest_prev = sum(grid[k+1][l-1:]) + \
                    sum(grid[i][n-1] for i in range(k+2, n))
            my_add = sum(grid[i][l] for i in range(k+1, n))
            grid[k][l] = largest_prev - my_add + 1
        if k < n - 1 or l < n - 1:
            if k < n - 1:
                ming[k][l] = ming[k+1][l] + grid[k][l]
            else:
                ming[k][l] = ming[k][l+1] + grid[k][l]
            if l < n - 1:
                maxg[k][l] = maxg[k][l+1] + grid[k][l]
            else:
                maxg[k][l] = ming[k+1][l] + grid[k][l]

for i in range(n-1):
    for j in range(n-1):
        assert ming[i+1][j] <= maxg[i+1][j] < ming[i][j+1] <= maxg[i][j+1]

for row in grid:
    print(*row)
stdout.flush()

# print()
# for row in ming:
#     print(*row)
# print()
# for row in maxg:
#     print(*row)

q = int(input())
for _ in range(q):
    k = int(input())
    print(1, 1)
    i, j = 0, 0
    while i < n-1 and j < n-1:
        k -= grid[i][j]
        if k <= maxg[i+1][j]:
            i += 1
        else:
            j += 1
        print(i+1, j+1)
    while i < n - 1:
        i += 1
        print(i+1,j+1)
    while j < n - 1:
        j += 1
        print(i+1,j+1)
    stdout.flush()


import math
import sys
input=sys.stdin.readline
t=int(input())
mat=[[0 for _ in range(t)] for _ in range(t)]
count=0
dicta=dict()
for i in range(t):
  for j in range(t):
    if i&1:
      mat[i][j]=1<<(i+j+2)
      dicta[1<<count]=(i+j+2)
      count+=1
#print(mat)

for i in range(t):
  print(*mat[i],flush=True)
q=int(input())
for i in range(q):
  k=int(input())
  k=bin(k)[2:]
  k=[i for i in k]
  k.reverse()
  print(1,1)
  x,y=1,1
  for i in range(3,len(k)):
    if k[i]=='1' and k[i-1]=='0':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='1' and k[i-1]=='1':
      print(x,y+1,flush=True)
      y+=1
    elif k[i]=='0' and k[i-1]=='1':
      print(x+1,y,flush=True)
      x+=1
    else:
      print(x,y+1,flush=True)
      y+=1


import math
import sys
input=sys.stdin.readline
t=int(input())
mat=[[0 for _ in range(t)] for _ in range(t)]
count=0
dicta=dict()
for i in range(t):
  for j in range(t):
    mat[i][j]=1<<count
    dicta[1<<count]=(i+1,j+1)
    count+=1
for i in range(t):
  print(*mat[i],flush=True)
q=int(input())
for i in range(q):
  k=int(input())
  k=bin(k)[2:]
  k=[i for i in k]
  k.reverse()
  for i in range(len(k)):
    if k[i]=='1':
      pq=1<<i
      print(dicta[pq][0],dicta[pq][1],flush=True)


import math
import sys
input=sys.stdin.readline
t=int(input())
mat=[[0 for _ in range(t)] for _ in range(t)]
count=0
dicta=dict()
for i in range(t):
  for j in range(t):
    if i&1:
      mat[i][j]=1<<(i+j+2)
      dicta[1<<count]=(i+j+2)
      count+=1
#print(mat)

for i in range(t):
  print(*mat[i],flush=True)
q=int(input())
for i in range(q):
  k=int(input())
  k=bin(k)[2:]
  k=[i for i in k]
  k.reverse()
  print(1,1)
  x,y=1,1
  for i in range(2,len(k)):
    if k[i]=='1' and k[i-1]=='0':
      print(x+1,y,flush=True)
      x+=1
    else:
      print(x,y+1,flush=True)
      y+=1


import math
import sys
input=sys.stdin.readline
t=int(input())
mat=[[0 for _ in range(t)] for _ in range(t)]
count=0
maxa=0
for i in range(t):
  for j in range(t):
    if i&1:
      mat[i][j]=1<<(i+j+2)
      maxa=1<<(i+j+2)
#print(mat)

for i in range(t):
  print(*mat[i],flush=True)
q=int(input())
for i in range(q):
  k=int(input())
  k=bin(k)[2:]
  k=[i for i in k]
  k.reverse()
  print(1,1)
  pq=len(bin(maxa))-2
  if t&1:
    for i in range(pq-len(k)+1):
      k.append('0')
  x,y=1,1
  for i in range(3,len(k)):
    if k[i]=='1' and k[i-1]=='0':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='1' and k[i-1]=='1':
      print(x,y+1,flush=True)
      y+=1
    elif k[i]=='0' and k[i-1]=='1':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='0' and k[i-1]=='0':
      print(x,y+1,flush=True)
      y+=1


import math
import sys
input=sys.stdin.readline
t=int(input())
mat=[[0 for _ in range(t)] for _ in range(t)]
count=0
for i in range(t):
  for j in range(t):
    if i&1:
      mat[i][j]=1<<(i+j+2)
#print(mat)

for i in range(t):
  print(*mat[i],flush=True)
q=int(input())
for i in range(q):
  k=int(input())
  k=bin(k)[2:]
  k=[i for i in k]
  k.reverse()
  print(1,1)
  x,y=1,1
  for i in range(3,len(k)):
    if k[i]=='1' and k[i-1]=='0':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='1' and k[i-1]=='1':
      print(x,y+1,flush=True)
      y+=1
    elif k[i]=='0' and k[i-1]=='1':
      print(x+1,y,flush=True)
      x+=1
    else:
      print(x,y+1,flush=True)
      y+=1


import math
import sys
input=sys.stdin.readline
t=int(input())
mat=[[0 for _ in range(t)] for _ in range(t)]
count=0
for i in range(t):
  for j in range(t):
    if i&1:
      mat[i][j]=1<<(i+j+2)
#print(mat)

for i in range(t):
  print(*mat[i],flush=True)
q=int(input())
for i in range(q):
  k=int(input())
  k=bin(k)[2:]
  k=[i for i in k]
  k.reverse()
  print(1,1)
  x,y=1,1
  for i in range(3,len(k)):
    if k[i]=='1' and k[i-1]=='0':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='1' and k[i-1]=='1':
      print(x,y+1,flush=True)
      y+=1
    elif k[i]=='0' and k[i-1]=='1':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='0' and k[i-1]=='0':
      print(x,y+1,flush=True)
      y+=1
  if t&1:
    if x==t-1 and y==t-1:
      print(x+1,y,flush=True)
      x+=1
      print(x,y+1,flush=True)
      y+=1
    else:
      print(x+1,y,flush=True)


import math
import sys
input=sys.stdin.readline
t=int(input())
mat=[[0 for _ in range(t)] for _ in range(t)]
count=0
maxa=0
for i in range(t):
  for j in range(t):
    if i&1:
      mat[i][j]=1<<(i+j+2)
      maxa=1<<(i+j+2)
#print(mat)

for i in range(t):
  print(*mat[i],flush=True)
q=int(input())
for i in range(q):
  k=int(input())
  k=bin(k)[2:]
  k=[i for i in k]
  k.reverse()
  print(1,1)
  pq=len(bin(maxa))-2
  for i in range(pq-len(k)+1):
    k.append('0')
  x,y=1,1
  for i in range(3,len(k)):
    if k[i]=='1' and k[i-1]=='0':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='1' and k[i-1]=='1':
      print(x,y+1,flush=True)
      y+=1
    elif k[i]=='0' and k[i-1]=='1':
      print(x+1,y,flush=True)
      x+=1
    elif k[i]=='0' and k[i-1]=='0':
      print(x,y+1,flush=True)
      y+=1


from sys import stdout

n = int(input())

arr = [[0 for i in range(n)] for i in range(n)]

for i in range(0, n, 2):
    a0 = 2**(2*n-i)
    for j in range(n):
        arr[i][j] = a0
        a0 //= 2

for i in arr:
    print(' '.join(map(str, i)))

stdout.flush()

for t in range(int(input())):
    su = int(input()) - arr[0][0]
    print(1, 1)
    curr = [0, 0]
    while curr != [n-1, n-1]:
        if curr[0] == n-1:
            curr[1] += 1
        elif curr[1] == n-1:
            curr[0] += 1
        else:
            right = arr[curr[0]][curr[1]+1]
            down = arr[curr[0]+1][curr[1]]
            if right > 0 and su > right:
                curr[1] += 1
                su -= right
            elif down > 0 and su > down:
                curr[0] += 1
                su -= down
            elif right == 0: curr[1] += 1
            else: curr[0] += 1
        print(curr[0]+1, curr[1]+1)

    stdout.flush()


from sys import stdout

n = int(input())

arr = [[0 for i in range(n)] for i in range(n)]

for i in range(0, n, 2):
    a0 = 2**(2*n-i)
    for j in range(n):
        arr[i][j] = a0
        a0 //= 2

for i in arr:
    print(' '.join(map(str, i)))

stdout.flush()

for t in range(int(input())):
    su = int(input()) - arr[0][0]
    print(1, 1)
    curr = [0, 0]
    while curr != [n-1, n-1]:
     #   print(su)
        if curr[0] == n-1:
            curr[1] += 1
        elif curr[1] == n-1:
            curr[0] += 1
        else:
            right = arr[curr[0]][curr[1]+1]
            down = arr[curr[0]+1][curr[1]]
           # print(right, down, 'hi')
            if right > 0 and su >= right:
           #     print('r1')
                curr[1] += 1
                su -= right
            elif down > 0 and su >= down:
          #      print('d1')
                curr[0] += 1
                su -= down
            elif right == 0:
          #      print('r2')
                curr[1] += 1
            else:
          #      print('d2')
                curr[0] += 1
        print(curr[0]+1, curr[1]+1)

    stdout.flush()


from sys import stdout

n = int(input())

arr = [[0 for i in range(n)] for i in range(n)]

for i in range(0, n, 2):
    a0 = 2**(2*n-i)
    for j in range(n):
        arr[i][j] = a0
        a0 //= 2

for i in arr:
    print(' '.join(map(str, i)))

stdout.flush()

for t in range(int(input())):
    su = int(input()) - arr[0][0]
    print(1, 1)
    curr = [0, 0]
    while curr != [n-1, n-1]:
     #   print(su)
        if curr[0] == n-1:
            curr[1] += 1
        elif curr[1] == n-1:
            curr[0] += 1
        else:
            right = arr[curr[0]][curr[1]+1]
            down = arr[curr[0]+1][curr[1]]
       #     print(right, down, 'hi')
            if right > 0 and su > right:
        #        print('r1')
                curr[1] += 1
                su -= right
            elif down > 0 and su > down:
        #        print('d1')
                curr[0] += 1
                su -= down
            elif right == 0: curr[0] += 1
            else: curr[1] += 1
        print(curr[0]+1, curr[1]+1)

    stdout.flush()


import sys
input = sys.stdin.readline

n = int(input())
l = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(1,n):
        l[i][j] = j * 1 << (i + j - 1)
for thing in l:
    print(' '.join(map(str,thing)))

sys.stdout.flush()
q = int(input())

for _ in range(q):
    k = int(input())
    out = ['1 1']
    currX = 0
    currY = 0
    for mov in range(2 * n - 2):
        k -= currX
        if k & 1:
            currX += 1
            k -= 1
        else:
            currY += 1
        k //= 2
        out.append(str(currY + 1)+ ' ' + str(currX + 1))
    print('\n'.join(out))


import sys
input = sys.stdin.readline

n = int(input())
l = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(1,n):
        l[i][j] = j * 1 << (i + j - 1)
for thing in l:
    print(' '.join(map(str,thing)))

sys.stdout.flush()
q = int(input())

for _ in range(q):
    k = int(input())
    out = ['1 1']
    currX = 0
    currY = 0
    for mov in range(2 * n - 2):
        k -= currX
        if k & 1:
            currX += 1
            k -= 1
        else:
            currY += 1
        k //= 2
        out.append(str(currY + 1)+ ' ' + str(currX + 1))
    print('\n'.join(out))
    sys.stdout.flush()


from sys import stdin
from math import log2
input = stdin.readline
base = [0] + [2 ** x for x in range(0, 54)]
matrix = []
n = int(input())
zeros = [0 for x in range(n)]
for line in range(n):
	if line & 1 == 0:
		print(*base[line:line+n], flush=True)
		matrix.append(base[line:n + line])
	else:
		print(*zeros, flush=True)
		matrix.append(zeros)

for _ in range(int(input())):
	k = int(input())
	bit_k = bin(k)[2:][::-1] + '0' * 100

	i, j = 0, 0
	print(1, 1)
	while (i + j) < 2 * (n - 1):
		if i == n - 1:
			j += 1
			print(i + 1, j + 1, flush=True)
			continue
		if j == n - 1:
			i += 1
			print(i + 1, j + 1, flush=True)
			continue
		if matrix[i][j + 1] > matrix[i + 1][j]:
			if bit_k[int(log2(matrix[i][j + 1]))] == '1':
				j += 1
				print(i + 1, j + 1, flush=True)
			else:
				i += 1
				print(i + 1, j + 1, flush=True)
		else:
			if bit_k[int(log2(matrix[i + 1][j]))] == '0':
				j += 1
				print(i + 1, j + 1, flush=True)
			else:
				i += 1
				print(i + 1, j + 1, flush=True)


from sys import stdin
from math import log2
input = stdin.readline
base = [0] + [2 ** x for x in range(0, 54)]
matrix = []
n = int(input())
zeros = [0 for x in range(n)]
for line in range(n):
	if line & 1 == 0:
		print(*base[line:line+n], flush=True)
		matrix.append(base[line:n + line])
	else:
		print(*zeros, flush=True)
		matrix.append(zeros)

for _ in range(int(input())):
	k = int(input())
	bit_k = bin(k)[2:][::-1]
	i, j = 0, 0
	print(1, 1)
	while (i + j) < 2 * (n - 1):
		if i == n - 1:
			j += 1
			print(i + 1, j + 1, flush=True)
			continue
		if j == n - 1:
			i += 1
			print(i + 1, j + 1, flush=True)
			continue
		if matrix[i][j + 1] > matrix[i + 1][j]:
			if bit_k[int(log2(matrix[i][j + 1]))] == '1':
				j += 1
				print(i + 1, j + 1, flush=True)
			else:
				i += 1
				print(i + 1, j + 1, flush=True)
		else:
			if bit_k[int(log2(matrix[i + 1][j]))] == '0':
				j += 1
				print(i + 1, j + 1, flush=True)
			else:
				i += 1
				print(i + 1, j + 1, flush=True)


def log(x):
	for i in range(len(x)): print(*x[i],flush=True)
def splice(a,n):
	an=[]
	for i in range(n):
		an.append(a[i][0:n])
	return(an)
def get_mat(n):
	a=[]
	for i in range(n):
		a.append([None]*(2*n-i-1))
	for i in range(n):
		for j in range(len(a[i])):
			if(i==0):
				a[i][j]=0
			else:
				if(j==0):
					a[i][j]=1
					for k in range(i):
						a[i][j]+=a[k][0]
					a[i][j]+=a[i-1][1]
				else:
					a[i][j] = a[i][j-1]+a[i-1][j+1]

	return(splice(a,n))

n=int(input())
mat=get_mat(n)
maxSumTill=[[None]*n for i in range(n)]
for i in range(n):
	for j in range(n):
		if(j==0):
			maxSumTill[i][j]=0
			for k in range(i+1): maxSumTill[i][j]+=mat[k][j]
		else:
			maxSumTill[i][j]=maxSumTill[i][j-1]+mat[i][j]

log(mat)

q=int(input())
for q in range(q):
	total=int(input())
	i=n-1
	j=n-1
	path=[]
	while(len(path)<(2*n-1)):
		path.append((i,j))
		total-=mat[i][j]
		if(i==0 and j==0):
			break
		if(i==0):
			j-=1
		elif(j==0):
			i-=1
		elif(mat[i][j-1]>total):
			i-=1
		elif(maxSumTill[i-1][j]<total):
			j-=1
		else:
			assert(0)
	assert(total==0)
	path.reverse()
	for i in range(len(path)):
		print(path[i][0]+1,path[i][1]+1,flush=True)


def log(x):
	for i in range(len(x)): print(*x[i],flush=True)
def splice(a,n):
	an=[]
	for i in range(n):
		an.append(a[i][0:n])
	return(an)
def get_mat(n):
	a=[]
	for i in range(n):
		a.append([None]*(2*n-i-1))
	for i in range(n):
		for j in range(len(a[i])):
			if(i==0):
				a[i][j]=0
			else:
				if(j==0):
					a[i][j]=1
					for k in range(i):
						a[i][j]+=a[k][0]
					a[i][j]+=a[i-1][1]
				else:
					a[i][j] = a[i][j-1]+a[i-1][j+1]

	return(splice(a,n))

def get_mat(n):
	a=[]
	for i in range(n):
		a.append([None]*(2*n-i-1))
	for i in range(n):
		for j in range(len(a[i])):
			if(i==0):
				a[i][j]=0
			else:
				if(j==0):
					a[i][j]=1+a[i-1][j+1]
				else:
					a[i][j]=1
					for dj in range(j+2):
						a[i][j]+=a[i-1][dj]
					for di in range(i-1):
						a[i][j]+=a[di][0]
					for di in range(i):
						a[i][j]-=a[di][j]
	return(splice(a,n))

n=int(input())
mat=get_mat(n)
maxSumTill=[[None]*n for i in range(n)]
for i in range(n):
	for j in range(n):
		if(j==0):
			maxSumTill[i][j]=0
			for k in range(i+1): maxSumTill[i][j]+=mat[k][j]
		else:
			maxSumTill[i][j]=maxSumTill[i][j-1]+mat[i][j]

log(mat)

q=int(input())
for q in range(q):
	total=int(input())
	i=n-1
	j=n-1
	path=[]
	while(len(path)<(2*n-1)):
		path.append((i,j))
		total-=mat[i][j]
		if(i==0 and j==0):
			break
		if(i==0):
			j-=1
		elif(j==0):
			i-=1
		elif(maxSumTill[i-1][j]<total):
			j-=1
		else:
			i-=1

	assert(total==0)
	path.reverse()
	for i in range(len(path)):
		print(path[i][0]+1,path[i][1]+1,flush=True)


from collections import defaultdict as dd, deque as dq
import math, string

def getInts():
    return [int(s) for s in input().split()]

def getInt():
    return int(input())

def getStrs():
    return [s for s in input().split()]

def getStr():
    return input()

def listStr():
    return list(input())

MOD = 10**9+7

"""
1 1 1 1

4 4 4 4

16 16 16 16

64 64 64 64

"""
def printX(arg):
    print(*arg,flush=True)

def solve():
    N = getInt()
    grid = [[pow(2,i+j-1) for j in range(N)] for i in range(N)]
    for i in range(N):
        if i % 2 == 0:
            grid[i] = [0]*N
    for g in grid:
        printX(g)
    Q = getInt()
    for q in range(Q):
        K = getInt()
        path = [(1,1)]
        curr = [1,1]
        for j in range(2*N-2):
            if K % 2 == 1:
                if curr[0] % 2 == 1:
                    curr[0] += 1
                else:
                    curr[1] += 1
            else:
                if curr[0] % 2 == 1:
                    curr[1] += 1
                else:
                    curr[0] += 1
            path.append(tuple(curr))
            K >>= 1
        for a,b in path:
            print(a,b,flush=True)
    return

#for _ in range(getInt()):
solve()


"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""

import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(10 ** 6)
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+"\n")
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

def get_bin():
    for _ in range(64):
        if k & (1 << _):
            arr.append(1 << _)

n = int(input())
mat = [[0] * n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if i & 1:
            mat[i][j] = pow(2, i + j + 2)
    print(*mat[i], flush=True)
for _ in range(int(input())):
    k = int(input())
    arr = []
    get_bin()
    i, j = 0, 0
    first = 0
    while i < n and j < n and first < len(arr):
        while j < n:
            if mat[i+1][j] != arr[first]:
                print(i+1, j+1, flush=True)
                j += 1
            else:
                print(i+1, j+1, flush=True)
                print(i+2, j+1, flush=True)
                first += 1
                break
        i += 1
        j += 1
        while j < n and first < len(arr) and mat[i][j] == arr[first]:
            print(i+1, j+1, flush=True)
            first += 1
            j += 1
        j -= 1
        i += 1
    if n & 1:
        while j < n:
            print(i+1, j+1, flush=True)
            j += 1


import sys
input = sys.stdin.readline
n=int(input())
grid=[0]*n
for i in range(n):
    grid[i]=[0]*n
    for j in range(n):
        i1=i//2
        i1=2*i1
        grid[i][j]=2**(i1+j)
    print(*grid[i])
    sys.stdout.flush()
for f in range(int(input())):
    k=int(input())
    i=0
    j=0
    print(1,1)
    k-=grid[i][j]
    while i<n-1 or j<n-1:
        val=grid[i][j]
        k1=k//val
        if i%2==0:
            if k1%2==0:
                j+=1
            else:
                i+=1
        else:
            if k1%4==0:
                i+=1
            else:
                j+=1
        print(i+1,j+1)
        sys.stdout.flush()
        k-=grid[i][j]


I = lambda : list(map(int,input().split()))

n,=I()
l=[[0]*n for i in range(n)]
t=1;p=1
for i in range(1,n-1):
	l[0][i]=p
	l[i][n-1]=p
	p*=2
if n==2:
	l[0][1]+=1
else:
	l[1][n-1]+=1
for i in range(1,n):
	x=t
	for j in range(n-1):
		l[i][j]=l[i-1][j+1]+x
		x*=2
	t*=2
for i in l:
	print(*i)
q,=I()
for i in range(q):
	x,=I()
	f=format(x,'b')
	f='0'*(2*n-2-len(f))+f
	an=[];a,b=1,1
	pp=0
	for j in f[::-1]:
		an.append([a,b])
		pp+=l[a-1][b-1]
		if j=='1':
			b+=1
		else:
			a+=1
	an.append([a,b])
	for iu in an:
		print(*iu)


import sys
input = sys.stdin.buffer.readline
I = lambda : list(map(int,input().split()))

n,=I()
l=[[0]*n for i in range(n)]
t=1;p=1
for i in range(1,n-1):
	l[0][i]=p
	l[i][n-1]=p
	p*=2
if n==2:
	l[0][1]+=1
else:
	l[1][n-1]+=1
for i in range(1,n):
	x=t
	for j in range(n-1):
		l[i][j]=l[i-1][j+1]+x
		x*=2
	t*=2
for i in l:
	print(*i)
q,=I()
for i in range(q):
	x,=I()
	f=format(x,'b')
	f='0'*(2*n-2-len(f))+f
	an=[];a,b=1,1
	pp=0
	for j in f[::-1]:
		an.append([a,b])
		pp+=l[a-1][b-1]
		if j=='1':
			b+=1
		else:
			a+=1
	an.append([a,b])
	for iu in an:
		print(*iu)


from sys import stdout

n = int(input())
grid = [[0 for _ in range(n)] for _ in range(n)]
for x in range(n):
    for y in range(n):
        if x % 2 == 1:
            grid[x][y] = 1 << (x + y)
        print(grid[x][y], end=' ')
    print()
stdout.flush()
q = int(input())
for _ in range(q):
    sum = int(input())
    path, x, y = [(0, 0)], 0, 0
    while True:
        if x % 2 == 0:
            if x == n - 1 or sum & (1 << (x + y + 1)) == 0: y += 1
            else: x += 1
        else:
            if sum & (1 << (x + y + 1)) != 0: y += 1
            else: x += 1
        path.append((x, y))
        if x == n - 1 and y == n - 1: break
    for i in path:
        x, y = i
        print(x + 1, y + 1)
    stdout.flush()


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y

def calc(a, v):
    mxx = -1
    ans = 0
    for i in range(len(a)):
        if mxx == -1:mxx = a[i]
        ans += max(mxx, a[i]) - a[i]
        mxx = max(mxx, a[i])
        a[i] = mxx
    #print(ans, a)
    bb = 0
    for x in sorted(list(set(a)), reverse=True):
        ans += v - bb - x
        bb = v - x
    return ans

def find(s):
    start = 0
    n = len(s)
    if s[0] == s[-1]:
        start = n-1
        while start > 0 and s[start-1] == s[start]:
            start -= 1
    s += s
    cur = s[start]
    val = 0
    a=[]
    for i in range(start, start+n):
        if s[i] == cur:
            val += 1
        else:
            cur = s[i]
            a += [val]
            val = 1
    a += [val]
    #print(a,start)
    ans = 0
    for i in a:
        ans += i // 3 if i != n else (i+2)//3
    return ans

for _ in range(int(input()) if not True else 1):
    n = int(input())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    a = [2**26]
    while len(a) != n:
        a += [a[-1]*2]
    for i in range(n):
        print(*a,flush=True)
    for __ in range(int(input())):
        mx = 26+n-1
        target = int(input())
        x, y = n-1,n-1
        a = []
        while target:
            a += [[x, y]]
            target -= 2**mx
            #print(target)
            if target >= 2**mx:
                y -= 1
            else:
                x -= 1
                mx -= 1
        for i in range(len(a)-1, -1, -1):
            print(a[i][0]+1, a[i][1]+1, flush=True)


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y

def calc(a, v):
    mxx = -1
    ans = 0
    for i in range(len(a)):
        if mxx == -1:mxx = a[i]
        ans += max(mxx, a[i]) - a[i]
        mxx = max(mxx, a[i])
        a[i] = mxx
    #print(ans, a)
    bb = 0
    for x in sorted(list(set(a)), reverse=True):
        ans += v - bb - x
        bb = v - x
    return ans

def find(s):
    start = 0
    n = len(s)
    if s[0] == s[-1]:
        start = n-1
        while start > 0 and s[start-1] == s[start]:
            start -= 1
    s += s
    cur = s[start]
    val = 0
    a=[]
    for i in range(start, start+n):
        if s[i] == cur:
            val += 1
        else:
            cur = s[i]
            a += [val]
            val = 1
    a += [val]
    #print(a,start)
    ans = 0
    for i in a:
        ans += i // 3 if i != n else (i+2)//3
    return ans

for _ in range(int(input()) if not True else 1):
    n = int(input())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    a = [2**26]
    while len(a) != n:
        a += [a[-1]*2]
    for i in range(n):
        print(*a,flush=True)
    for __ in range(int(input())):
        mx = 26+n-1
        target = int(input())
        x, y = n-1,n-1
        a = []
        while target:
            a += [[x, y]]
            target -= 2**mx
            #print(target)
            if target >= 2**mx:
                y -= 1
            else:
                x -= 1
                mx -= 1
        for i in range(len(a)-1, -1, -1):
            print(a[i][0]+1, a[i][1]+1, flush=True)
        print("\n")


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y

def calc(a, v):
    mxx = -1
    ans = 0
    for i in range(len(a)):
        if mxx == -1:mxx = a[i]
        ans += max(mxx, a[i]) - a[i]
        mxx = max(mxx, a[i])
        a[i] = mxx
    #print(ans, a)
    bb = 0
    for x in sorted(list(set(a)), reverse=True):
        ans += v - bb - x
        bb = v - x
    return ans

def find(s):
    start = 0
    n = len(s)
    if s[0] == s[-1]:
        start = n-1
        while start > 0 and s[start-1] == s[start]:
            start -= 1
    s += s
    cur = s[start]
    val = 0
    a=[]
    for i in range(start, start+n):
        if s[i] == cur:
            val += 1
        else:
            cur = s[i]
            a += [val]
            val = 1
    a += [val]
    #print(a,start)
    ans = 0
    for i in a:
        ans += i // 3 if i != n else (i+2)//3
    return ans

for _ in range(int(input()) if not True else 1):
    n = int(input())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    a = [[0]*n for __ in range(n)]
    for i in range(n):
        for j in range(n):
            if i % 2:a[i][j] = 2**(i+j+2)
    for i in range(n):
        print(*a[i],flush=True)
    for __ in range(int(input())):
        target = int(input())
        b = binary(target, 52)[2:][::-1]
        x, y = 0,0
        a = [[x, y]]
        while min(x,y) != n-1:
            if x == n-1:
                y += 1
                a += [[x, y]]
                continue
            if y == n-1:
                x += 1
                a += [[x, y]]
                continue
            if x % 2 == 1:
                if int(b[x+y+3]):
                    y += 1
                    a += [[x, y]]
                else:
                    x += 1
                    a += [[x, y]]
            else:
                if int(b[x+y+3]):
                    x += 1
                    a += [[x, y]]
                else:
                    y += 1
                    a += [[x, y]]
        for i in a:
            print(i[0]+1,i[1]+1,flush=True)


import sys,math as mt
import heapq as hp
import collections as cc
import bisect as bi
import math as mt
import itertools as it
I=lambda:list(map(int,input().split()))
n=int(input())
ar=[[0]*n for i in range(n)]
for i in range(n):
	for j in range(n):
		temp=i+j-1
		tf=int(j%2!=0)
		if tf:
			ar[i][j]=1<<temp
for i in ar:
	print(*i)
#print(flush=True)
tc=int(input())
while tc:
	tc-=1
	q,=I()
	x=0;y=0
	while x!=n and y!=n:
		print(x+1,y+1)
		if q&(1<<x+y)!=0:
			if(y%2==0):
				y+=1
			else:
				x+=1
		else:
			if y%2==0:
				x+=1
			else:
				y+=1


from sys import stdout
n=int(input())
for i in range(n):
    s=[]
    for j in range(n):
        s.append(2**(i+j)*(i%2))
    print(*s)
    stdout.flush()
q=int(input())
for _ in range(q):
    count=int(input())
    pre=0
    count//=2
    print(1,1)
    a=b=1
    stdout.flush()
    for _ in range(n+n-2):
        now=count%2
        count//=2
        bit=now
        if pre==1:
            bit=now^1
        b+=bit^1
        a+=bit
        print(a,b)
        stdout.flush()
        pre=now


import sys

n = int(input())

def diag(i, j):
  return i+j

def printBin(n):
  print(str(bin(n))[2:])

for i in range(n):
  if i%2 == 0:
    print('0 '*n)
  else:
    tp = ''
    for j in range(n):
      d = diag(i, j)
      tp += str(1<<d) + " "
      # print(str(bin(1<<d))[2:], end = ' ')
    # print()
    print(tp)

sys.stdout.flush()

q = int(input())

for _ in range(q):
  k = int(input())
  pos = [0,0]
  # printBin(k)
  while pos[0] < n-1 or pos[1] < n-1:
    print(pos[0]+1,pos[1]+1,sep=' ')
    if pos[0] == n-1:
      pos[1] += 1
    elif pos[1] == n-1:
      pos[0] += 1
    else:
      d = pos[0]+pos[1]+1
      # print('d', d)
      # print('k & (1<<d)', k & (1<<d))
      if (k & (1<<d)) != 0:
        if pos[0]%2 == 0:
          pos[0] += 1
        else:
          pos[1] += 1
      else:
        if pos[0]%2 == 0:
          pos[1] += 1
        else:
          pos[0] += 1
  print(n, n, sep=' ')


import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7

n=int(data())
mat=[]
for i in range(n):
    if i%2==0:
        mat.append([0]*n)
    else:
        mat.append([])
        for j in range(n):
            mat[-1].append(1<<(i+j))
for i in range(n):
    print(*mat[i],flush=True)
q=int(data())
for i in range(q):
    k=int(data())
    l=['1 1']
    r=c=0
    for i in range(2*n-2):
        if 1<<(r+c+1) & k:
            c += r % 2 != 0
            r += r % 2 == 0
        else:
            c += r % 2 == 0
            r += r % 2 != 0
        l.append(str(r+1)+' '+str(c+1))
    print('\n'.join(l),flush=True)


import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7

n=int(data())
l=list(range(n))
mat=[]
for i in range(n):
    print(*l,flush=True)
    mat.append(l)
s=0
l=[]
for i in range(n):
    l.append([1,i+1])
    s+=i
for i in range(1,n):
    l.append([i+1,n])
    s+=n-1
q=int(data())
for i in range(q):
    k=int(data())
    s1=s
    ind=-1
    while s>k:
        ind+=1
        j=n-1
        while s>k and j>0:
            s-=1
            l[ind+j][0]+=1
            l[ind+j][1]-=1
            j-=1
    for i in range(len(l)):
        l[i]=str(l[i][0])+' '+str(l[i][1])
    print('\n'.join(l),flush=True)


import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7

n=int(data())
l=list(range(1,n+1))
mat=[]
for i in range(n):
    print(*l,flush=True)
    mat.append(l)
s=0
l=[]
for i in range(n):
    l.append([1,i+1])
    s+=i+1
for i in range(1,n):
    l.append([i+1,n])
    s+=n
q=int(data())
for i in range(q):
    k=int(data())
    s1=s
    ind=-1
    while s>k:
        ind+=1
        j=n-1
        while s>k and j>0:
            s-=1
            l[ind+j][0]+=1
            l[ind+j][1]-=1
            j-=1
    for i in range(len(l)):
        l[i]=str(l[i][0])+' '+str(l[i][1])
    print('\n'.join(l),flush=True)


n=int(input())
arr=[[0 for i in range(n)]for i in range(n)]
for i in range(n):
    if(i%2==0):
        for j in range(n):
            arr[i][j]=1<<(2*n-i-j)
            print(1<<(2*n-i-j),end=" ")
        print()
    else:
        for j in range(n):
            print(0,end=" ")
        print()
q=int(input())
for i in range(q):
    z=int(input())
    x=0
    y=0
    curr=0
    while(z>=0):
        z-=(arr[x][y])
        print(x+1,y+1)
        if(x+1==n and y+1==n):
            break
        curr+=1
        #stdout.flush()
        if(x==n-1):
            y=y+1
            continue
        elif(y==n-1):
            x=x+1
            continue
        to=max(arr[x+1][y],arr[x][y+1])
        if(to==arr[x+1][y] and z>=arr[x+1][y]):
            x+=1
            continue
        elif(to==arr[x+1][y]):
            y=y+1
            continue
        elif(to==arr[x][y+1] and z>=arr[x][y+1]):
            y+=1
            continue
        else:
            x+=1
            continue


n=int(input())
arr=[[0 for i in range(n)]for i in range(n)]
for i in range(n):
    if(i%2==0):
        for j in range(n):
            arr[i][j]=1<<(2*n-i-j)
            print(1<<(2*n-i-j),end=" ")
        print()
    else:
        for j in range(n):
            print(0,end=" ")
        print()
q=int(input())
for i in range(q):
    z=int(input())
    x=0
    y=0
    curr=0
    while(z>0):
        z-=(arr[x][y])
        print(x+1,y+1)
        curr+=1
        stdout.flush()
        if(z==0):
            break
        if(x==n-1):
            y=y+1
            continue
        elif(y==n-1):
            x=x+1
            continue
        to=max(arr[x+1][y],arr[x][y+1])
        if(to==arr[x+1][y] and z>=arr[x+1][y]):
            x+=1
            continue
        elif(to==arr[x+1][y]):
            y=y+1
            continue
        elif(to==arr[x][y+1] and z>=arr[x][y+1]):
            y+=1
            continue
        else:
            x+=1
            continue
    if(curr<2*n-1):
        print(n,n)
        stdout.flush()


import java.util.*;
import java.io.*;
import java.math.*;
public class loser
{
    static class InputReader {
        public BufferedReader br;
        public StringTokenizer token;
        public InputReader(InputStream stream)
        {
            br=new BufferedReader(new InputStreamReader(stream),32768);
            token=null;
        }

        public String next()
        {
            while(token==null || !token.hasMoreTokens())
            {
                try
                {
                    token=new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }
    }
    static class card{
        long a;
        int i;
        public card(long a,int i)
        {
            this.a=a;
            this.i=i;
        }
    }
    static class sort implements Comparator<pair>
    {
        public int compare(pair o1,pair o2)
        {
            return (o1.b-o2.b)*-1;
        }
    }
    /*static class sort1 implements Comparator<pair>
    {
    public int compare(pair o1,pair o2)
    {
    if(o1.a!=o2.a)
    return (int)(o1.a-o2.a);
    else
    return (o1.b-o2.b);
    }
    }*/
    static class extra{
        static void shuffle(long a[])
        {
            List<Long> l=new ArrayList<>();
            for(int i=0;i<a.length;i++)
                l.add(a[i]);
            Collections.shuffle(l);
            for(int i=0;i<a.length;i++)
                a[i]=l.get(i);
        }

        static long gcd(long a,long b)
        {
            if(b==0)
                return a;
            else
                return gcd(b,a%b);
        }

        static boolean valid(int i,int j,int r,int c)
        {
            if(i>=0 && i<r && j>=0 && j<c)
                return true;
            else
                return false;
        }
        static boolean v[]=new boolean[100001];
        static List<Integer> l=new ArrayList<>();
        static int t;
        static void seive()
        {
            for(int i=2;i<100001;i++)
            {
                if(!v[i])
                {
                    t++;
                    l.add(i);
                    for(int j=2*i;j<100001;j+=i)
                        v[j]=true;
                }
            }
        }

        static int binary(int a[],int val,int n)
        {
            int mid=0,l=0,r=n-1,ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(a[mid]==val)
                {
                    r=mid-1;
                    ans=mid;
                }
                else if(a[mid]>val)
                    r=mid-1;
                else
                {
                    l=mid+1;
                    ans=l;
                }
            }
            return (ans+1);
        }
    }
    static class pair{
        int a;
        int b;
        public pair(int a,int n)
        {
            this.a=a;
            this.b=n;
        }
    }
    static InputReader sc=new InputReader(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    public  static void main(String[] args)
    {
        solver s=new solver();
        int t=1;
        while(t>0)
        {
            s.solve();
            t--;
        }
    }
    static class solver
    {
        void solve()
        {
            int n=sc.nextInt();
            int m=sc.nextInt();
            char c[]=sc.next().toCharArray();
            int cop[]=new int[n+1];
            int t=0;
            for(int i=1;i<n;i++)
            {
                if(c[i]=='.' && c[i-1]=='.')
                {
                    cop[i]=1;
                    t++;
                }
            }
            for(int i=0;i<m;i++)
            {
                int x=sc.nextInt();
                char ch=sc.next().charAt(0);
                int ans=t;
                c[x-1]=ch;
                if(cop[x-1]==1 && cop[x]==1)
                {
                    ans-=2;
                    cop[x]=0;
                }
                else if(cop[x-1]==1 && cop[x]==0)
                ans-=1;
                else if(cop[x-1]==0 && cop[x]==1)
                {
                    ans-=1;
                    cop[x]=0;
                }
                System.out.println(ans);
                t=ans;
                cop[x-1]=0;
            }
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class loser
{
    static class InputReader {
        public BufferedReader br;
        public StringTokenizer token;
        public InputReader(InputStream stream)
        {
            br=new BufferedReader(new InputStreamReader(stream),32768);
            token=null;
        }

        public String next()
        {
            while(token==null || !token.hasMoreTokens())
            {
                try
                {
                    token=new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }
    }
    static class card{
        long a;
        int i;
        public card(long a,int i)
        {
            this.a=a;
            this.i=i;
        }
    }
    static class sort implements Comparator<pair>
    {
        public int compare(pair o1,pair o2)
        {
            return (o1.b-o2.b)*-1;
        }
    }
    /*static class sort1 implements Comparator<pair>
    {
    public int compare(pair o1,pair o2)
    {
    if(o1.a!=o2.a)
    return (int)(o1.a-o2.a);
    else
    return (o1.b-o2.b);
    }
    }*/
    static class extra{
        static void shuffle(long a[])
        {
            List<Long> l=new ArrayList<>();
            for(int i=0;i<a.length;i++)
                l.add(a[i]);
            Collections.shuffle(l);
            for(int i=0;i<a.length;i++)
                a[i]=l.get(i);
        }

        static long gcd(long a,long b)
        {
            if(b==0)
                return a;
            else
                return gcd(b,a%b);
        }

        static boolean valid(int i,int j,int r,int c)
        {
            if(i>=0 && i<r && j>=0 && j<c)
                return true;
            else
                return false;
        }
        static boolean v[]=new boolean[100001];
        static List<Integer> l=new ArrayList<>();
        static int t;
        static void seive()
        {
            for(int i=2;i<100001;i++)
            {
                if(!v[i])
                {
                    t++;
                    l.add(i);
                    for(int j=2*i;j<100001;j+=i)
                        v[j]=true;
                }
            }
        }

        static int binary(int a[],int val,int n)
        {
            int mid=0,l=0,r=n-1,ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(a[mid]==val)
                {
                    r=mid-1;
                    ans=mid;
                }
                else if(a[mid]>val)
                    r=mid-1;
                else
                {
                    l=mid+1;
                    ans=l;
                }
            }
            return (ans+1);
        }
    }
    static class pair{
        int a;
        int b;
        public pair(int a,int n)
        {
            this.a=a;
            this.b=n;
        }
    }
    static InputReader sc=new InputReader(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    public  static void main(String[] args)
    {
        solver s=new solver();
        int t=1;
        while(t>0)
        {
            s.solve();
            t--;
        }
    }
    static class solver
    {
        void solve()
        {
            int n=sc.nextInt();
            int m=sc.nextInt();
            char c[]=(sc.next()+" ").toCharArray();
            int cop[]=new int[n+1];
            int t=0;
            for(int i=1;i<n;i++)
            {
                if(c[i]=='.' && c[i-1]=='.')
                {
                    cop[i]=1;
                    t++;
                }
            }
            for(int i=0;i<m;i++)
            {
                int x=sc.nextInt();
                char ch=sc.next().charAt(0);
                if(ch!='.')
                {
                    int ans=t;
                    c[x-1]=ch;
                    if(cop[x-1]==1 && cop[x]==1)
                    {
                        ans-=2;
                        cop[x]=0;
                    }
                    else if(cop[x-1]==1 && cop[x]==0)
                        ans-=1;
                    else if(cop[x-1]==0 && cop[x]==1)
                    {
                        ans-=1;
                        cop[x]=0;
                    }
                    System.out.println(ans);
                    t=ans;
                    cop[x-1]=0;
                }
                else
                {
                    if(x!=1 && c[x-2]=='.' && c[x]=='.' && c[x-1]!='.')
                    {
                        t+=2;
                        cop[x-1]=1;
                        cop[x]=1;
                    }
                    else if(c[x-1]!='.' && c[x]=='.')
                    {
                        t+=1;
                        cop[x]=1;
                    }
                    else if(x!=1 && c[x-1]!='.' && c[x-2]=='.')
                    {
                        t+=1;
                        cop[x-1]=1;
                    }
                    c[x-1]=ch;
                    System.out.println(t);
                }
            }
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class k
{
    static class InputReader {
        public BufferedReader br;
        public StringTokenizer token;
        public InputReader(InputStream stream)
        {
            br=new BufferedReader(new InputStreamReader(stream),32768);
            token=null;
        }

        public String next()
        {
            while(token==null || !token.hasMoreTokens())
            {
                try
                {
                    token=new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }
    }
    static class card{
        long a;
        int i;
        public card(long a,int i)
        {
            this.a=a;
            this.i=i;
        }
    }
    static class sort implements Comparator<pair>
    {
        public int compare(pair o1,pair o2)
        {
            return (o1.b-o2.b)*-1;
        }
    }
    /*static class sort1 implements Comparator<pair>
    {
    public int compare(pair o1,pair o2)
    {
    if(o1.a!=o2.a)
    return (int)(o1.a-o2.a);
    else
    return (o1.b-o2.b);
    }
    }*/
    static class extra{
        static void shuffle(long a[])
        {
            List<Long> l=new ArrayList<>();
            for(int i=0;i<a.length;i++)
                l.add(a[i]);
            Collections.shuffle(l);
            for(int i=0;i<a.length;i++)
                a[i]=l.get(i);
        }

        static long gcd(long a,long b)
        {
            if(b==0)
                return a;
            else
                return gcd(b,a%b);
        }

        static boolean valid(int i,int j,int r,int c)
        {
            if(i>=0 && i<r && j>=0 && j<c)
                return true;
            else
                return false;
        }
        static boolean v[]=new boolean[100001];
        static List<Integer> l=new ArrayList<>();
        static int t;
        static void seive()
        {
            for(int i=2;i<100001;i++)
            {
                if(!v[i])
                {
                    t++;
                    l.add(i);
                    for(int j=2*i;j<100001;j+=i)
                        v[j]=true;
                }
            }
        }

        static int binary(int a[],int val,int n)
        {
            int mid=0,l=0,r=n-1,ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(a[mid]==val)
                {
                    r=mid-1;
                    ans=mid;
                }
                else if(a[mid]>val)
                    r=mid-1;
                else
                {
                    l=mid+1;
                    ans=l;
                }
            }
            return (ans+1);
        }
    }
    static class pair{
        int a;
        int b;
        public pair(int a,int n)
        {
            this.a=a;
            this.b=n;
        }
    }
    static InputReader sc=new InputReader(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    public  static void main(String[] args)
    {
        solver s=new solver();
        int t=1;
        while(t>0)
        {
            s.solve();
            t--;
        }
        out.close();
    }
    static class solver
    {
        void solve()
        {
            int n=sc.nextInt();
            int m=sc.nextInt();
            char c[]=(sc.next()+" ").toCharArray();
            int cop[]=new int[n+1];
            int t=0;
            for(int i=1;i<n;i++)
            {
                if(c[i]=='.' && c[i-1]=='.')
                {
                    cop[i]=1;
                    t++;
                }
            }
            for(int i=0;i<m;i++)
            {
                int x=sc.nextInt();
                char ch=sc.next().charAt(0);
                if(ch!='.')
                {
                    c[x-1]=ch;
                    if(cop[x-1]==1 && cop[x]==1)
                    {
                        t-=2;
                        cop[x]=0;
                    }
                    else if(cop[x-1]==1 && cop[x]==0)
                        t-=1;
                    else if(cop[x-1]==0 && cop[x]==1)
                    {
                       t-=1;
                        cop[x]=0;
                    }
                    cop[x-1]=0;
                }
                else
                {
                    if(x!=1 && c[x-2]=='.' && c[x]=='.' && c[x-1]!='.')
                    {
                        t+=2;
                        cop[x-1]=1;
                        cop[x]=1;
                    }
                    else if(c[x-1]!='.' && c[x]=='.')
                    {
                        t+=1;
                        cop[x]=1;
                    }
                    else if(x!=1 && c[x-1]!='.' && c[x-2]=='.')
                    {
                        t+=1;
                        cop[x-1]=1;
                    }
                    c[x-1]=ch;
                }
                out.println(t);
            }
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class loser
{
    static class InputReader {
        public BufferedReader br;
        public StringTokenizer token;
        public InputReader(InputStream stream)
        {
            br=new BufferedReader(new InputStreamReader(stream),32768);
            token=null;
        }

        public String next()
        {
            while(token==null || !token.hasMoreTokens())
            {
                try
                {
                    token=new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }
    }
    static class card{
        long a;
        int i;
        public card(long a,int i)
        {
            this.a=a;
            this.i=i;
        }
    }
    static class sort implements Comparator<pair>
    {
        public int compare(pair o1,pair o2)
        {
            return (o1.b-o2.b)*-1;
        }
    }
    /*static class sort1 implements Comparator<pair>
    {
    public int compare(pair o1,pair o2)
    {
    if(o1.a!=o2.a)
    return (int)(o1.a-o2.a);
    else
    return (o1.b-o2.b);
    }
    }*/
    static class extra{
        static void shuffle(long a[])
        {
            List<Long> l=new ArrayList<>();
            for(int i=0;i<a.length;i++)
                l.add(a[i]);
            Collections.shuffle(l);
            for(int i=0;i<a.length;i++)
                a[i]=l.get(i);
        }

        static long gcd(long a,long b)
        {
            if(b==0)
                return a;
            else
                return gcd(b,a%b);
        }

        static boolean valid(int i,int j,int r,int c)
        {
            if(i>=0 && i<r && j>=0 && j<c)
                return true;
            else
                return false;
        }
        static boolean v[]=new boolean[100001];
        static List<Integer> l=new ArrayList<>();
        static int t;
        static void seive()
        {
            for(int i=2;i<100001;i++)
            {
                if(!v[i])
                {
                    t++;
                    l.add(i);
                    for(int j=2*i;j<100001;j+=i)
                        v[j]=true;
                }
            }
        }

        static int binary(int a[],int val,int n)
        {
            int mid=0,l=0,r=n-1,ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(a[mid]==val)
                {
                    r=mid-1;
                    ans=mid;
                }
                else if(a[mid]>val)
                    r=mid-1;
                else
                {
                    l=mid+1;
                    ans=l;
                }
            }
            return (ans+1);
        }
    }
    static class pair{
        int a;
        int b;
        public pair(int a,int n)
        {
            this.a=a;
            this.b=n;
        }
    }
    static InputReader sc=new InputReader(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    public  static void main(String[] args)
    {
        solver s=new solver();
        int t=1;
        while(t>0)
        {
            s.solve();
            t--;
        }
    }
    static class solver
    {
        void solve()
        {
            int n=sc.nextInt();
            int m=sc.nextInt();
            char c[]=(sc.next()+" ").toCharArray();
            int t=0;
            for(int i=1;i<n;i++)
            {
                if(c[i]=='.' && c[i-1]=='.')
                {
                    t++;
                }
            }
            for(int i=0;i<m;i++)
            {
                int x=sc.nextInt()-1;
                char ch=sc.next().charAt(0);
                if(ch!='.')
                {
                    if(c[x]=='.' && c[x+1]=='.')
                    t--;
                    if(x!=0 && c[x-1]=='.' && c[x]=='.')
                    t--;
                    c[x]=ch;
                }
                else if(c[x]!='.')
                {
                    c[x]=ch;
                    if(x!=0 && c[x-1]=='.' && c[x]=='.')
                    t++;
                     if(c[x+1]=='.'&& c[x]=='.')
                    t++;
                }
                System.out.println(t);
            }
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class k
{
    static class InputReader {
        public BufferedReader br;
        public StringTokenizer token;
        public InputReader(InputStream stream)
        {
            br=new BufferedReader(new InputStreamReader(stream),32768);
            token=null;
        }

        public String next()
        {
            while(token==null || !token.hasMoreTokens())
            {
                try
                {
                    token=new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }
    }
    static class card{
        long a;
        int i;
        public card(long a,int i)
        {
            this.a=a;
            this.i=i;
        }
    }
    static class sort implements Comparator<pair>
    {
        public int compare(pair o1,pair o2)
        {
            return (o1.b-o2.b)*-1;
        }
    }
    /*static class sort1 implements Comparator<pair>
    {
    public int compare(pair o1,pair o2)
    {
    if(o1.a!=o2.a)
    return (int)(o1.a-o2.a);
    else
    return (o1.b-o2.b);
    }
    }*/
    static class extra{
        static void shuffle(long a[])
        {
            List<Long> l=new ArrayList<>();
            for(int i=0;i<a.length;i++)
                l.add(a[i]);
            Collections.shuffle(l);
            for(int i=0;i<a.length;i++)
                a[i]=l.get(i);
        }

        static long gcd(long a,long b)
        {
            if(b==0)
                return a;
            else
                return gcd(b,a%b);
        }

        static boolean valid(int i,int j,int r,int c)
        {
            if(i>=0 && i<r && j>=0 && j<c)
                return true;
            else
                return false;
        }
        static boolean v[]=new boolean[100001];
        static List<Integer> l=new ArrayList<>();
        static int t;
        static void seive()
        {
            for(int i=2;i<100001;i++)
            {
                if(!v[i])
                {
                    t++;
                    l.add(i);
                    for(int j=2*i;j<100001;j+=i)
                        v[j]=true;
                }
            }
        }

        static int binary(int a[],int val,int n)
        {
            int mid=0,l=0,r=n-1,ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(a[mid]==val)
                {
                    r=mid-1;
                    ans=mid;
                }
                else if(a[mid]>val)
                    r=mid-1;
                else
                {
                    l=mid+1;
                    ans=l;
                }
            }
            return (ans+1);
        }
    }
    static class pair{
        int a;
        int b;
        public pair(int a,int n)
        {
            this.a=a;
            this.b=n;
        }
    }
    static InputReader sc=new InputReader(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    public  static void main(String[] args)
    {
        solver s=new solver();
        int t=1;
        while(t>0)
        {
            s.solve();
            t--;
        }
        out.close();
    }
    static class solver
    {
        void solve()
        {
            int n=sc.nextInt();
            int m=sc.nextInt();
            char c[]=(sc.next()+" ").toCharArray();
            int cop[]=new int[n+1];
            int t=0;
            for(int i=1;i<n;i++)
            {
                if(c[i]=='.' && c[i-1]=='.')
                {
                    cop[i]=1;
                    t++;
                }
            }
            for(int i=0;i<m;i++)
            {
                int x=sc.nextInt();
                char ch=sc.next().charAt(0);
                if(ch!='.')
                {
                    int ans=t;
                    c[x-1]=ch;
                    if(cop[x-1]==1 && cop[x]==1)
                    {
                        ans-=2;
                        cop[x]=0;
                    }
                    else if(cop[x-1]==1 && cop[x]==0)
                        ans-=1;
                    else if(cop[x-1]==0 && cop[x]==1)
                    {
                        ans-=1;
                        cop[x]=0;
                    }
                    System.out.println(ans);
                    t=ans;
                    cop[x-1]=0;
                }
                else
                {
                    if(x!=1 && c[x-2]=='.' && c[x]=='.' && c[x-1]!='.')
                    {
                        t+=2;
                        cop[x-1]=1;
                        cop[x]=1;
                    }
                    else if(c[x-1]!='.' && c[x]=='.')
                    {
                        t+=1;
                        cop[x]=1;
                    }
                    else if(x!=1 && c[x-1]!='.' && c[x-2]=='.')
                    {
                        t+=1;
                        cop[x-1]=1;
                    }
                    c[x-1]=ch;
                    out.println(t);
                }
            }
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
public class loser
{
    static class InputReader {
        public BufferedReader br;
        public StringTokenizer token;
        public InputReader(InputStream stream)
        {
            br=new BufferedReader(new InputStreamReader(stream),32768);
            token=null;
        }

        public String next()
        {
            while(token==null || !token.hasMoreTokens())
            {
                try
                {
                    token=new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }
    }
    static class card{
        long a;
        int i;
        public card(long a,int i)
        {
            this.a=a;
            this.i=i;
        }
    }
    static class sort implements Comparator<pair>
    {
        public int compare(pair o1,pair o2)
        {
            return (o1.b-o2.b)*-1;
        }
    }
    /*static class sort1 implements Comparator<pair>
    {
    public int compare(pair o1,pair o2)
    {
    if(o1.a!=o2.a)
    return (int)(o1.a-o2.a);
    else
    return (o1.b-o2.b);
    }
    }*/
    static class extra{
        static void shuffle(long a[])
        {
            List<Long> l=new ArrayList<>();
            for(int i=0;i<a.length;i++)
                l.add(a[i]);
            Collections.shuffle(l);
            for(int i=0;i<a.length;i++)
                a[i]=l.get(i);
        }

        static long gcd(long a,long b)
        {
            if(b==0)
                return a;
            else
                return gcd(b,a%b);
        }

        static boolean valid(int i,int j,int r,int c)
        {
            if(i>=0 && i<r && j>=0 && j<c)
                return true;
            else
                return false;
        }
        static boolean v[]=new boolean[100001];
        static List<Integer> l=new ArrayList<>();
        static int t;
        static void seive()
        {
            for(int i=2;i<100001;i++)
            {
                if(!v[i])
                {
                    t++;
                    l.add(i);
                    for(int j=2*i;j<100001;j+=i)
                        v[j]=true;
                }
            }
        }

        static int binary(int a[],int val,int n)
        {
            int mid=0,l=0,r=n-1,ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(a[mid]==val)
                {
                    r=mid-1;
                    ans=mid;
                }
                else if(a[mid]>val)
                    r=mid-1;
                else
                {
                    l=mid+1;
                    ans=l;
                }
            }
            return (ans+1);
        }
    }
    static class pair{
        int a;
        int b;
        public pair(int a,int n)
        {
            this.a=a;
            this.b=n;
        }
    }
    static InputReader sc=new InputReader(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    public  static void main(String[] args)
    {
        solver s=new solver();
        int t=1;
        while(t>0)
        {
            s.solve();
            t--;
        }
    }
    static class solver
    {
        void solve()
        {
            int n=sc.nextInt();
            int m=sc.nextInt();
            char c[]=(sc.next()+" ").toCharArray();
            int t=0;
            for(int i=1;i<n;i++)
            {
                if(c[i]=='.' && c[i-1]=='.')
                {
                    t++;
                }
            }
            for(int i=0;i<m;i++)
            {
                int x=sc.nextInt()-1;
                char ch=sc.next().charAt(0);
                if(ch!='.')
                {
                    if(c[x]=='.' && c[x+1]=='.')
                    t--;
                    if(x!=0 && c[x-1]=='.' && c[x]=='.')
                    t--;
                    c[x]=ch;
                    System.out.println(t);
                }
                else if(c[x]!='.')
                {
                    c[x]=ch;
                    if(x!=0 && c[x-1]=='.' && c[x]=='.')
                    t++;
                     if(c[x+1]=='.'&& c[x]=='.')
                    t++;
                    System.out.println(t);
                }
            }
        }
    }
}


import java.io.*;
import java.util.*;
public class k
{
    public static void main(String args[])throws IOException
    {
        Scanner sc=new Scanner(System.in);
        BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
        int n=sc.nextInt();
        int m=sc.nextInt();
        char c[]=(sc.next()+" ").toCharArray();
        int t=0;
        for(int i=1;i<n;i++)
        {
            if(c[i]=='.' && c[i-1]=='.')
            {
                t++;
            }
        }
        for(int i=0;i<m;i++)
        {
            int x=sc.nextInt()-1;
            char ch=sc.next().charAt(0);
            if(ch!='.')
            {
                if(c[x]=='.' && c[x+1]=='.')
                    t--;
                if(x!=0 && c[x-1]=='.' && c[x]=='.')
                    t--;
                c[x]=ch;
            }
            else if(c[x]!='.')
            {
                c[x]=ch;
                if(x!=0 && c[x-1]=='.' && c[x]=='.')
                    t++;
                if(c[x+1]=='.'&& c[x]=='.')
                    t++;
            }
            log.write(t);
        }
        log.flush();
    }
}


import java.util.Scanner;

/**
 *
 * @author Rishabh
 */
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();int m=s.nextInt();
        String inp=s.next();
        char ar[]=inp.toCharArray();
        int count=0,c=0,segcount=0;
        for(int i=0;i<n;i++){
            if(inp.charAt(i)!='.'){if(c>0){segcount++;c=0;}}
            if(inp.charAt(i)=='.'){count++;c++;}}if(c>0)segcount++;
           // System.out.println(count+" "+segcount);
        for(int i=0;i<m;i++){
        int x=s.nextInt()-1;char z=s.next().charAt(0);

       // int count=0;
     if(z=='.'){
     if(ar[x]!='.')count++;
     if(ar[x]!='.'){
     if(x==0){if(ar[x+1]!='.')segcount++;}
     else if(x==n-1){if(ar[x-1]!='.')segcount++;}
     else
     {
     if(ar[x-1]=='.' && ar[x+1]=='.')segcount--;
     else if(ar[x-1]!='.' && ar[x+1]!='.')segcount++;
     }
     }

     }
     else{
     if(ar[x]=='.')count--;
     if(ar[x]=='.'){
     if(x==0){if(ar[x+1]!='.')segcount--;}
     else if(x==n-1){if(ar[x-1]!='.')segcount--;}
     else
     {
     if(ar[x-1]=='.' && ar[x+1]=='.')segcount++;
     else if(ar[x-1]!='.' && ar[x+1]!='.')segcount--;
     }

     }

     }

        System.out.println(count-segcount);
        ar[x]=z;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author Rishabh
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       // Scanner s=new Scanner(System.in);
        StringTokenizer st=new StringTokenizer(br.readLine());
        int n=Integer.parseInt(st.nextToken());int m=Integer.parseInt(st.nextToken());
        String inp=br.readLine();//s.next();
        char ar[]=inp.toCharArray();
        int count=0,c=0,segcount=0;
        if(n==1){
        for(int i=0;i<m;i++){
         StringTokenizer ss=new StringTokenizer(br.readLine());int x=Integer.parseInt(ss.nextToken())-1;char z=ss.nextToken().charAt(0);
        //ar[x]=z;
        System.out.println(0);
        }

        }else{StringBuilder sb=new StringBuilder();
        for(int i=0;i<n;i++){
            if(inp.charAt(i)!='.'){if(c>0){segcount++;c=0;}}
            if(inp.charAt(i)=='.'){count++;c++;}}if(c>0)segcount++;
           // System.out.println(count+" "+segcount);
        for(int i=0;i<m;i++){
       StringTokenizer ss=new StringTokenizer(br.readLine());int x=Integer.parseInt(ss.nextToken())-1;char z=ss.nextToken().charAt(0);

       // int count=0;
     if(z=='.'){
     if(ar[x]!='.')count++;
     if(ar[x]!='.'){
     if(x==0){if(ar[x+1]!='.')segcount++;}
     else if(x==n-1){if(ar[x-1]!='.')segcount++;}
     else
     {
     if(ar[x-1]=='.' && ar[x+1]=='.')segcount--;
     else if(ar[x-1]!='.' && ar[x+1]!='.')segcount++;
     }
     }

     }
     else{
     if(ar[x]=='.')count--;
     if(ar[x]=='.'){
     if(x==0){if(ar[x+1]!='.')segcount--;}
     else if(x==n-1){if(ar[x-1]!='.')segcount--;}
     else
     {
     if(ar[x-1]=='.' && ar[x+1]=='.')segcount++;
     else if(ar[x-1]!='.' && ar[x+1]!='.')segcount--;
     }

     }

     }
        sb.append(count-segcount);sb.append("\n");
       // System.out.println(count-segcount);
        ar[x]=z;
        }
    System.out.print(sb);}
}
}


import java.util.Scanner;

/**
 *
 * @author Rishabh
 */
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();int m=s.nextInt();
        String inp=s.next();
        char ar[]=inp.toCharArray();
        int count=0,c=0,segcount=0;
        if(n==1){
        for(int i=0;i<m;i++){
         int x=s.nextInt()-1;char z=s.next().charAt(0);
        //ar[x]=z;
        System.out.println(0);
        }

        }else{
        for(int i=0;i<n;i++){
            if(inp.charAt(i)!='.'){if(c>0){segcount++;c=0;}}
            if(inp.charAt(i)=='.'){count++;c++;}}if(c>0)segcount++;
           // System.out.println(count+" "+segcount);
        for(int i=0;i<m;i++){
        int x=s.nextInt()-1;char z=s.next().charAt(0);

       // int count=0;
     if(z=='.'){
     if(ar[x]!='.')count++;
     if(ar[x]!='.'){
     if(x==0){if(ar[x+1]!='.')segcount++;}
     else if(x==n-1){if(ar[x-1]!='.')segcount++;}
     else
     {
     if(ar[x-1]=='.' && ar[x+1]=='.')segcount--;
     else if(ar[x-1]!='.' && ar[x+1]!='.')segcount++;
     }
     }

     }
     else{
     if(ar[x]=='.')count--;
     if(ar[x]=='.'){
     if(x==0){if(ar[x+1]!='.')segcount--;}
     else if(x==n-1){if(ar[x-1]!='.')segcount--;}
     else
     {
     if(ar[x-1]=='.' && ar[x+1]=='.')segcount++;
     else if(ar[x-1]!='.' && ar[x+1]!='.')segcount--;
     }

     }

     }

        System.out.println(count-segcount);
        ar[x]=z;
        }
    }
}
}


import java.util.Scanner;

/**
 *
 * @author Rishabh
 */
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();int m=s.nextInt();
        String inp=s.next();
        char ar[]=inp.toCharArray();
        for(int i=0;i<m;i++){
        int x=s.nextInt();char z=s.next().charAt(0);
        ar[x-1]=z;
        int count=0;
        int conse=0;
        for(int j=0;j<n;j++){
          //  System.out.print(ar[j]);
        if(ar[j]!='.'){
        if(conse>0){count+=(conse-1);}
        conse=0;
        }
        else conse++;

       // System.out.println(conse);
        }
        if(conse>0)count+=(conse-1);

        System.out.println(count);

        }
    }
}


import java.io.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;

@SuppressWarnings("unchecked")
public class P570C {

  public void run() throws Exception {
    int n = nextInt();
    int m = nextInt();
    char [] s = next().toCharArray();

    TreeMap<Integer, Integer> il = new TreeMap();
    int f = 0;

    for (int i = 0; i < n; i++) {
      if (s[i] == '.') {
        int l = 1;
        for (; (((i + l) < n) && (s[i + l] == '.')); l++);
        il.put(i, l);
        f += l - 1;
        i += l;
      }
    }

    while ((m--) > 0) {
      int idx = nextInt() - 1;
      char ch = next().charAt(0);
      boolean changed = false;

      if ((ch == '.') && (s[idx] != '.')) {
        changed = true;

        int i1 = idx;
        int l1 = 1;

        if ((idx > 0) && (s[idx - 1] == '.')) {
          Map.Entry<Integer, Integer> me = il.floorEntry(idx);

          i1 = me.getKey();
          l1 += me.getValue();
          il.remove(i1);
        }

        if ((idx < (n - 1)) && (s[idx + 1] == '.')) {
          l1 += il.get(idx + 1);
          il.remove(idx + 1);
        }

        il.put(i1, l1);
      } else if (s[idx] == '.') {
        changed = true;

        Map.Entry<Integer, Integer> me = il.floorEntry(idx);
        int i1 = me.getKey();
        int l1 = idx - i1;
        int i2 = idx + 1;
        int l2 = i1 + me.getValue() - i2;

        il.remove(idx);

        if (l1 > 0) {
          il.put(i1, l1);
        }
        if (l2 > 0) {
          il.put(i2, l2);
        }
      }

      if (changed) {
        f = 0;
        for (int l : il.values()) {
          f += l - 1;
        }
      }

      println(f);
      s[idx] = ch;
    }

  }

  public static void main(String... args) throws Exception {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedOutputStream(System.out));
    new P570C().run();
    br.close();
    pw.close();
    System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
  }

  static long startTime = System.currentTimeMillis();
  static BufferedReader br;
  static PrintWriter pw;
  StringTokenizer stok;

  String nextToken() throws IOException {
    while (stok == null || !stok.hasMoreTokens()) {
      String s = br.readLine();
      if (s == null) { return null; }
      stok = new StringTokenizer(s);
    }
    return stok.nextToken();
  }

  void print(byte b) { print("" + b); }
  void print(int i) { print("" + i); }
  void print(long l) { print("" + l); }
  void print(double d) { print("" + d); }
  void print(char c) { print("" + c); }
  void print(Object o) {
    if (o instanceof int[]) { print(Arrays.toString((int [])o));
    } else if (o instanceof long[]) { print(Arrays.toString((long [])o));
    } else if (o instanceof char[]) { print(Arrays.toString((char [])o));
    } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
    } else if (o instanceof short[]) { print(Arrays.toString((short [])o));
    } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
    } else if (o instanceof float[]) { print(Arrays.toString((float [])o));
    } else if (o instanceof double[]) { print(Arrays.toString((double [])o));
    } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
    } else { print("" + o); }
  }
  void print(String s) { pw.print(s); }
  void println() { println(""); }
  void println(byte b) { println("" + b); }
  void println(int i) { println("" + i); }
  void println(long l) { println("" + l); }
  void println(double d) { println("" + d); }
  void println(char c) { println("" + c); }
  void println(Object o) { print(o); println(); }
  void println(String s) { pw.println(s); }
  int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
  long nextLong() throws IOException { return Long.parseLong(nextToken()); }
  double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
  char nextChar() throws IOException { return (char) (br.read()); }
  String next() throws IOException { return nextToken(); }
  String nextLine() throws IOException { return br.readLine(); }
  int [] readInt(int size) throws IOException {
    int [] array = new int [size];
    for (int i = 0; i < size; i++) { array[i] = nextInt(); }
    return array;
  }
  long [] readLong(int size) throws IOException {
    long [] array = new long [size];
    for (int i = 0; i < size; i++) { array[i] = nextLong(); }
    return array;
  }
  double [] readDouble(int size) throws IOException {
    double [] array = new double [size];
    for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
    return array;
  }
  String [] readLines(int size) throws IOException {
    String [] array = new String [size];
    for (int i = 0; i < size; i++) { array[i] = nextLine(); }
    return array;
  }
}