Problem: 2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only 2^{k-1} teams remain. If only one team remains, it is declared the champion; otherwise, 2^{k-2} games are played: in the first one of them, the winner of the game "1 vs 2" plays against the winner of the game "3 vs 4", then the winner of the game "5 vs 6" plays against the winner of the game "7 vs 8", and so on. This process repeats until only one team remains.

For example, this picture describes the chronological order of games with k = 3:

<image>

Let the string s consisting of 2^k - 1 characters describe the results of the games in chronological order as follows:

  * if s_i is 0, then the team with lower index wins the i-th game; 
  * if s_i is 1, then the team with greater index wins the i-th game; 
  * if s_i is ?, then the result of the i-th game is unknown (any team could win this game). 



Let f(s) be the number of possible winners of the tournament described by the string s. A team i is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team i is the champion.

You are given the initial state of the string s. You have to process q queries of the following form: 

  * p c — replace s_p with character c, and print f(s) as the result of the query. 

Input

The first line contains one integer k (1 ≤ k ≤ 18).

The second line contains a string consisting of 2^k - 1 characters — the initial state of the string s. Each character is either ?, 0, or 1.

The third line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries.

Then q lines follow, the i-th line contains an integer p and a character c (1 ≤ p ≤ 2^k - 1; c is either ?, 0, or 1), describing the i-th query.

Output

For each query, print one integer — f(s).

Example

Input


3
0110?11
6
5 1
6 ?
7 ?
1 ?
5 ?
1 1


Output


1
2
3
3
5
4

Solution (in JAVA):

Solution:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 *
 * @author is2ac
 */
public class D_CF {
    
    public static void main(String[] args) throws IOException {
        FastScanner58 fs = new FastScanner58();
        //Reader fs = new Reader();
        PrintWriter pw = new PrintWriter(System.out);
        //int t = fs.ni();
        int t = 1;
        for (int tc = 0; tc < t; tc++) {
            int n = fs.ni();
            int k = (int)Math.pow(2,n)-1;
            char[] s = fs.next().toCharArray();
            int q = fs.ni();
            Map<Integer,Integer> p = new HashMap();
            Map<Integer,int[]> c = new HashMap();
            Map<Integer,Integer> dp = new HashMap();
            int ind = k-2;
            for (int i = k-1; i > -1; i--) {
                if (ind<0) continue;
                int[] ch = new int[2];
                ch[1] = ind--;
                ch[0] = ind--;
                c.put(i,ch);
                p.put(ch[0],i);
                p.put(ch[1],i);
            }
            
            sum(k-1,p,c,s,dp);
            
            for (int i = 0; i < q; i++) {
                int node = fs.ni()-1;
                char nc = fs.next().charAt(0);
                int os = sum(node,p,c,s,dp);
                int ns = 0;
                s[node] = nc;
                if (!c.containsKey(node)) {
                    if (nc=='?') {
                        ns = 2;
                    } else {
                        ns = 1;
                    }
                } else {
                    int[] ch = c.get(node);
                    int a = sum(ch[0],p,c,s,dp);
                    int b = sum(ch[1],p,c,s,dp);
                    if (s[node]=='0') {
                        ns += a;
                    } else if (s[node]=='1') {
                        ns += b;
                    } else {
                        ns += a;
                        ns += b;
                    }
                }
                //s[node] = nc;
                dp.put(node,ns);
                while (node!=k-1) {
                    
                    int parent = p.get(node);
                    int[] ch = c.get(parent);
                    if (node==ch[0]) {
                        if (s[parent]=='1') {
                            break;
                        } else {
                            int sum = dp.get(parent);
                            sum -= os;
                            sum += ns;
                            dp.put(parent,sum);
                        }
                    } else {
                        if (s[parent]=='0') {
                            break;
                        } else {
                            int sum = dp.get(parent);
                            sum -= os;
                            sum += ns;
                            dp.put(parent,sum);
                        }
                    }
                    if (parent==k-1) break;
                    node = parent;
                }
                pw.println(sum(k-1,p,c,s,dp));
            }
        }
        
        pw.close();
    }
    public static int sum(int node, Map<Integer,Integer> p, Map<Integer,int[]> c, char[] s, Map<Integer,Integer> dp) {
        if (dp.containsKey(node)) return dp.get(node);
        if (!c.containsKey(node)) {
            if (s[node]=='?') {
                return 2;
            } else {
                return 1;
            }
        }
        int[] ch = c.get(node);
        int a = sum(ch[0],p,c,s,dp);
        int b = sum(ch[1],p,c,s,dp);
        int sum = 0;
        if (s[node]=='0') {
            sum += a;
        } else if (s[node]=='1') {
            sum += b;
        } else {
            sum += a;
            sum += b;
        }
        dp.put(node,sum);
        return sum;
    }
    public static int dis(int a, int b, int n) {
        //for a to get to b
        if (a < b) {
            return b - a;
        }
        b += n;
        return b - a;
    }

