#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
int q, n;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q >> n;
  int cnt = 1;
  vector<map<int, int>> to(cnt);
  auto go = [&](string s) {
    int cur = 0;
    for (char a : s) {
      int id = a - 'a';
      if (!to[cur].count(id)) {
        to[cur][id] = cnt++;
        to.resize(cnt);
      }
      cur = to[cur][id];
    }
    return cur;
  };
  vector<string> a(n);
  vector<int> pos(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    pos[i] = go(a[i]);
  }
  vector<int> suff(cnt);
  suff[0] = -1;
  vector<int> que = {0};
  for (int b = 0; b < (int)que.size(); b++) {
    int v = que[b];
    for (auto it : to[v]) {
      int c = it.second;
      int id = it.first;
      int p = suff[v];
      while (p > -1 && !to[p].count(id)) p = suff[p];
      if (p != -1) suff[c] = to[p][id];
      que.push_back(c);
    }
  }
  vector<vector<int>> g(cnt);
  for (int i = 1; i < cnt; i++) {
    g[suff[i]].push_back(i);
  }
  vector<int> orders;
  vector<int> din(cnt);
  vector<int> dout(cnt);
  function<void(int)> dfs = [&](int v) {
    din[v] = orders.size();
    orders.push_back(v);
    for (int i : g[v]) {
      dfs(i);
    }
    dout[v] = orders.size() - 1;
  };
  dfs(0);
  int sz = 4 * cnt + 5;
  vector<int> tree(sz);
  vector<int> ad(sz);
  function<void(int, int, int, int, int, int)> modify =
      [&](int x, int l, int r, int ll, int rr, int v) {
        assert(x >= 0 && x < sz);
        if (ll <= l && r <= rr) {
          tree[x] += v;
          ad[x] += v;
          return;
        }
        int y = (l + r) >> 1;
        if (ad[x]) {
          tree[2 * x + 1] += ad[x];
          tree[2 * x + 2] += ad[x];
          ad[2 * x + 1] += ad[x];
          ad[2 * x + 2] += ad[x];
          ad[x] = 0;
        }
        if (y >= ll) modify(2 * x + 1, l, y, ll, rr, v);
        if (y < rr) modify(2 * x + 2, y + 1, r, ll, rr, v);
      };
  function<int(int, int, int, int)> get = [&](int x, int l, int r, int p) {
    assert(x >= 0 && x < sz);
    if (l == r) {
      assert(r == p);
      return tree[x];
    }
    int y = (l + r) >> 1;
    if (ad[x]) {
      tree[2 * x + 1] += ad[x];
      tree[2 * x + 2] += ad[x];
      ad[2 * x + 1] += ad[x];
      ad[2 * x + 2] += ad[x];
      ad[x] = 0;
    }
    if (y >= p)
      return get(2 * x + 1, l, y, p);
    else
      return get(2 * x + 2, y + 1, r, p);
  };
  vector<int> alive(n, 1);
  for (int v : pos) {
    modify(0, 0, cnt - 1, din[v], dout[v], 1);
  }
  while (q--) {
    char t;
    cin >> t;
    if (t == '?') {
      string s;
      cin >> s;
      long long ret = 0;
      int cur = 0;
      for (char a : s) {
        int id = a - 'a';
        while (cur != -1 && !to[cur].count(id)) cur = suff[cur];
        if (cur != -1)
          cur = to[cur][id];
        else
          cur = 0;
        ret += get(0, 0, cnt - 1, din[cur]);
      }
      cout << ret << endl;
    } else {
      int v;
      cin >> v;
      v--;
      if (t == '+' && !alive[v]) {
        modify(0, 0, cnt - 1, din[pos[v]], dout[pos[v]], 1);
        alive[v] = 1;
      }
      if (t == '-' && alive[v]) {
        modify(0, 0, cnt - 1, din[pos[v]], dout[pos[v]], -1);
        alive[v] = 0;
      }
    }
  }
  return 0;
}
