#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 233;
const int maxk = 1e5 + 233;
int ch[maxn][26], f[maxn], val[maxn], pre[maxn], trie_size = 0;
int n, k, cur = 0;
bool inq[maxk];
vector<int> a[maxk];
int vis[maxk * 2], b[maxk * 2], dp[maxk * 2], st[maxk * 2], en[maxk * 2];
string s[maxk];
char t[maxn];
queue<int> q;
long long ans = 0;
void Build_Trie() {
  int cur, c, l;
  memset(ch, 0, sizeof(ch));
  memset(val, 0, sizeof(val));
  for (int i = 1; i <= n; i++) {
    cur = 0;
    l = s[i].length();
    for (int j = 0; j < l; j++) {
      c = s[i][j] - 'a';
      if (!ch[cur][c]) ch[cur][c] = ++trie_size;
      cur = ch[cur][c];
    }
    val[cur] = i;
  }
  return;
}
void get_fail() {
  int u, v, r;
  for (int i = 0; i < 26; i++) {
    u = ch[0][i];
    if (!u) continue;
    q.push(u);
    f[u] = 0;
    if (val[u]) a[0].push_back(val[u]);
  }
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int c = 0; c < 26; c++) {
      v = ch[u][c];
      r = f[u];
      if (!v) continue;
      q.push(v);
      while (r && !ch[r][c]) r = f[r];
      f[v] = ch[r][c];
      pre[v] = (val[f[v]] ? f[v] : pre[f[v]]);
      if (val[v]) a[val[pre[v]]].push_back(val[v]);
    }
  }
  return;
}
void add(int x, int v) {
  for (int i = x; i <= cur; i += (i & (-i))) b[i] += v;
  return;
}
int query(int x) {
  int sum = 0;
  for (int i = st[x]; i; i -= (i & (-i))) sum += b[i];
  return sum + dp[x];
}
void dfs(int x, int fa) {
  vis[++cur] = x;
  st[x] = en[x] = cur;
  for (int i = 0; i < a[x].size(); i++) {
    int y = a[x][i];
    if (y == fa) continue;
    dp[y] = dp[x] + 1;
    dfs(y, x);
    vis[++cur] = x;
    en[x] = cur;
  }
  return;
}
int main() {
  cin >> k >> n;
  for (int i = 1; i <= n; i++) cin >> s[i];
  Build_Trie();
  get_fail();
  getchar();
  cur = 0;
  dp[0] = 0;
  dfs(0, -1);
  for (int i = 1; i <= n; i++) inq[i] = 1;
  char op;
  int x, l, c, j = 0;
  for (int i = 1; i <= k; i++) {
    op = getchar();
    if (op == '+') {
      scanf("%d", &x);
      getchar();
      if (inq[x] == 0) {
        add(st[x], 1);
        add(en[x] + 1, -1);
      }
      inq[x] = 1;
    } else if (op == '-') {
      scanf("%d", &x);
      getchar();
      if (inq[x] == 1) {
        add(st[x], -1);
        add(en[x] + 1, 1);
      }
      inq[x] = 0;
    } else {
      ans = 0;
      scanf("%s", t);
      getchar();
      l = strlen(t);
      j = 0;
      for (int i = 0; i < l; i++) {
        c = t[i] - 'a';
        while (j && !ch[j][c]) j = f[j];
        j = ch[j][c];
        if (val[j])
          ans += query(val[j]);
        else if (pre[j])
          ans += query(val[pre[j]]);
      }
      printf("%I64d\n", ans);
    }
  }
  return 0;
}
