#include <bits/stdc++.h>
using namespace std;
const int MX = 100005;
const int BLOCK = 205;
const int inf = 1000000069;
const long long inf_ll = 8000000000000000069ll;
const long long mod = 1e9 + 7;
const long long inv2 = (mod + 1) / 2;
const int dxh[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dyh[] = {2, -2, 2, -2, 1, -1, 1, -1};
const int dx[] = {1, -1, 0, 0, 0, 0};
const int dy[] = {0, 0, 1, -1, 0, 0};
const int dz[] = {0, 0, 0, 0, 1, -1};
const int dxd[] = {1, 1, 1, 0, -1, -1, -1, 0};
const int dyd[] = {1, 0, -1, -1, -1, 0, 1, 1};
mt19937 rng(time(NULL));
struct aho_corasick {
  static const int alpha = 26;
  vector<array<int, alpha> > go;
  vector<int> link, end_pos;
  aho_corasick() { add_node(); }
  int add_string(string s) {
    int nw = 0;
    for (char c : s) {
      if (!go[nw][c - 'a']) {
        int get = add_node();
        go[nw][c - 'a'] = get;
      }
      nw = go[nw][c - 'a'];
    }
    end_pos[nw]++;
    return nw;
  }
  void build() {
    queue<int> q;
    for (int c = 0; c < alpha; c++)
      if (go[0][c]) q.push(go[0][c]);
    for (int nw, u; !q.empty();) {
      nw = q.front();
      q.pop();
      u = link[nw];
      for (int c = 0; c < alpha; c++) {
        if (!go[nw][c])
          go[nw][c] = go[u][c];
        else {
          link[go[nw][c]] = go[u][c];
          end_pos[go[nw][c]] += end_pos[go[u][c]];
          q.push(go[nw][c]);
        }
      }
    }
  }
  int add_node() {
    int ps = go.size();
    go.emplace_back(array<int, alpha>());
    link.emplace_back(0);
    end_pos.emplace_back(0);
    return ps;
  }
} ac;
int fwt[MX * 10];
void upd(int ps, int v) {
  for (ps++; ps < MX * 10; ps += ps & -ps) fwt[ps] += v;
}
int que(int ps) {
  int rs = 0;
  for (ps++; ps > 0; ps -= ps & -ps) rs += fwt[ps];
  return rs;
}
void updrange(int lf, int rg, int v) {
  upd(lf, v);
  upd(rg + 1, -v);
}
int q, k;
string s[MX];
int pt[MX], cr[MX];
vector<int> adj[MX * 10];
int tin[MX * 10], tout[MX * 10], tim = 0;
void dfs(int nw) {
  tin[nw] = tim++;
  for (int nx : adj[nw]) dfs(nx);
  tout[nw] = tim;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> q >> k;
  for (int i = 1; i <= k; i++) {
    cin >> s[i];
    pt[i] = ac.add_string(s[i]);
    cr[i] = 1;
  }
  ac.build();
  int n = ac.go.size();
  for (int i = 1; i < n; i++) {
    adj[ac.link[i]].push_back(i);
  }
  dfs(0);
  for (string x; q--;) {
    cin >> x;
    string r = "";
    for (int i = 1; i < x.size(); i++) r += x[i];
    if (x[0] == '?') {
      long long ans = 0ll;
      int nw = 0;
      for (char c : r) {
        nw = ac.go[nw][c - 'a'];
        ans += ac.end_pos[nw] + que(tin[nw]);
      }
      cout << ans << "\n";
    } else if (x[0] == '+') {
      int u = stoi(r);
      if (cr[u] != 1) {
        cr[u] = 1;
        updrange(tin[pt[u]], tout[pt[u]] - 1, 1);
      }
    } else {
      int u = stoi(r);
      if (cr[u] != -1) {
        cr[u] = -1;
        updrange(tin[pt[u]], tout[pt[u]] - 1, -1);
      }
    }
  }
  return 0;
}
