Fedya string S initially array W initially emptythere n query process time Query consist lowercase english letter c_i nonnegative integer w_i first c_i append S w_i append W the answer query sum suspiciousnesse subsegment W L R 1 leq L leq R leq iWe define suspiciousness subsegment follow substre S corresponding subsegment string consecutive character Lth Rth inclusive match prefix S length substring corresponding subsegment 1 R l 1 suspiciousness equal minimum array W L R subsegment Otherwise case substre match correspond prefix suspiciousness 0help Fedya answer query orderly come