RAM

Time limit2sMemory limit512 MB

Summary
Process files one by one; after each, count how many times a given letter appears in the last K characters seen so far.
Level

Medium6 of 10

Topics
Array, Simulation, Prefix sum, Implementation
Solved
No attempts yet

Problem

Hackers broke into Mirko's computer through the Shellshock vulnerability and raised the system voltage, destroying almost all of its RAM except the last 2MB. Mirko's computer has exactly 26 hard disks, labeled with the uppercase English letters A to Z. Fortunately, Mirko has a huge log of hard disk accesses. The log is a string of hard disk labels in the order the disks were accessed.

Mirko analyzed the attack as follows:

  • He split the log into NN smaller files S1,S2,…,SNS_1, S_2, \ldots, S_N that fit in RAM. Each file is a string of uppercase English letters, and concatenating the files in order gives the whole log.
  • He loaded the files one after another. Right after loading file SiS_i, he counted how many times hard disk HiH_i was accessed among the last KiK_i accesses of the log from its beginning up to and including file SiS_i.

Write a program that answers all NN of Mirko's queries.

Input

The first line contains the number of files NN (1≤N≤200 0001 \le N \le 200\,000).

The next 2N2N lines are divided into NN groups of two lines. The ii-th group is:

  • The first line contains a string SiS_i of uppercase English letters (1≤∣Si∣≤4 0001 \le |S_i| \le 4\,000).
  • The second line contains an uppercase English letter HiH_i, the label of a hard disk, and the number of accesses KiK_i, separated by a space (1≤Ki≤∑j=1i∣Sj∣1 \le K_i \le \sum_{j=1}^{i} |S_j|).

The total length of all files, ∑i=1N∣Si∣\sum_{i=1}^{N} |S_i|, is at most 2 000 0002\,000\,000.

Output

Print NN lines with the answers to Mirko's queries in order. Line ii contains the exact number of characters equal to HiH_i among the last KiK_i characters of the concatenation of S1S_1 through SiS_i.

Examples2

  1. Example 1

    Input
    3
    BAAB
    B 2
    AABB
    A 6
    ZA
    Z 1
    
    Expected output
    1
    3
    0
    
  2. Example 2

    Input
    1
    A
    A 1
    
    Expected output
    1