#include <bits/stdc++.h>
const int oo = 2139063143;
const int N = 1210000;
const int P = 1000000007;
using namespace std;
inline void sc(int &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
inline void print(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
inline void pr(long long x) { print(x), putchar('\n'); }
struct EDGE {
  int v, nx;
} lb[N];
int tote = 1, top[N];
void addedge(int u, int v) { lb[++tote] = (EDGE){v, top[u]}, top[u] = tote; }
int dfn[N], las[N];
void dfs(int u) {
  dfn[u] = ++dfn[0];
  for (int kb = top[u]; kb; kb = lb[kb].nx) dfs(lb[kb].v);
  las[u] = dfn[0];
}
int bit[N];
void add(int x, int d) {
  for (; x <= dfn[0]; x += x & -x) bit[x] += d;
}
int sum(int x) {
  int ans = 0;
  for (; x; x -= x & -x) ans += bit[x];
  return ans;
}
struct ACAM {
  int fa, son[27];
} ac[N];
int totp = 1;
int pos[N];
void ins(char *s, int n, int id) {
  int p = 1;
  for (int i = 1; i <= n; i++) {
    int &v = ac[p].son[s[i] - 'a' + 1];
    if (!v) v = ++totp;
    p = v;
  }
  pos[id] = p;
}
queue<int> q;
void build() {
  for (int i = 1; i <= 26; i++) ac[0].son[i] = 1;
  q.push(1);
  while (q.size()) {
    int u = q.front(), f = ac[u].fa;
    q.pop();
    for (int c = 1; c <= 26; c++) {
      int &v = ac[u].son[c], g = ac[f].son[c];
      if (v)
        ac[v].fa = g, q.push(v);
      else
        v = g;
    }
  }
}
bool vis[N];
char s[N];
int main() {
  int m, n;
  sc(m), sc(n);
  for (int i = 1; i <= n; i++)
    scanf("%s", s + 1), ins(s, strlen(s + 1), i), vis[i] = 1;
  build();
  for (int i = 1; i <= totp; i++) addedge(ac[i].fa, i);
  dfs(1);
  for (int i = 1; i <= n; i++) add(dfn[pos[i]], 1), add(las[pos[i]] + 1, -1);
  while (m--) {
    scanf("%s", s);
    int len = strlen(s);
    if (s[0] == '?') {
      int cur = 1;
      long long ans = 0;
      for (int i = 1; i < len; i++) {
        cur = ac[cur].son[s[i] - 'a' + 1];
        ans += sum(dfn[cur]);
      }
      pr(ans);
    } else {
      int id = 0;
      for (int i = 1; i < len; i++) id = id * 10 + s[i] - '0';
      if ((s[0] == '+') == vis[id]) continue;
      add(dfn[pos[id]], s[0] == '+' ? 1 : -1),
          add(las[pos[id]] + 1, s[0] == '+' ? -1 : 1);
      vis[id] = s[0] == '+';
    }
  }
  return 0;
}