    public static long gcd(long n1, long n2) {
        if (n2 == 0) {
            return n1;
        }
        return gcd(n2, n1 % n2);
    }
}

class GFG {

    // A utility function to get the
    // middle index of given range.
    static int getMid(int s, int e) {
        return s + (e - s) / 2;
    }

    /*
    * A recursive function to get the sum
    of values in given range of the array.
    * The following are parameters
      for this function.
    *
    * st -> Pointer to segment tree
    * node -> Index of current node in
    *         the segment tree.
    * ss & se -> Starting and ending indexes
    *         of the segment represented
    *         by current node, i.e., st[node]
    * l & r -> Starting and ending indexes
    *         of range query
     */
    static long MinUtil(long[] st, int ss,
            int se, int l,
            int r, int node) {

        // If segment of this node is completely
        // part of given range, then return
        // the max of segment
        if (l <= ss && r >= se) {
            return st[node];
        }

        // If segment of this node does not
        // belong to given range
        if (se < l || ss > r) {
            return (long) (1e11);
        }

        // If segment of this node is partially
        // the part of given range
        int mid = getMid(ss, se);
        
        return Math.min(
                MinUtil(st, ss, mid, l, r,
                        2 * node + 1),
                MinUtil(st, mid + 1, se, l, r,
                        2 * node + 2));
    }

    /*
    * A recursive function to update the
    nodes which have the given index in their
    * range. The following are parameters
    st, ss and se are same as defined above
    * index -> index of the element to be updated.
     */
    static void updateValue(long arr[], long[] st, int ss,
            int se, int index,
            long value,
            int node) {
        if (index < ss || index > se) {
            //System.out.println("Invalid Input");
            return;
        }
        
        if (ss == se) {

            // update value in array and in
            // segment tree
            arr[index] = value;
            st[node] = value;
        } else {
            int mid = getMid(ss, se);
            
            if (index >= ss && index <= mid) {
                updateValue(arr, st, ss, mid,
                        index, value,
                        2 * node + 1);
            } else {
                updateValue(arr, st, mid + 1, se, index,
                        value, 2 * node + 2);
            }
            
            st[node] = Math.min(st[2 * node + 1],
                    st[2 * node + 2]);
        }
        return;
    }

    // Return max of elements in range from
    // index l (query start) to r (query end).
    static long getMin(long[] st, int n, int l, int r) {

        // Check for erroneous input values
        if (l < 0 || r > n - 1 || l > r) {
            //System.out.printf("Invalid Input\n");
            return (long) (1e11);
        }
        
        return MinUtil(st, 0, n - 1, l, r, 0);
    }

