import java.io.*;
import java.util.*;

public class tipofyourtongue_bowen {
  static class Pair {
    int first, second;
    public Pair(int f, int s) {
      first = f;
      second = s;
    }
    @Override
    public int hashCode() {
      return (first * 127 + 999937) ^ second;
    }

    @Override
    public boolean equals(Object obj) {
      if (this == obj) return true;
      Pair ano = (Pair)obj;
      return ano.first == first && ano.second == second;
    }
  }

  static class Quadruplet {
    Pair first, second;
    public Quadruplet(Pair f, Pair s) {
      first = f;
      second = s;
    }
    @Override
    public int hashCode() {
      return (first.hashCode() * 127 + 999937) ^ second.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
      if (this == obj) return true;
      Quadruplet ano = (Quadruplet)obj;
      return ano.first.equals(first) && ano.second.equals(second);
    }
  }
  static final int MAXN = 1000005;
  static long[] A = new long[] { 234234234, 987654321 };
  static long[][] Apow = new long[2][MAXN];
  static int[] P = new int[]{ 1000000007, 1000000009 };
  static int[][] hstr = new int[2][MAXN];
  static void init() {
    for (int t = 0; t < 2; t++) {
      Apow[t][0] = 1;
      for (int i = 1; i < MAXN; i++) Apow[t][i] = Apow[t][i - 1] * A[t] % P[t];
    }
  }
  static void hashit(String s){
    int n = s.length();
    for (int t = 0; t < 2; t++) {
      for (int i = 0; i < n; i++) {
        hstr[t][i] = (int)((i > 0 ? hstr[t][i - 1] : 0) * A[t] % P[t] + (s.charAt(i) - 'a' + 1));
        if (hstr[t][i] >= P[t]) hstr[t][i] -= P[t];
      }
    }
  }
  static Pair gethash(int l, int r){
    int[] h = new int[2];
    for (int t = 0; t < 2; t++) {
      h[t] = (int)(P[t] + hstr[t][r] - (l > 0 ? hstr[t][l - 1] : 0) * Apow[t][r - l + 1] % P[t]);
      if (h[t] >= P[t]) h[t] -= P[t];
    }
    return new Pair(h[0], h[1]);
  }
  public static void main(String[] args) throws IOException {
    init();

    InputReader in = new InputReader(new InputStreamReader(System.in));
    int N = in.nextInt(), Q = in.nextInt();
    HashMap<Quadruplet, Integer> preAndSufCnt = new HashMap<>();
    HashMap<Pair, Integer> preCnt = new HashMap<>(), sufCnt = new HashMap<>();
    for (int i = 0; i < N; i++) {
      String s = in.next();
      hashit(s);
      int len = s.length();
      for (int j = 1; j <= len; j++) {
        Pair ph = gethash(0, j - 1);
        Pair sh = gethash(len - j, len - 1);
        preCnt.put(ph, preCnt.containsKey(ph) ? preCnt.get(ph) + 1 : 1);
        sufCnt.put(sh, sufCnt.containsKey(sh) ? sufCnt.get(sh) + 1 : 1);
        Quadruplet ps = new Quadruplet(ph, sh);
        preAndSufCnt.put(ps, preAndSufCnt.containsKey(ps) ? preAndSufCnt.get(ps) + 1 : 1);
      }
    }
    while (Q-- > 0) {
      String t = in.next(), p = in.next(), q = in.next();
      int len = p.length();
      hashit(p);
      Pair ph = gethash(0, len - 1);
      hashit(q);
      Pair sh = gethash(0, len - 1);

      int ans = 0;
      Quadruplet ps = new Quadruplet(ph, sh);
      int psVal = preAndSufCnt.containsKey(ps) ? preAndSufCnt.get(ps) : 0;
      if (t.equals("AND"))
        ans = psVal;
      else if (t.equals("OR"))
        ans = (preCnt.containsKey(ph) ? preCnt.get(ph) : 0) + (sufCnt.containsKey(sh) ? sufCnt.get(sh) : 0) - psVal;
      else
        ans = (preCnt.containsKey(ph) ? preCnt.get(ph) : 0) + (sufCnt.containsKey(sh) ? sufCnt.get(sh) : 0) - 2 * psVal;
      System.out.println(ans);
    }
  }

  public static int[] unique(int[] values) {
    Arrays.sort(values);
    int L = 1;
    for (int i = 1; i < values.length; i++) {
      if (values[i] != values[i - 1]) values[L++] = values[i];
    }
    int[] result = new int[L];
    for (int i = 0; i < L; i++) result[i] = values[i];
    return result;
  }
}

class InputReader {
  BufferedReader br;
  StringTokenizer st;

  InputReader(InputStreamReader in) {
    br = new BufferedReader(in);
  }

  String next() {
    while (st == null || !st.hasMoreTokens()) {
      try {
        st = new StringTokenizer(br.readLine());
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return st.nextToken();
  }

  int nextInt() {
    return Integer.parseInt(next());
  }
}
