#include <bits/stdc++.h>
using namespace std;
int dp[1000050][26];
int vir[1000050], sz[1000050], vis[1000050], jump[1000050], last[1000050];
char ar[1000050];
vector<pair<int, int> > pi(1000050);
int p = 1, t;
int up(int n) {
  int i, k = 0;
  for (i = 0; i < n; i++) {
    if (dp[k][ar[i] - 'a'] == 0) dp[k][ar[i] - 'a'] = p, p++;
    k = dp[k][ar[i] - 'a'];
  }
  sz[k]++;
  return k;
}
void build() {
  int i, n, a, b, c;
  queue<int> q;
  for (i = 0; i < 26; i++) {
    if (dp[0][i] > 0) vir[dp[0][i]] = 0, q.push(dp[0][i]);
  }
  while (!q.empty()) {
    a = q.front();
    q.pop();
    for (i = 0; i < 26; i++) {
      n = dp[a][i];
      if (n > 0) {
        b = vir[a];
        while (dp[b][i] == 0) {
          c = vir[b];
          if (c == 0 && b == 0) break;
          b = c;
        }
        if (dp[b][i] > 0)
          vir[n] = dp[b][i];
        else
          vir[n] = 0;
        q.push(n);
      }
    }
  }
  return;
}
void buildjump() {
  int i, a;
  for (i = 1; i < p; i++) {
    a = vir[i];
    while (a > 0 && sz[a] == 0 && a > i) a = vir[a];
    if (sz[a] > 0)
      jump[i] = a;
    else
      jump[i] = jump[a];
  }
  return;
}
int cal(int a) {
  int j;
  if (pi[a].second == t) return pi[a].first;
  pi[a].second = t;
  pi[a].first = sz[a] + cal(jump[a]);
  return pi[a].first;
}
int findstr(int n) {
  int ans = 0, i, j, k = 0, a, x;
  for (i = 1; i < n; i++) {
    x = ar[i] - 'a';
    while (dp[k][x] == 0) {
      a = vir[k];
      if (a == 0 && k == 0) break;
      k = a;
    }
    if (dp[k][x] > 0) k = dp[k][x];
    a = k;
    ans += cal(a);
  }
  return ans;
}
int main() {
  int i, n, m, x, k;
  scanf("%d", &x), scanf("%d", &k);
  for (t = 1; t <= k; t++) {
    scanf("%s", ar);
    n = strlen(ar);
    last[t] = up(n);
    vis[t] = 1;
  }
  build();
  buildjump();
  for (t = 1; t <= x; t++) {
    scanf("%s", ar);
    n = strlen(ar);
    if (ar[0] == '?')
      printf("%d\n", findstr(n));
    else {
      m = 0;
      for (i = 1; i < n; i++) m *= 10, m += (ar[i] - '0');
      if (ar[0] == '+' && vis[m] == 0)
        vis[m] = 1, sz[last[m]]++;
      else if (ar[0] == '-' && vis[m] == 1)
        vis[m] = 0, sz[last[m]]--;
    }
  }
  return 0;
}