    // A recursive function that constructs Segment
    // Tree for array[ss..se]. si is index of
    // current node in segment tree st
    static long constructSTUtil(long arr[],
            int ss, int se,
            long[] st, int si) {

        // If there is one element in array, store
        // it in current node of segment tree and return
        if (ss == se) {
            st[si] = arr[ss];
            return arr[ss];
        }

        // If there are more than one elements, then
        // recur for left and right subtrees and
        // store the max of values in this node
        int mid = getMid(ss, se);
        
        st[si] = Math.min(
                constructSTUtil(arr, ss, mid,
                        st, si * 2 + 1),
                constructSTUtil(arr, mid + 1,
                        se, st,
                        si * 2 + 2));
        
        return st[si];
    }

    /*
    * Function to construct segment tree from
    given array. This function allocates
    * memory for segment tree.
     */
    static long[] constructST(long arr[], int n) {

        // Height of segment tree
        int x = (int) Math.ceil(Math.log(n) / Math.log(2));

        // Maximum size of segment tree
        int max_size = 2 * (int) Math.pow(2, x) - 1;

        // Allocate memory
        long[] st = new long[max_size];

        // Fill the allocated memory st
        constructSTUtil(arr, 0, n - 1, st, 0);

        // Return the constructed segment tree
        return st;
    }
}

class BIT16 {
    
    long[] bit;
    
    public BIT16(int size) {
        bit = new long[size];
    }
    
    public void update(int ind, long delta) {
        while (ind < bit.length) {
            bit[ind] += delta;
            ind = ind + (ind & (-1 * ind));
        }
    }
    
    public long sum(int ind) {
        long s = 0;
        while (ind > 0) {
            s += bit[ind];
            ind = ind - (ind & (-1 * ind));
        }
        return s;
    }
    
    public long query(int l, int r) {
        return sum(r) - sum(l);
    }
}

class UnionFind18 {
    
    int[] id;
    
    public UnionFind18(int size) {
        id = new int[size];
        for (int i = 0; i < size; i++) {
            id[i] = i;
        }
    }
    
    public int find(int p) {
        int root = p;
        while (root != id[root]) {
            root = id[root];
        }
        while (p != root) {
            int next = id[p];
            id[p] = root;
            p = next;
        }
        return root;
    }
    
    public void union(int p, int q) {
        int a = find(p), b = find(q);
        if (a == b) {
            return;
        }
        id[b] = a;
    }
}

class Reader {
    
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;
    
    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }
    
    public Reader(String file_name) throws IOException {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }
    
    public String readLine() throws IOException {
        byte[] buf = new byte[64];
        int cnt = 0, c;
        while ((c = read()) != -1) {
            if (c == '\n') {
                break;
            }
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }
    
    public int ni() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = (c == '-');
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) {
            return -ret;
        }
        return ret;
    }
    
    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = (c == '-');
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) {
            return -ret;
        }
        return ret;
    }
    
    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = (c == '-');
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') {
            while ((c = read()) >= '0' && c <= '9') {
                ret += (c - '0') / (div *= 10);
            }
        }
        if (neg) {
            return -ret;
        }
        return ret;
    }
    
    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) {
            buffer[0] = -1;
        }
    }
    
    private byte read() throws IOException {
        if (bufferPointer == bytesRead) {
            fillBuffer();
        }
        return buffer[bufferPointer++];
    }
    
    public void close() throws IOException {
        if (din == null) {
            return;
        }
        din.close();
    }
}

class FastScanner58 {
    
    BufferedReader br;
    StringTokenizer st;
    
    public FastScanner58() {
        br = new BufferedReader(new InputStreamReader(System.in), 32768);
        st = null;
    }
    
    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }
    
    int ni() {
        return Integer.parseInt(next());
    }
    
    int[] intArray(int N) {
        int[] ret = new int[N];
        for (int i = 0; i < N; i++) {
            ret[i] = ni();
        }
        return ret;
    }
    
    long nl() {
        return Long.parseLong(next());
    }
    
    long[] longArray(int N) {
        long[] ret = new long[N];
        for (int i = 0; i < N; i++) {
            ret[i] = nl();
        }
        return ret;
    }
    
    double nd() {
        return Double.parseDouble(next());
    }
    
    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}