#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int f = 1, ans = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return f * ans;
}
const int MAXN = 3e5 + 11;
struct AC_String {
  int ch[MAXN][28], tot = 1, fail[MAXN];
  queue<int> que;
  int ins(char *s) {
    int u = 1, len = strlen(s);
    for (int i = 0; i < len; i++) {
      if (!ch[u][s[i] - 'a']) ch[u][s[i] - 'a'] = ++tot;
      u = ch[u][s[i] - 'a'];
    }
    return u;
  }
  void Get() {
    for (int i = 0; i < 26; i++) ch[0][i] = 1;
    que.push(1);
    while (!que.empty()) {
      int xx = que.front();
      que.pop();
      for (int i = 0; i < 26; i++) {
        if (!ch[xx][i])
          ch[xx][i] = ch[fail[xx]][i];
        else {
          que.push(ch[xx][i]);
          fail[ch[xx][i]] = ch[fail[xx]][i];
        }
      }
    }
    return;
  }
} S;
int N, Q, ps[MAXN], M;
char str[MAXN];
multiset<int> s[MAXN];
vector<int> vec[MAXN];
struct Segment {
  int Maxn[MAXN << 2];
  void init() {
    memset(Maxn, -1, sizeof(Maxn));
    return;
  }
  void Modify(int k, int l, int r, int ps, int w) {
    if (l == r) {
      Maxn[k] = w;
      return;
    }
    int mid = (l + r) >> 1;
    if (ps <= mid)
      Modify(k << 1, l, mid, ps, w);
    else
      Modify(k << 1 | 1, mid + 1, r, ps, w);
    Maxn[k] = max(Maxn[k << 1], Maxn[k << 1 | 1]);
    return;
  }
  int Query(int k, int l, int r, int x, int y) {
    if (x <= l && r <= y) return Maxn[k];
    int res = -1, mid = (l + r) >> 1;
    if (x <= mid) res = max(res, Query(k << 1, l, mid, x, y));
    if (mid < y) res = max(res, Query(k << 1 | 1, mid + 1, r, x, y));
    return res;
  }
} T;
int dfn[MAXN], rev[MAXN], fa[MAXN], top[MAXN], num, siz[MAXN], son[MAXN],
    W[MAXN];
void dfs(int u) {
  siz[u] = 1;
  for (auto v : vec[u]) {
    fa[v] = u;
    dfs(v);
    siz[u] += siz[v];
    if (siz[v] > siz[son[u]]) son[u] = v;
  }
  return;
}
void dfs1(int u) {
  if (son[u]) {
    dfn[son[u]] = ++num, rev[num] = son[u], top[son[u]] = top[u];
    dfs1(son[u]);
  }
  for (auto v : vec[u]) {
    if (v == son[u]) continue;
    dfn[v] = ++num, rev[num] = v, top[v] = v;
    dfs1(v);
  }
  return;
}
int calc(int x) {
  int res = -1;
  while (x != -1) {
    res = max(res, T.Query(1, 1, M + 1, dfn[top[x]], dfn[x]));
    x = fa[top[x]];
  }
  return res;
}
int main() {
  N = read(), Q = read();
  for (int i = 1; i <= N; i++) {
    scanf("%s", str);
    int x = S.ins(str);
    ps[i] = x;
  }
  S.Get();
  for (int i = 1; i <= S.tot; i++) vec[S.fail[i]].push_back(i);
  M = S.tot;
  for (int i = 1; i <= N; i++) s[ps[i]].insert(0);
  fa[0] = -1;
  dfs(0);
  dfn[0] = num = 1, rev[1] = 0;
  dfs1(0);
  T.init();
  for (int i = 1; i <= M; i++)
    if (s[i].size()) T.Modify(1, 1, M + 1, dfn[i], 0);
  while (Q--) {
    int opt = read();
    if (opt == 1) {
      int id = read(), x = ps[id], w = read();
      s[x].erase(s[x].find(W[id]));
      W[id] = w;
      s[x].insert(w);
      T.Modify(1, 1, M + 1, dfn[x], *(--s[x].end()));
    }
    if (opt == 2) {
      int u = 1, res = -1;
      scanf("%s", str);
      int len = strlen(str);
      for (int i = 0; i < len; i++)
        u = S.ch[u][str[i] - 'a'], res = max(res, calc(u));
      printf("%d\n", res);
    }
  }
  return 0;
}
