#include <bits/stdc++.h>
const int N = 1e6;
struct Edge {
  int to, nxt;
} e[N * 2 + 10];
int head[N + 10], tote = 1;
inline void addEdge(int u, int v) {
  e[++tote].to = v;
  e[tote].nxt = head[u];
  head[u] = tote;
}
int n, m, trie[N + 10][26], ed[N + 10], mp[N + 10], fail[N + 10], tot;
char s[N + 10];
int c[N * 2 + 10], L[N + 10], R[N + 10], cnt;
bool vis[N + 10];
char opt[N + 10];
inline void insert(char* str, int id) {
  int len = int(strlen(str)), u = 0;
  for (int i = 0; i < len; i++) {
    int k = str[i] - 'a';
    if (!trie[u][k]) trie[u][k] = ++tot;
    u = trie[u][k];
  }
  ed[u]++;
  mp[id] = u;
}
void build() {
  std::queue<int> q;
  for (int i = 0; i < 26; i++)
    if (trie[0][i]) q.push(trie[0][i]);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < 26; i++) {
      if (trie[u][i])
        fail[trie[u][i]] = trie[fail[u]][i], q.push(trie[u][i]);
      else
        trie[u][i] = trie[fail[u]][i];
    }
  }
}
void DFS(int u) {
  L[u] = ++cnt;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    DFS(v);
  }
  R[u] = ++cnt;
}
inline void modify(int pos, int x) {
  for (; pos <= cnt; pos += (pos & (-pos))) c[pos] += x;
}
inline int query(int pos) {
  int res = 0;
  for (; pos; pos -= (pos & (-pos))) res += c[pos];
  return res;
}
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    insert(s, i);
  }
  build();
  for (int i = 1; i <= tot; i++) addEdge(fail[i], i);
  DFS(0);
  for (int i = 1; i <= n; i++)
    vis[i] = 1, modify(L[mp[i]], 1), modify(R[mp[i]], -1);
  while (m--) {
    scanf("%s", opt);
    int len = int(strlen(opt + 1));
    if (opt[0] == '?') {
      int ans = 0, u = 0;
      for (int i = 1; i <= len; i++) {
        int k = opt[i] - 'a';
        u = trie[u][k], ans += query(L[u]);
      }
      printf("%d\n", ans);
    }
    if (opt[0] == '+') {
      int x;
      sscanf(opt + 1, "%d", &x);
      if (!vis[x]) vis[x] = 1, modify(L[mp[x]], 1), modify(R[mp[x]], -1);
    }
    if (opt[0] == '-') {
      int x;
      sscanf(opt + 1, "%d", &x);
      if (vis[x]) vis[x] = 0, modify(L[mp[x]], -1), modify(R[mp[x]], 1);
    }
  }
  return 0;
}
