#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 123, base = 784578;
int pow1[300005] = {1}, pref1[300005];
int cnt[300005];
unordered_map<int, int> mp;
vector<int> lens;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (int i = 1; i < 300005; i++) pow1[i] = 1LL * pow1[i - 1] * base % mod;
  int q, t;
  string s;
  cin >> q;
  while (q--) {
    cin >> t >> s;
    for (int i = 0; i < s.size(); i++)
      pref1[i + 1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
    if (t == 1) {
      mp[1LL * pref1[s.size()] * pow1[300005 - 1 - s.size()] % mod]++;
      if (cnt[s.size()]++ == 0) lens.push_back(s.size());
    } else if (t == 2) {
      mp[1LL * pref1[s.size()] * pow1[300005 - 1 - s.size()] % mod]--;
      if (--cnt[s.size()] == 0)
        lens.erase(find(lens.begin(), lens.end(), s.size()));
    } else {
      long long ans = 0LL;
      for (int i = 0; i < s.size(); i++)
        for (int& l : lens)
          if (i + l <= s.size())
            ans += mp[1LL * (pref1[i + l] - pref1[i] + mod) *
                      pow1[300005 - 1 - i - l] % mod];
      cout << ans << endl;
    }
  }
}
