Snake Escaping

Given a toxicity value for each of 2^L bitmasks, answer Q queries: each query fixes some bits and leaves others free, and asks the sum of values over all matching masks.

Medium7Bit manipulationPrefix sumDynamic programmingMathNo attempts yetTime limit2sMemory limit64 MB

Problem

JOI Laboratory keeps 2L2^L poisonous snakes, numbered 0,1,,2L10, 1, \ldots, 2^L - 1. Each snake is divided into LL parts from the head to the tail, and each part is either blue or red. Write the number of snake ii in binary as i=k=1Lck2Lki = \sum_{k=1}^{L} c_k 2^{L-k} (0ck10 \le c_k \le 1). Then

  • if ck=0c_k = 0, the kk-th part of snake ii from the head is blue, and
  • if ck=1c_k = 1, the kk-th part of snake ii from the head is red.

Each snake has an integer between 00 and 99, inclusive, called its toxicity. You are given a string SS of length 2L2^L consisting of the digits 00 to 99. The ii-th character of SS (1i2L1 \le i \le 2^L) is the toxicity of snake i1i - 1.

Snakes are quick, so they often escape from JOI Laboratory. People living near the laboratory file complaints when they see snakes escaping.

You are given the complaints for QQ days. The complaint for the dd-th day (1dQ1 \le d \le Q) is a string TdT_d of length LL consisting of the characters 00, 11, and ??.

  • If the jj-th character of TdT_d (1jL1 \le j \le L) is 00, the jj-th part of every snake that escaped on the dd-th day is blue.
  • If the jj-th character of TdT_d is 11, the jj-th part of every snake that escaped on the dd-th day is red.
  • If the jj-th character of TdT_d is ??, nobody reported anything about the jj-th part of the snakes that escaped on the dd-th day.

Every complaint is accurate. All snakes that escaped were caught by the laboratory staff on the same day, so the same snake may escape again on a different day.

To estimate the risk of escaping snakes, Professor K, the executive director of JOI Laboratory, wants to know, for each day, the sum of the toxicities of the snakes that might have escaped. That is, for the dd-th day you must add up the toxicities of all snakes that do not contradict TdT_d.

Given the string SS describing the toxicities and the complaints for QQ days, write a program that computes, for each day, the sum of the toxicities of the snakes that might have escaped from the laboratory.

Note that the memory limit for this task is small.

Input

Read the following data from standard input.

  • The first line contains two integers LL and QQ separated by a space: the number of parts of each snake and the number of days with complaints.
  • The second line contains a string SS of length 2L2^L describing the toxicities of the snakes.
  • The dd-th of the following QQ lines (1dQ1 \le d \le Q) contains a string TdT_d of length LL, the complaint for the dd-th day.

Output

Write QQ lines to standard output. The dd-th line must contain one integer: the sum of the toxicities of the snakes that might have escaped on the dd-th day.

Constraints

  • 1L201 \le L \le 20
  • 1Q10000001 \le Q \le 1\,000\,000
  • SS is a string of length 2L2^L.
  • SS consists only of the characters 0,1,2,3,4,5,6,7,8,90, 1, 2, 3, 4, 5, 6, 7, 8, 9.
  • TdT_d is a string of length LL (1dQ1 \le d \le Q).
  • TdT_d consists only of the characters 0,1,?0, 1, ? (1dQ1 \le d \le Q).