Under construction

Still being built. Things may change

AB

Interview

Time limit2sMemory limit1024 MB

Summary
Maintain string sets A and B with add and delete, and count ways to write a query string as a prefix of an A element joined with a suffix of a B element.
Level

Medium5 of 10

Topics
Hash map, String, Trie
Solved
No attempts yet

Problem

Write a program that handles the following queries on sets AA, BB and a string SS.

  • add A SS: Adds SS to AA.
  • delete A SS: Removes SS from AA.
  • add B SS: Adds SS to BB.
  • delete B SS: Removes SS from BB.
  • find SS: Prints the number of ways to form SS by joining a prefix of an element of AA with a suffix of an element of BB. Two ways are different if the elements differ or if the prefixes (suffixes) differ. Empty prefixes (suffixes) are not considered.

Initially, AA and BB are empty. No query adds an element that already exists or removes an element that does not exist.

Input

The first line contains the number of queries QQ. 1≤Q≤1 0001 \leq Q \leq 1\,000

Each of the next QQ lines contains one query. Strings consist of lowercase English letters, and each has length between 1 and 1,000 inclusive.

There is at least one find query.

Output

Print the answer to each find query on its own line.

Examples2

  1. Example 1

    Input
    7
    add A aba
    add A a
    add A ab
    add B bab
    add B b
    add B ab
    find abab
    
    Expected output
    10
    
  2. Example 2

    Input
    8
    add A a
    add B bcd
    add A ab
    add B cd
    find abcd
    delete A a
    delete B bcd
    find abcd
    
    Expected output
    4
    1