output_description: Print the number of unordered pairs (S_i, S_j) where i \neq j and Limak can
  obtain one string from the other.
  
  * * *
input_description: Input is given from Standard Input in the following format.
  
      
      
      N
      S_1
      S_2
      \vdots
      S_N
attempt: from collections import Counter
  from string import ascii_lowercase
  
  n = int(input())
  sss = [input() for _ in range(n)]
  flags = {c: 1 << (ord(c) - 97) for c in ascii_lowercase}
  
  trie_chr = [-1]
  trie_children = [{}]
  trie_fin = [0]
  l = 1
  
  for s in sss:
      node = 0
      for i in range(len(s) - 1, 0, -1):
          c = ord(s[i]) - 97
          if c in trie_children[node]:
              node = trie_children[node][c]
          else:
              trie_chr.append(c)
              trie_children.append({})
              trie_fin.append(0)
              trie_children[node][c] = l
              node = l
              l += 1
      trie_fin[node] |= flags[s[0]]
  
  ans = 0
  
  for s in sss:
      cnt = Counter(s)
      flag = 0
      for c in cnt:
          flag |= flags[c]
      node = 0
      if len(s) > 1 and trie_fin[node] > 0:
          ans += bin(trie_fin[node] & flag).count("1")
  
      for i in range(len(s) - 1, 1, -1):
          c = s[i]
          cnt[c] -= 1
          if cnt[c] == 0:
              flag ^= flags[c]
          k = ord(c) - 97
          node = trie_children[node][k]
          if trie_fin[node] > 0:
              ans += bin(trie_fin[node] & flag).count("1")
  
  print(ans)
  
problem_description: Statement
  
  Limak can repeatedly remove one of the first two characters of a string, for
  example abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx.
  
  You are given N different strings S_1, S_2, \ldots, S_N. Among N \cdot (N-1) /
  2 pairs (S_i, S_j), in how many pairs could Limak obtain one string from the
  other?
samples: [{"input": "3\n    abcxyx\n    cyx\n    abc", "output": "1\n    \n\nThe only good pair is (abcxyx, cyx).\n\n* * *"}, {"input": "6\n    b\n    a\n    abc\n    c\n    d\n    ab", "output": "5\n    \n\nThere are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab)."}]