#include <bits/stdc++.h>
const int MAXL = 1e6;
int n, m;
char str[MAXL + 5];
class AcceptedAutomaton {
 private:
  int cnt, fail[MAXL + 5], end[MAXL + 5], trans[MAXL + 5][26];
  int indx, ref[MAXL + 5], bit[MAXL + 5], dfn[MAXL + 5], rdfn[MAXL + 5],
      siz[MAXL + 5];
  bool status[MAXL + 5];
  std ::vector<int> graph[MAXL + 5];
  inline int lowbit(const int x) const { return x & -x; }
  inline void update(const int x, const int k) {
    for (int i = x; i <= cnt + 1; i += lowbit(i)) bit[i] += k;
  }
  inline int sum(const int x) const {
    int ret = 0;
    for (int i = x; i; i -= lowbit(i)) ret += bit[i];
    return ret;
  }

 public:
  inline bool getStatus(const int id) const { return status[id]; }
  inline void insert(const char* str, const int id) {
    int r = 0;
    for (int i = 0; str[i]; ++i) {
      if (!trans[r][str[i] - 'a']) trans[r][str[i] - 'a'] = ++cnt;
      r = trans[r][str[i] - 'a'];
    }
    ++end[r], ref[id] = r, status[id] = true;
  }
  inline void build() {
    static std ::queue<int> q;
    for (; !q.empty(); q.pop())
      ;
    for (int i = 0; i ^ 26; ++i)
      if (trans[0][i]) q.push(trans[0][i]);
    for (; !q.empty(); q.pop()) {
      int u = q.front();
      end[u] += end[fail[u]], graph[fail[u]].push_back(u);
      for (int i = 0; i ^ 26; ++i) {
        if (!trans[u][i])
          trans[u][i] = trans[fail[u]][i];
        else
          fail[trans[u][i]] = trans[fail[u]][i], q.push(trans[u][i]);
      }
    }
  }
  inline void travel(const int u) {
    dfn[u] = ++indx, siz[rdfn[indx] = u] = 1;
    for (int v : graph[u]) travel(v), siz[u] += siz[v];
  }
  inline void initends() {
    travel(0);
    for (int i = 0; i <= cnt; ++i)
      update(dfn[i], end[i] - end[rdfn[dfn[i] - 1]]);
  }
  inline void add(const int id) {
    int u = ref[id];
    status[id] = true;
    update(dfn[u], 1), update(dfn[u] + siz[u], -1);
  }
  inline void rem(const int id) {
    int u = ref[id];
    status[id] = false;
    update(dfn[u], -1), update(dfn[u] + siz[u], 1);
  }
  inline int matchSum(const char* str) {
    int r = 0, ret = 0;
    for (int i = 0; str[i]; ++i) ret += sum(dfn[r = trans[r][str[i] - 'a']]);
    return ret;
  }
} ac;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%s", str);
    ac.insert(str, i);
  }
  ac.build(), ac.initends();
  for (; n--;) {
    scanf("%s", str);
    if (str[0] == '?')
      printf("%d\n", ac.matchSum(str + 1));
    else {
      int id;
      sscanf(str + 1, "%d", &id);
      if (str[0] == '+' && !ac.getStatus(id)) ac.add(id);
      if (str[0] == '-' && ac.getStatus(id)) ac.rem(id);
    }
  }
  return 0;
}
