#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
char s[N];
int trie[N][26], tot, fail[N], idx[N], L[N], R[N], C[N], cnt;
bool vis[N];
vector<int> mp[N];
void add(int x, int v) {
  while (x <= cnt) {
    C[x] += v;
    x += x & -x;
  }
}
int sum(int x) {
  int ans = 0;
  while (x > 0) {
    ans += C[x];
    x -= x & -x;
  }
  return ans;
}
void dfs(int u) {
  L[u] = ++cnt;
  for (int i = 0; i < mp[u].size(); i++) dfs(mp[u][i]);
  R[u] = cnt;
}
void Insert(int x) {
  int rt = 0, n = strlen(s);
  for (int i = 0; i < n; i++) {
    int id = s[i] - 'a';
    if (!trie[rt][id]) trie[rt][id] = ++tot;
    rt = trie[rt][id];
  }
  idx[x] = rt;
}
void getfail() {
  queue<int> q;
  for (int i = 0; i < 26; i++)
    if (trie[0][i]) q.push(trie[0][i]);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < 26; i++) {
      if (trie[x][i]) {
        fail[trie[x][i]] = trie[fail[x]][i];
        q.push(trie[x][i]);
      } else
        trie[x][i] = trie[fail[x]][i];
    }
  }
}
int main() {
  int q, n;
  scanf("%d %d", &q, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    Insert(i);
  }
  getfail();
  for (int i = 1; i <= tot; i++) mp[fail[i]].push_back(i);
  dfs(0);
  for (int i = 1; i <= n; i++) {
    add(L[idx[i]], 1);
    add(R[idx[i]] + 1, -1);
    vis[i] = 1;
  }
  while (q--) {
    scanf("%s", s);
    int id = 0, len = strlen(s);
    if (s[0] == '+') {
      for (int i = 1; i < len; i++) id = id * 10 + s[i] - '0';
      if (vis[id]) continue;
      add(L[idx[id]], 1);
      add(R[idx[id]] + 1, -1);
      vis[id] = 1;
    } else if (s[0] == '-') {
      for (int i = 1; i < len; i++) id = id * 10 + s[i] - '0';
      if (!vis[id]) continue;
      add(L[idx[id]], -1);
      add(R[idx[id]] + 1, 1);
      vis[id] = 0;
    } else {
      int ans = 0, now = 0;
      for (int i = 1; i < len; i++) {
        int id = s[i] - 'a';
        now = trie[now][id];
        ans += sum(L[now]);
      }
      printf("%d\n", ans);
    }
  }
}
