#include <bits/stdc++.h>
using namespace std;
int cc = 0, c[2 * 1000005], tot = 0, n, m, tree[1000005][26], mark[1000005],
    b[1000005], bj[1000005], fail[1000005], g[1000005], loc[1000005],
    st[1000005], ed[1000005], last[1000005];
vector<int> d[1000005], dui;
void add(int ii, string ss) {
  int i, p, w = 0;
  for (i = 0; i < (int)ss.size(); i++) {
    p = ss[i] - 'a';
    if (!tree[w][p]) tree[w][p] = ++tot;
    w = tree[w][p];
  }
  mark[w]++;
  b[ii] = w;
}
void bfs() {
  int h, t, fa, p, i;
  for (h = t = 1; h <= t; h++) {
    fa = g[h];
    for (i = 0; i < 26; i++)
      if (tree[fa][i]) {
        p = fail[fa];
        while (p && !tree[p][i]) p = fail[p];
        if (tree[p][i] && tree[p][i] != tree[fa][i])
          fail[tree[fa][i]] = tree[p][i];
        g[++t] = tree[fa][i];
        d[fail[g[t]]].push_back(g[t]);
      }
  }
}
void dfs() {
  int p;
  dui.push_back(0);
  st[0] = ++cc;
  while (!dui.empty()) {
    p = dui[dui.size() - 1];
    if (last[p] == d[p].size()) {
      ed[p] = ++cc;
      dui.pop_back();
    } else {
      dui.push_back(d[p][last[p]]);
      st[d[p][last[p]]] = ++cc;
      last[p]++;
    }
  }
}
void change(int v, int w) {
  for (; v <= cc; v += v & (-v)) c[v] += w;
}
void init() {
  int i;
  string ss;
  scanf("%d%d\n", &m, &n);
  for (i = 1; i <= n; i++) {
    getline(cin, ss);
    add(i, ss);
  }
  bfs();
  dfs();
  for (i = 0; i <= tot; i++) {
    change(st[i], mark[i]);
    change(ed[i], -mark[i]);
  }
}
int sum(int v) {
  int tq = 0;
  for (; v; v -= v & (-v)) tq += c[v];
  return tq;
}
void calc(string ss) {
  long long ans = 0;
  int i, p, w = 0;
  for (i = 0; i < (int)ss.size(); i++) {
    p = ss[i] - 'a';
    while (w && !tree[w][p]) w = fail[w];
    if (tree[w][p]) w = tree[w][p];
    ans += sum(st[w]);
  }
  cout << ans << endl;
}
void work() {
  char op;
  int i, tq;
  string ss;
  for (i = 1; i <= m; i++) {
    op = getchar();
    if (op == '?') {
      getline(cin, ss);
      calc(ss);
    } else {
      scanf("%d\n", &tq);
      if (op == '+') {
        if (bj[tq]) {
          bj[tq] = 0;
          change(st[b[tq]], mark[b[tq]]);
          change(ed[b[tq]], -mark[b[tq]]);
        }
      } else {
        if (!bj[tq]) {
          bj[tq] = 1;
          change(st[b[tq]], -mark[b[tq]]);
          change(ed[b[tq]], mark[b[tq]]);
        }
      }
    }
  }
}
int main() {
  init();
  work();
}
