#include <bits/stdc++.h>
using namespace std;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(pair<uint64_t, uint64_t> x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x.first + FIXED_RANDOM) ^
           (splitmix64(x.second + FIXED_RANDOM) >> 1);
  }
};
struct PolyHash {
  static const int mod = 1e9 + 123;
  static vector<int> pow1;
  static vector<unsigned long long> pow2;
  static int base;
  vector<int> pref1;
  vector<unsigned long long> pref2;
  PolyHash(string& s) : pref1(s.size() + 1, 0), pref2(s.size() + 1, 0) {
    for (int i = 0; i < s.size(); i++)
      pref1[i + 1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod,
                pref2[i + 1] = (pref2[i] + s[i] * pow2[i]);
  }
  inline pair<int, unsigned long long> operator()(int pos, int len) const {
    int hash1 = pref1[pos + len] - pref1[pos];
    unsigned long long hash2 = pref2[pos + len] - pref2[pos];
    hash1 += (hash1 < 0 ? mod : 0);
    hash1 = 1LL * hash1 * pow1[(int)pow1.size() - pos - len - 1] % mod;
    hash2 *= pow2[(int)pow1.size() - pos - len - 1];
    return {hash1, hash2};
  }
  static inline int gen_base(int st, int en) {
    mt19937 rnd(
        chrono::high_resolution_clock::now().time_since_epoch().count());
    int base = uniform_int_distribution<int>(st + 1, en)(rnd);
    return base & 1 ? base : base - 1;
  }
};
int PolyHash::base(gen_base(256, PolyHash::mod - 1));
vector<int> PolyHash::pow1{1};
vector<unsigned long long> PolyHash::pow2{1};
int cnt[300005];
unordered_map<pair<int, unsigned long long>, int, custom_hash> mp;
vector<int> lens;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  while (PolyHash::pow1.size() <= 300005)
    PolyHash::pow1.push_back(1LL * PolyHash::pow1.back() * PolyHash::base %
                             PolyHash::mod),
        PolyHash::pow2.push_back(PolyHash::pow2.back() * PolyHash::base);
  int q, t;
  string s;
  cin >> q;
  while (q--) {
    cin >> t >> s;
    PolyHash p(s);
    if (t == 1) {
      mp[p(0, s.size())]++;
      if (cnt[s.size()]++ == 0) lens.push_back(s.size());
    } else if (t == 2) {
      mp[p(0, s.size())]--;
      if (--cnt[s.size()] == 0)
        lens.erase(find(lens.begin(), lens.end(), s.size()));
    } else {
      long long ans = 0LL;
      PolyHash p(s);
      for (int i = 0; i < s.size(); i++)
        for (int& l : lens) ans += (i + l <= s.size() ? mp[p(i, l)] : 0);
      cout << ans << endl;
    }
  }
}
