Under construction

Still being built. Things may change

Pizza Marathon

Time limit5sMemory limit1024 MB

Summary
Answer sums of prices over pizzas whose names start with pizza i's name and have length at least k, with point price updates.
Level

Medium7 of 10

Topics
Trie, Tree, Segment tree, String
Solved
No attempts yet

Problem

Tired of the hard work underground, the Ninja Turtles decided to take a vacation. How better to spend free time when you are green and carry a shell on your back? A week-long pizza marathon, of course!

The turtles have the menu of their favorite pizzeria. Each pizza has a name and a price. The size of a pizza equals the length of its name. Some pizzas the turtles have already tried. To order, they pick a pizza they liked and order all pizzas similar to it with size at least kk. Pizza ii is similar to pizza jj if sis_i is a prefix of sjs_j.

The turtles cannot travel around the city themselves, so they asked April to report changes at the pizzeria, in particular changes in prices.

During the week, Donatello wrote down pizza orders and price changes in his notebook. At the end of the week he tried to total all orders, found there were too many, and asked you to help.

Input

The first line contains two integers nn and mm (1≤n≤1061 \le n \le 10^6, 1≤m≤1061 \le m \le 10^6), the number of pizzas on the menu and the number of notes in the notebook. The next nn lines describe the menu: the ii-th line contains the name sis_i and the initial price cic_i (0<ci≤1090 < c_i \le 10^9) of the ii-th pizza, separated by a space. Names consist only of lowercase Latin letters. The total length of all names does not exceed 10610^6. All names are distinct.

The next mm lines contain Donatello's notes in one of the following formats:

  • "? i k": the turtles ordered pizzas similar to the ii-th pizza with size at least kk (1≤i≤n1 \le i \le n, 1≤k≤1061 \le k \le 10^6).
  • "! i j": April reported that the price of the ii-th pizza increased by jj (1≤i≤n1 \le i \le n, ∣j∣≤109|j| \le 10^9).

Output

For each order, print its total cost on a separate line.

Examples2

  1. Example 1

    Input
    2 4
    margarita 20
    marga 10
    ? 1 5
    ? 1 6
    ? 2 5
    ? 2 6
    
    Expected output
    30
    20
    10
    0
    
  2. Example 2

    Input
    2 2
    supreme 20
    supremesuper 30
    ! 2 20
    ? 2 2
    
    Expected output
    70