#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
const int maxM = 1e6 + 5;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long sq(long long x) { return (x * x) % MOD; }
long long modP(long long a, long long b) {
  return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}
bool on[maxN];
int n, id[maxM], TIME;
int f[maxM], sz = 0;
int Q[maxM], lt, rt;
int st[maxM], fn[maxM];
int trie[maxM][26];
long long fen[maxM];
string s[maxN], str;
vector<int> G[maxM];
void SHAHO() {
  for (int i = 1; i <= n; i++) {
    int curr = 0;
    for (auto ch : s[i]) {
      int c = ch - 'a';
      if (!trie[curr][c]) trie[curr][c] = ++sz;
      curr = trie[curr][c];
    }
    id[i] = curr;
  }
  for (int i = 0; i < 26; i++)
    if (trie[0][i]) Q[rt++] = trie[0][i];
  while (lt != rt) {
    int v = Q[lt++];
    for (int i = 0; i < 26; i++) {
      if (trie[v][i]) {
        Q[rt++] = trie[v][i];
        f[trie[v][i]] = trie[f[v]][i];
      } else
        trie[v][i] = trie[f[v]][i];
    }
  }
  return;
}
void DFS(int v) {
  st[v] = TIME++;
  for (auto u : G[v]) DFS(u);
  fn[v] = TIME;
  return;
}
void add(int p, int x) {
  for (; p < maxM; p += p & -p) fen[p] += x;
  return;
}
long long fetch(int p) {
  long long res = 0;
  for (; p > 0; p -= p & -p) res += fen[p];
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q >> n;
  for (int i = 1; i <= n; i++) cin >> s[i], on[i] = 1;
  SHAHO();
  for (int i = 0; i <= sz; i++)
    if (i != f[i]) G[f[i]].push_back(i);
  DFS(0);
  for (int i = 1; i <= n; i++) {
    add(st[id[i]], +1);
    add(fn[id[i]], -1);
  }
  int v;
  while (q--) {
    char t;
    cin >> t;
    if (t == '?') {
      cin >> str;
      v = 0;
      long long ans = 0;
      for (auto ch : str) {
        v = trie[v][ch - 'a'];
        ans += fetch(st[v]);
      }
      cout << ans << "\n";
      continue;
    }
    cin >> v;
    if (t == '+') {
      add(st[id[v]], 1 - on[v]);
      add(fn[id[v]], on[v] - 1);
      on[v] = 1;
    } else {
      add(st[id[v]], -on[v]);
      add(fn[id[v]], +on[v]);
      on[v] = 0;
    }
  }
  return 0;
}
