Under construction

Still being built. Things may change

Ssari and Bird's Pyramid

Time limit1sMemory limit256 MB

Summary
Count how many times a given letter appears in a requested pyramid row filled by repeating a word in alternating directions.
Level

Medium5 of 10

Topics
Math, Prefix sum
Solved
No attempts yet

Problem

Ssari and Bird had nothing at all to do, so they built a pyramid of height NN in the back yard and decided to write their favorite word on it. Row ii from the top has ii cells.

The word goes in one letter per cell, in a zigzag. Row 1 fills from left to right, row 2 from right to left, row 3 from left to right again, and so on. Once the word is written out, it starts over from its first letter.

For N=6N = 6 and the word JANJETINA the pyramid comes out like this.

     J
    N A
   J E T
  J A N I
 A N J E T
N A J A N I

Bird picks a row number aia_i and an uppercase letter cic_i, then asks "how many times does cic_i appear in row aia_i?". Answer Bird's KK questions and help Ssari out.

Input

The first line contains the height of the pyramid, NN (1≤N≤10181 \le N \le 10^{18}).

The second line contains a word made only of uppercase letters. The word is at most 10610^6 letters long.

The third line contains the number of rows Bird picked, KK (1≤K≤500001 \le K \le 50000).

Each of the next KK lines contains aia_i and cic_i separated by a space (1≤ai≤N1 \le a_i \le N, and cic_i is an uppercase letter).

Output

Print KK lines. Line ii holds the number of times cic_i appears in row aia_i of the pyramid.

Examples3

  1. Example 1

    Input
    6
    JANJETINA
    5
    1 J
    1 A
    6 N
    6 I
    5 E
    
    Expected output
    1
    0
    2
    1
    1
    
  2. Example 2

    Input
    5
    A
    5
    1 A
    2 A
    3 A
    4 A
    5 B
    
    Expected output
    1
    2
    3
    4
    0
    
  3. Example 3

    Input
    3
    AB
    3
    2 A
    2 B
    3 B
    
    Expected output
    1
    1
    2