Implement the StreamChecker class as follows:

StreamChecker(words): Constructor, init the data structure with the given words.
query(letter): returns true if and only if for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.

 
Example:
StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary.
streamChecker.query('a');          // return false
streamChecker.query('b');          // return false
streamChecker.query('c');          // return false
streamChecker.query('d');          // return true, because 'cd' is in the wordlist
streamChecker.query('e');          // return false
streamChecker.query('f');          // return true, because 'f' is in the wordlist
streamChecker.query('g');          // return false
streamChecker.query('h');          // return false
streamChecker.query('i');          // return false
streamChecker.query('j');          // return false
streamChecker.query('k');          // return false
streamChecker.query('l');          // return true, because 'kl' is in the wordlist

 
Note:

1 <= words.length <= 2000
1 <= words[i].length <= 2000
Words will only consist of lowercase English letters.
Queries will only consist of lowercase English letters.
The number of queries is at most 40000.

Difficulty: MEDIUM
Tags: String algorithms

Data structures
Skill types: Data structures

Starter code:
class StreamChecker:
    def __init__(self, words: List[str]):
    def query(self, letter: str) -> bool:
# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)

Solutions:
class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.stream = deque([])
		for word in set(words):
			node = self.trie
			for ch in word[::-1]:
				if not ch in node:
					node[ch] = {}
				node = node[ch]
			node['$'] = word

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.trie
		for ch in self.stream:
			if '$' in node:
				return True
			if not ch in node:
				return False
			node = node[ch]
		return '$' in node


class TrieNode:

	def __init__(self):
		self.isword = False
		self.children = [None] * 26

class StreamChecker:

	def __init__(self, words):
		self.root = TrieNode()
		self.lastchars = []
		for word in words:
			self.add(word)

	def add(self, word):
		parent = self.root
		for c in reversed(word):
			i = ord(c) - 97
			if not parent.children[i]:
				parent.children[i] = TrieNode()
			parent = parent.children[i]
		parent.isword = True

	def search(self, word):
		parent = self.root
		for c in reversed(word):
			i = ord(c) - 97
			if parent.isword:
				return True
			if not parent.children[i]:
				return False
			parent = parent.children[i]
		return parent.isword

	def query(self, letter):
		self.lastchars.append(letter)
		return self.search(self.lastchars)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = set(words)
		self.letters = ''

	def query(self, letter: str) -> bool:
		self.letters += letter
		for x in self.words:
			if self.letters.endswith(x):
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add(word)

	def query(self, char: str) -> bool:
		return self.trie.find(char)

class Trie:

	class Node:

		def __init__(self, complete=False):
			self.complete = complete
			self.children = {}

	def __init__(self):
		self.root = self.Node()
		self.current = []

	def add(self, word: str) -> bool:
		node = self.root
		for (index, char) in enumerate(word):
			if char not in node.children:
				node.children[char] = self.Node(index == len(word) - 1)
			node = node.children[char]
			if index == len(word) - 1:
				node.complete = index == len(word) - 1
		return True

	def find(self, char: str) -> bool:
		self.current.append(self.root)
		self.current = [node.children[char] for node in self.current if char in node.children]
		return any((node.complete for node in self.current))


class StreamChecker(object):

	def __init__(self, words):
		self.waitlist = []
		self.trie = dict()
		for word in words:
			temp_dict = self.trie
			for letter in word:
				temp_dict = temp_dict.setdefault(letter, dict())
			temp_dict['#'] = '#'

	def query(self, letter):
		waitlist = []
		if letter in self.trie:
			waitlist.append(self.trie[letter])
		for item in self.waitlist:
			if letter in item:
				waitlist.append(item[letter])
		self.waitlist = waitlist
		return any(('#' in item for item in self.waitlist))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		for ele in words:
			self.add(ele)
		self.stack = [self.root]

	def add(self, ele):
		current = self.root
		for char in ele:
			current = current.children[char]
		current.last = True

	def query(self, letter: str) -> bool:
		flag = False
		new_stack = [self.root]
		for ele in self.stack:
			if letter in ele.children:
				new_stack.append(ele.children[letter])
				if ele.children[letter].last:
					flag = True
		self.stack = new_stack
		if flag:
			return True
		return False

class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.last = False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isEnd = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		node = self.root
		for char in word:
			if char not in node.children:
				node.children[char] = TrieNode()
			node = node.children[char]
		node.isEnd = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.maxlen = len(words)
		self.cache = ''
		for w in words:
			self.trie.insert(w[::-1])

	def query(self, letter: str) -> bool:
		self.cache += letter
		self.result = False
		k = 1
		node = self.trie.root
		while k <= len(self.cache):
			char = self.cache[-k]
			if char in node.children:
				self.result = self.result or node.children[char].isEnd
				k += 1
				node = node.children[char]
			else:
				break
		if len(self.cache) > self.maxlen:
			self.cache = self.cache[1:]
		return self.result


class TrieNode:

	def __init__(self):
		self.is_word = False
		self.chars = {}

class Trie:

	def __init__(self):
		self.root = TrieNode()
		self.prev_nodes = []

	def addWord(self, word):
		curr = self.root
		for c in word:
			if c not in curr.chars:
				curr.chars[c] = TrieNode()
			curr = curr.chars[c]
		curr.is_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.addWord(word)

	def query(self, letter: str) -> bool:
		curr_nodes = []
		for prev_node in self.trie.prev_nodes:
			if letter in prev_node.chars:
				curr_nodes.append(prev_node.chars[letter])
		if letter in self.trie.root.chars:
			curr_nodes.append(self.trie.root.chars[letter])
		self.trie.prev_nodes = curr_nodes
		found_letter = False
		for node in curr_nodes:
			if node.is_word:
				found_letter = True
		return found_letter


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			curnode = self.root
			for ch in word:
				if ch not in curnode:
					curnode[ch] = {}
				curnode = curnode[ch]
			curnode['is_end'] = True
		self.leads = [self.root]

	def query(self, letter: str) -> bool:
		next_leads = [self.root]
		found_word = False
		for lead in self.leads:
			if letter in lead:
				new_lead = lead[letter]
				next_leads.append(new_lead)
				if 'is_end' in new_lead:
					found_word = True
		self.leads = next_leads
		return found_word


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			node = self.root
			for c in word[::-1]:
				if c not in node:
					node[c] = {}
				node = node[c]
			node['$'] = True
		self.stream = []

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		node = self.root
		for c in self.stream[::-1]:
			if c in node:
				node = node[c]
				if '$' in node:
					return True
			else:
				break
		return False


from collections import deque

class StreamChecker:
	TERMINAL = None

	def __init__(self, words: List[str]):
		self.trie = {}
		self.build_trie(words)
		self.cursors = deque([])

	def build_trie(self, words: List[str]):
		trie = self.trie
		for word in words:
			node = trie
			for char in word:
				if char not in node:
					node[char] = {}
				node = node[char]
			node[self.TERMINAL] = True

	def query(self, letter: str) -> bool:
		cursors = self.cursors
		cursors.append(self.trie)
		found = False
		valid_cursors = []
		while cursors:
			cursor = cursors.popleft()
			if letter in cursor:
				cursor = cursor[letter]
				valid_cursors.append(cursor)
				if self.TERMINAL in cursor:
					found = True
		cursors.extend(valid_cursors)
		return found


from collections import defaultdict

class TrieNode:

	def __init__(self):
		self.children = defaultdict(TrieNode)
		self.end = False

class Trie:

	def __init__(self, words):
		self.root = TrieNode()
		self.processing = []
		for word in words:
			self.add(word)

	def add(self, word):
		node = self.root
		for c in word:
			node = node.children[c]
		node.end = True

	def search(self, c):
		processed = []
		self.processing.append(self.root)
		found = False
		for node in self.processing:
			if c in node.children:
				node = node.children[c]
				processed.append(node)
				found |= node.end
		self.processing = processed
		return found

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie(words)

	def query(self, letter: str) -> bool:
		return self.trie.search(letter)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = {}
		for i in words:
			cur = self.words
			for l in i[::-1]:
				if l not in cur:
					cur[l] = {}
				cur = cur[l]
			cur['.'] = {}
		self.his = []

	def query(self, letter: str) -> bool:
		self.his.append(letter)
		cur = self.words
		for i in self.his[::-1]:
			if cur.get(i) == None:
				return False
			cur = cur[i]
			if '.' in cur:
				return True
		return False


from collections import deque

class TrieNode:

	def __init__(self, char):
		self.char = char
		self.children = {}
		self.eow = False

class Trie:

	def __init__(self):
		self.rootTrie = TrieNode('*')
		self.currNode = self.rootTrie

	def addWord(self, word):
		self.currNode = self.rootTrie
		for (idx, char) in enumerate(word):
			if char not in self.currNode.children:
				charNode = TrieNode(char)
				self.currNode.children[char] = charNode
			self.currNode = self.currNode.children[char]
		self.currNode.eow = True

	def findNext(self, lettersQ):
		self.currNode = self.rootTrie
		for char in lettersQ:
			if char in self.currNode.children:
				self.currNode = self.currNode.children[char]
				if self.currNode.eow:
					return True
			else:
				return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.streamTrie = Trie()
		self.lettersQ = deque()
		for word in words:
			self.streamTrie.addWord(word[::-1])

	def query(self, letter: str) -> bool:
		self.lettersQ.appendleft(letter)
		if len(self.lettersQ) > 2000:
			self.lettersQ.pop()
		return self.streamTrie.findNext(self.lettersQ)


class TrieNode:

	def __init__(self):
		self.is_word = False
		self.children = defaultdict(TrieNode)

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add_word(self, word):
		node = self.root
		for c in word:
			node = node.children[c]
		node.is_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add_word(word)
		self.candidates = [self.trie.root]

	def query(self, letter: str) -> bool:
		new_candidates = [self.trie.root]
		ans = False
		for option in self.candidates:
			if letter in option.children:
				child = option.children[letter]
				new_candidates.append(child)
				ans = ans or child.is_word
		self.candidates = new_candidates
		return ans


class Trie:

	def __init__(self):
		self.children = defaultdict(list)
		self.end_of_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		self.nodes = []
		for word in words:
			self.addWord(word)

	def addWord(self, word):
		node = self.root
		for w in word:
			if w not in node.children:
				node.children[w] = Trie()
			node = node.children[w]
		node.end_of_word = True

	def query(self, letter: str) -> bool:
		self.nodes.append(self.root)
		temp = False
		new_nodes = []
		for node in self.nodes:
			if letter in node.children:
				node = node.children[letter]
				if node.end_of_word:
					temp = True
				new_nodes.append(node)
		self.nodes = new_nodes
		return temp


class TrieNode:

	def __init__(self, value, leaf=False, children=None):
		self.children = children or {}
		self.value = value
		self.leaf = leaf

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode(value=None)
		self.pointers = []
		for word in words:
			node = self.root
			for c in word:
				if c not in node.children:
					c_node = TrieNode(c)
					node.children[c] = c_node
				node = node.children[c]
			node.leaf = True

	def query(self, letter: str) -> bool:
		result = False
		revised_pointers = []
		self.pointers.append(self.root)
		for pointer in self.pointers:
			if letter in pointer.children:
				node = pointer.children[letter]
				if node.leaf:
					result |= True
				revised_pointers.append(node)
		self.pointers = revised_pointers
		return result


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.waiting = []
		for word in words:
			self.root.insert(word)

	def query(self, letter: str) -> bool:
		temp = []
		for node in self.waiting + [self.root]:
			if letter in node.child:
				temp.append(node.child[letter])
		self.waiting = temp
		for node in self.waiting:
			if node.is_complete:
				return True
		return False

class TrieNode:

	def __init__(self):
		self.child = {}
		self.is_complete = False

	def insert(self, word):
		node = self
		for letter in word:
			if letter not in node.child:
				new_node = TrieNode()
				node.child[letter] = new_node
			node = node.child[letter]
		node.is_complete = True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			root = self.trie
			for ch in word:
				if ch not in root:
					root[ch] = {}
				root = root[ch]
			root['*'] = True
		self.storech = []

	def query(self, letter: str) -> bool:
		a = False
		storech = []
		if letter in self.trie:
			storech.append(self.trie[letter])
		for ele in self.storech:
			if letter in ele:
				storech.append(ele[letter])
		self.storech = storech
		return any(('*' in ele for ele in self.storech))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.d = defaultdict(dict)
		for word in words:
			word = word[::-1]
			d = self.d
			for c in word[:-1]:
				d[c] = d.get(c) or defaultdict(dict)
				d = d[c]
			c = word[-1]
			if c not in d:
				d[c] = {'0': 1}
			else:
				d[c]['0'] = 1
			self.queue = []

	def query(self, letter: str) -> bool:
		self.queue.append(letter)
		d = self.d
		for c in self.queue[::-1]:
			if c in d:
				d = d[c]
				if '0' in d:
					return True
			else:
				break
		return False


class Trie:
	_end = '_end_'

	def __init__(self, words):
		self.root = dict()
		self.where = [self.root]
		self.delete = set()
		for word in words:
			current_dict = self.root
			for letter in word:
				current_dict = current_dict.setdefault(letter, {})
			current_dict[Trie._end] = Trie._end

	def next(self, letter):
		ret = False
		self.where.append(self.root)
		for (i, where) in enumerate(self.where):
			if letter not in where:
				self.delete.add(i)
			else:
				self.where[i] = where[letter]
				if Trie._end in where[letter]:
					ret = True
		for i in reversed(sorted(self.delete)):
			del self.where[i]
		self.delete = set()
		return ret

	def to_delete(self):
		return self.delete

	def has(self, word):
		current_dict = self.root
		for letter in word:
			if letter not in current_dict:
				return False
			current_dict = current_dict[letter]
		return Trie._end in current_dict

	def add(self, word):
		current_dict = self.root
		for letter in word:
			current_dict = current_dict.setdefault(letter, {})
		current_dict[Trie._end] = Trie._end

class StreamChecker:

	def __init__(self, words):
		self.Trie = Trie(words)

	def query(self, letter):
		return self.Trie.next(letter)


class StreamChecker:

	class TrieNode:

		def __init__(self):
			self.children = {}
			self.terminal = False

	def __init__(self, words: List[str]):
		self.array = []
		self.root = self.TrieNode()
		for word in words:
			node = self.root
			for char in word[::-1]:
				if char not in node.children:
					node.children[char] = self.TrieNode()
				node = node.children[char]
			node.terminal = True

	def query(self, letter: str) -> bool:
		self.array.append(letter)
		node = self.root
		array = self.array[::-1]
		for char in array:
			if char in node.children:
				node = node.children[char]
				if node.terminal:
					return True
			else:
				return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for w in words:
			curr = self.root
			for ch in w:
				if ch not in curr:
					curr[ch] = {}
				curr = curr[ch]
			curr[0] = None
		self.cursors = []

	def query(self, letter: str) -> bool:
		self.cursors.append(self.root)
		new_cursors = []
		res = False
		for c in self.cursors:
			if letter in c:
				if 0 in c[letter]:
					res = True
				new_cursors.append(c[letter])
		self.cursors = new_cursors
		return res


class Tree:

	def __init__(self, val=''):
		self.val = val
		self.isTerminal = False
		self.subs = dict()

class StreamChecker:

	def __init__(self, words: List[str]):
		self.possibleNodes = list()
		self.root = Tree()
		for word in words:
			traverser = self.root
			for char in word:
				if char in traverser.subs:
					traverser = traverser.subs[char]
				else:
					node = Tree(char)
					traverser.subs[char] = node
					traverser = node
			traverser.isTerminal = True

	def query(self, letter: str) -> bool:
		newPossibleNodes = []
		if letter in self.root.subs:
			newPossibleNodes.append(self.root.subs[letter])
		for node in self.possibleNodes:
			if letter in node.subs:
				newPossibleNodes.append(node.subs[letter])
		self.possibleNodes = newPossibleNodes
		for node in newPossibleNodes:
			if node.isTerminal:
				return True
		return False


class WordNode:

	def __init__(self):
		self.children = [None] * 26
		self.isFront = False

class WordTrie:

	def __init__(self):
		self.head = WordNode()

	def addWord(self, word):
		node = self.head
		for c in word[::-1]:
			if not node.children[ord(c) - ord('a')]:
				node.children[ord(c) - ord('a')] = WordNode()
			node = node.children[ord(c) - ord('a')]
		node.isFront = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = WordTrie()
		for w in words:
			self.Trie.addWord(w)
		self.queries = []

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		node = self.Trie.head
		for l in self.queries[::-1]:
			if not node.children[ord(l) - ord('a')]:
				return False
			node = node.children[ord(l) - ord('a')]
			if node.isFront:
				return True
		return False


class trie:

	def __init__(self, val=None):
		self.val = val
		self.next = {}
		self.is_complete = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = trie()
		for w in words:
			node = self.root
			for c in w[::-1]:
				if c not in node.__next__:
					node.next[c] = trie(c)
				node = node.next[c]
			node.is_complete = True
		self.queries = []

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		node = self.root
		for c in self.queries[::-1]:
			if c not in node.__next__:
				return False
			node = node.next[c]
			if node.is_complete:
				return True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.possible = []
		for word in words:
			t = self.trie
			for c in word:
				if c not in t:
					t[c] = {}
				t = t[c]
			t['#'] = '#'

	def query(self, letter: str) -> bool:
		temp = []
		if letter in self.trie:
			temp.append(self.trie[letter])
		for d in self.possible:
			if letter in d:
				temp.append(d[letter])
		self.possible = temp
		return any(('#' in item for item in self.possible))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			cur = self.trie
			for chrs in word:
				if chrs not in cur:
					cur[chrs] = {}
				cur = cur[chrs]
			cur['#'] = True
		self.stream = collections.deque()
		print(self.trie)

	def query(self, letter: str) -> bool:
		temp = collections.deque()
		self.stream.append(self.trie)
		for p in self.stream:
			if letter in p:
				temp.append(p[letter])
		self.stream = temp
		for p in self.stream:
			if '#' in p:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = Tree('0', [])
		for item in words:
			curr = self.tree
			for letter in item[::-1]:
				result = curr.in_list(letter)
				if result is None:
					curr.subtrees.append(Tree(letter, []))
					curr = curr.subtrees[-1]
				else:
					curr = curr.subtrees[result]
			curr.food = True
		self.q = ''

	def query(self, letter: str) -> bool:
		self.q += letter
		reverse_q = self.q[::-1]
		curr = self.tree
		for letter in reverse_q:
			result = curr.in_list(letter)
			if result is None:
				return False
			else:
				curr = curr.subtrees[result]
				if curr.food:
					return True
		return False

class Tree:

	def __init__(self, root, subtrees: List):
		self.root = root
		self.food = False
		self.subtrees = subtrees

	def in_list(self, letter: str):
		for (i, item) in enumerate(self.subtrees):
			if item.root == letter:
				return i
		return None


class TrieNode:

	def __init__(self):
		self.letters = {}
		self.word = False

	def trie(self, words):
		root = TrieNode()
		for word in words:
			current = root
			for letter in word:
				if letter not in current.letters:
					current.letters[letter] = TrieNode()
				current = current.letters[letter]
			current.word = True
		return root

class StreamChecker:

	def __init__(self, words: List[str]):
		trie = TrieNode()
		self.root = trie.trie(words)
		self.left = [self.root]

	def query(self, letter: str) -> bool:
		right = [self.root]
		finded = False
		for trie in self.left:
			if letter in trie.letters:
				tmp = trie.letters[letter]
				right += [tmp]
				if tmp.word:
					finded = True
		self.left = right
		return finded


class StreamChecker:

	def __init__(self, words: List[str]):
		self.data = {}
		for word in words:
			self._add_word(word)
		self.q = []

	def _add_word(self, word):
		word += '$'
		d = self.data
		for char in word:
			if char not in d:
				d[char] = {}
			d = d[char]

	def query(self, letter: str) -> bool:
		new_q = []
		if letter in self.data:
			new_q.append(self.data[letter])
		for acc in self.q:
			if letter in acc:
				new_q.append(acc[letter])
		self.q = new_q
		for acc in self.q:
			if '$' in acc:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.nodes = []
		for word in words:
			node = self.trie
			for char in word:
				if char not in node:
					node[char] = {}
				node = node[char]
			node['end'] = True

	def query(self, letter: str) -> bool:
		self.nodes.append(self.trie)
		temp = False
		new_nodes = []
		for node in self.nodes:
			if letter in node:
				node = node[letter]
				if 'end' in node:
					temp = True
				new_nodes.append(node)
		self.nodes = new_nodes
		return temp


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for w in words:
			node = self.trie
			for char in w:
				node.setdefault(char, {})
				node = node[char]
			node.setdefault('*', w)
		self.curr = deque([self.trie])

	def query(self, letter: str) -> bool:
		ret = False
		for _ in range(len(self.curr)):
			node = self.curr.popleft()
			if letter in node:
				node = node[letter]
				self.curr.append(node)
				if '*' in node:
					ret = True
		self.curr.append(self.trie)
		return ret


def to_index(c):
	return ord(c) - ord('a')

class Node:

	def __init__(self, val=None):
		self.val = val
		self.end = False
		self.childs = [None] * (ord('z') - ord('a') + 1)

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, word: str) -> None:
		ptr = self.root
		for c in word:
			i = to_index(c)
			if ptr.childs[i] is None:
				node = Node(c)
				ptr.childs[i] = node
			ptr = ptr.childs[i]
		ptr.end = True

	def search(self, word: str) -> bool:
		ptr = self.root
		for c in word:
			i = to_index(c)
			if ptr.childs[i] is None:
				return False
			ptr = ptr.childs[i]
		return ptr.end == True

	def startsWith(self, prefix: str) -> bool:
		ptr = self.root
		for c in prefix:
			i = to_index(c)
			if ptr.childs[i] is None:
				return False
			ptr = ptr.childs[i]
		return True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		for w in words:
			self.t.insert(w)
		self.d = set()
		self.n = 2000
		self.k = deque()
		self.store = {}

	def query(self, letter: str) -> bool:
		r = False
		if len(self.k) == self.n:
			self.k.popleft()
		i = to_index(letter)
		self.k.append(i)
		v = self.store.get(tuple(self.k), None)
		if v is not None:
			(self.d, r) = v
			return r
		new_d = set()
		for node in self.d:
			if node.childs[i] is not None:
				new_d.add(node.childs[i])
				if node.childs[i].end:
					r = True
		if self.t.root.childs[i] is not None:
			new_d.add(self.t.root.childs[i])
			if self.t.root.childs[i].end:
				r = True
		self.d = tuple(new_d)
		self.store[tuple(self.k)] = (self.d, r)
		return r


class Node:

	def __init__(self):
		self.c = collections.defaultdict(Node)
		self.e = 0

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Node()
		for w in words:
			temp = self.root
			for i in w:
				temp = temp.c[i]
			temp.e = 1
		self.w = []

	def query(self, letter: str) -> bool:
		self.w = [i.c[letter] for i in self.w + [self.root] if letter in i.c]
		return any((i.e for i in self.w))


class TrieNode:

	def __init__(self):
		self.child = defaultdict(TrieNode)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.waitlist = []
		for word in words:
			node = self.root
			for ch in word:
				node = node.child[ch]
			node.child['#'] = defaultdict(TrieNode)

	def query(self, letter: str) -> bool:
		waitlist = []
		now = self.root
		if letter in now.child:
			waitlist.append(now.child[letter])
		for node in self.waitlist:
			if letter in node.child:
				waitlist.append(node.child[letter])
		self.waitlist = waitlist
		for node in waitlist:
			if '#' in node.child:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.points = [self.trie]
		for word in words:
			cur = self.trie
			for w in word:
				if not w in cur:
					cur[w] = {}
				cur = cur[w]
			cur['#'] = None

	def query(self, letter: str) -> bool:
		temp = [self.trie]
		ret = False
		for p in self.points:
			if letter in p:
				p = p[letter]
				if '#' in p:
					ret = True
				temp.append(p)
		self.points = temp
		return ret


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			temp = self.trie
			for char in word:
				if char not in temp:
					temp[char] = {}
				temp = temp[char]
			temp['*'] = True
		self.pointer = deque()

	def query(self, letter: str) -> bool:
		temp = self.trie
		self.pointer.append(temp)
		length = len(self.pointer)
		flag = False
		for i in range(length):
			node = self.pointer.popleft()
			if letter in node:
				node = node[letter]
				self.pointer.append(node)
				if '*' in node:
					flag = True
		return flag


class TrieNode:

	def __init__(self):
		self.neighbors = defaultdict(TrieNode)
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		for word in words:
			node = self.root
			for c in word:
				node = node.neighbors[c]
			node.isWord = True
		self.nodes = []

	def query(self, letter: str) -> bool:
		isWord = False
		newNodes = []
		self.nodes.append(self.root)
		for node in self.nodes:
			if letter in node.neighbors:
				temp = node.neighbors[letter]
				isWord |= temp.isWord
				newNodes.append(temp)
		self.nodes = newNodes
		return isWord


class TRIE:

	def __init__(self, next_letters=None, flag=False):
		if not next_letters:
			self.next_letters = {}
		else:
			self.next_letters = next_letters
		self.flag = flag

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TRIE()
		for word in words:
			curr_node = self.root
			for l in word:
				if l not in curr_node.next_letters:
					new_node = TRIE()
					curr_node.next_letters[l] = new_node
					curr_node = new_node
				else:
					curr_node = curr_node.next_letters[l]
			curr_node.flag = True
		self.curr_nodes = set([self.root])

	def query(self, letter: str) -> bool:
		found = False
		new_curr_nodes = set([self.root])
		for node in self.curr_nodes:
			if letter in node.next_letters:
				curr_node = node.next_letters[letter]
				new_curr_nodes.add(curr_node)
				if curr_node.flag:
					found = True
		self.curr_nodes = new_curr_nodes
		return found


class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_end = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def addWord(self, word):
		curr = self.root
		for ch in word:
			if ch not in curr.children:
				new_node = TrieNode()
				curr.children[ch] = new_node
			curr = curr.children[ch]
		curr.is_end = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.addWord(reversed(word))
		self.queue = collections.deque()

	def query(self, letter: str) -> bool:
		self.queue.appendleft(letter)
		curr = self.trie.root
		for ch in self.queue:
			if ch not in curr.children:
				return False
			if ch in curr.children:
				curr = curr.children[ch]
				if curr.is_end:
					return True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = set(words)
		self.words_trie = defaultdict(dict)
		for word in words:
			trie_node = self.words_trie[word[-1]]
			for letter in word[-2::-1]:
				next_node = trie_node.setdefault(letter, {})
				trie_node = next_node
			trie_node[None] = None
		print(self.words_trie)
		self.queries = []

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		trie_node = None
		for query_letter in self.queries[::-1]:
			if trie_node is None:
				trie_node = self.words_trie.get(query_letter, {})
			else:
				trie_node = trie_node.get(query_letter, {})
			if not trie_node:
				break
			elif None in trie_node:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add(word)
		self.consideration = []

	def query(self, letter: str) -> bool:
		self.consideration = [node.words[letter] for node in self.consideration + [self.trie] if letter in node.words]
		return any((node.is_word for node in self.consideration))

class Trie:

	def __init__(self):
		self.is_word = False
		self.prefix = ''
		self.words = {}

	def add(self, word):
		if not word:
			self.is_word = True
			return
		if word[0] in self.words:
			self.words[word[0]].add(word[1:])
		else:
			n = Trie()
			n.prefix = self.prefix + word[0]
			self.words[word[0]] = n
			self.words[word[0]].add(word[1:])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			self.add_word_to_trie(self.trie, word, 0)
		self.pointers = {}
		self.q_no = 0

	def query(self, letter: str) -> bool:
		self.q_no += 1
		self.pointers[self.q_no] = self.trie
		result = False
		to_delete = []
		for (q_no, pointer) in list(self.pointers.items()):
			if letter not in pointer:
				to_delete.append(q_no)
				continue
			self.pointers[q_no] = pointer[letter]
			result = result or 1 in self.pointers[q_no]
		for del_c in to_delete:
			del self.pointers[del_c]
		return result

	def add_word_to_trie(self, node, word, i):
		if i == len(word):
			node[1] = True
			return
		if word[i] not in node:
			node[word[i]] = {}
		self.add_word_to_trie(node[word[i]], word, i + 1)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			d = self.trie
			for c in word:
				if c not in d:
					d[c] = {}
				d = d[c]
			d['*'] = True
		self.open_words = []

	def query(self, letter: str) -> bool:
		found = False
		new_open_words = []
		if letter in self.trie:
			new_open_words.append(self.trie[letter])
			if '*' in self.trie[letter]:
				found = True
		for word in self.open_words:
			if letter in word:
				if '*' in word[letter]:
					found = True
				new_open_words.append(word[letter])
		self.open_words = new_open_words
		return found


class TrieNode:

	def __init__(self):
		self.word = False
		self.children = {}

class Trie:

	def __init__(self):
		self.head = TrieNode()
		self.head.word = True

	def insert(self, word):
		itr = self.head
		for c in word:
			if c not in itr.children:
				itr.children[c] = TrieNode()
			itr = itr.children[c]
		itr.word = True

	def contains(self, word):
		itr = self.head
		for c in word:
			if c in itr.children:
				itr = itr.children[c]
				if itr.word:
					return True
			else:
				return False
		return itr.word

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.word = []

	def query(self, letter: str) -> bool:
		self.word.append(letter)
		return self.trie.contains(self.word[::-1])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.dic = {}
		for w in words:
			dic = self.dic
			for c in w[::-1]:
				if not c in dic:
					dic[c] = {}
				dic = dic[c]
			dic['#'] = True
		self.stack = []

	def query(self, letter: str) -> bool:
		self.stack.append(letter)
		dic = self.dic
		for c in self.stack[::-1]:
			if '#' in dic:
				return True
			if c not in dic:
				return False
			dic = dic[c]
		return dic.get('#', False)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			node = self.trie
			for c in word:
				try:
					node = node[c]
				except KeyError:
					n = node[c] = {}
					node = n
			node['$'] = None
		self.index = 0
		self.nodes = {}

	def query(self, letter: str) -> bool:
		nodes = {}
		self.nodes[self.index] = self.trie
		for (i, node) in list(self.nodes.items()):
			try:
				nodes[i] = node[letter]
			except KeyError:
				pass
		self.nodes = nodes
		self.index += 1
		return any(('$' in node for node in list(nodes.values())))


from typing import List
root = '@'
marker = '$'

class TrieNode:

	def __init__(self, letter):
		self.letter = letter
		self.children = dict()

class StreamChecker:

	def __init__(self, words: List[str]):
		self.spelt = False
		self.root = TrieNode(root)
		self.pointers = [self.root]
		for word in words:
			self.trie_insert(word)

	def trie_insert(self, word: str):
		current = self.root
		for letter in word:
			if letter in current.children:
				current = current.children[letter]
			else:
				letter_node = TrieNode(letter)
				current.children[letter] = letter_node
				current = letter_node
		current.children[marker] = TrieNode(marker)

	def query(self, letter: str) -> bool:
		new_ptrs = []
		self.spelt = False
		for ptr in self.pointers:
			if letter in ptr.children:
				advanced_ptr = ptr.children[letter]
				if marker in advanced_ptr.children:
					self.spelt = True
				new_ptrs.append(advanced_ptr)
		self.pointers = new_ptrs + [self.root]
		return self.spelt


class Node:

	def __init__(self, x):
		self.val = x
		self.next = {}
		self.word = False

class Trie:

	def __init__(self):
		self.root = Node(None)

	def add(self, word):
		root = self.root
		for w in word:
			if w not in root.__next__:
				node = Node(w)
				root.next[w] = node
			root = root.next[w]
		root.word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.key = {}
		self.pre = set([])
		for i in words:
			self.trie.add(i)

	def query(self, letter: str) -> bool:
		root = self.trie.root
		temp = set([])
		ans = False
		for i in self.pre:
			if letter in i.__next__:
				node = i.next[letter]
				if node.word:
					ans = True
				temp.add(node)
		if letter in root.__next__:
			node = root.next[letter]
			temp.add(node)
			if node.word:
				ans = True
		self.pre = temp
		return ans


class Node:

	def __init__(self, val):
		self.val = val
		self.children = {}
		self.end = False

class WordTree:

	def __init__(self):
		self.root = Node(0)

	def add_word(self, word):
		cur = self.root
		for c in word[::-1]:
			if c in cur.children:
				cur = cur.children[c]
			else:
				cur.children[c] = Node(c)
				cur = cur.children[c]
		cur.end = True

class StreamChecker:

	def __init__(self, words):
		self.tree = WordTree()
		for word in words:
			self.tree.add_word(word)
		self.query_l = []

	def query(self, letter: str) -> bool:
		self.query_l.append(letter)
		cur = self.tree.root
		for c in self.query_l[::-1]:
			if c not in cur.children:
				return False
			cur = cur.children[c]
			if cur.end:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.p = []
		for w in words:
			self.insert(w)

	def insert(self, w):
		p = self.trie
		for c in w:
			if c in p:
				p = p[c]
			else:
				p[c] = {}
				p = p[c]
		p['#'] = ''

	def query(self, letter: str) -> bool:
		self.p.append(self.trie)
		p2 = []
		for p in self.p:
			if letter in p:
				p2.append(p[letter])
		self.p = p2
		return any(('#' in p for p in self.p))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.dic = {}
		for i in words:
			cur = self.dic
			for j in i[::-1]:
				if j not in cur:
					cur[j] = {}
					cur = cur[j]
				else:
					cur = cur[j]
			cur['#'] = {}
		self.res = []

	def query(self, letter: str) -> bool:
		self.res.append(letter)
		cur = self.dic
		for i in self.res[::-1]:
			if i not in cur:
				return False
			if '#' in cur[i]:
				return True
			if i in cur:
				cur = cur[i]
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = self.create_trie(words)
		self.l = []

	def create_trie(self, words):
		trie = {}
		for word in words:
			curr = trie
			for ch in word:
				if ch not in curr:
					curr[ch] = {}
				curr = curr[ch]
			curr['.'] = True
		return trie

	def query(self, letter: str) -> bool:
		self.l.append(self.trie)
		self.l = [trie[letter] for trie in self.l if letter in trie]
		return any(('.' in trie for trie in self.l))


class TrieNode:

	def __init__(self, eow):
		self.children = {}
		self.eow = eow

class Trie:

	def __init__(self):
		self.root = TrieNode(False)

	def add(self, word):
		cur = self.root
		for ch in word:
			if ch not in cur.children:
				cur.children[ch] = TrieNode(False)
			cur = cur.children[ch]
		cur.eow = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add(word)
		self.trie_curs = [self.trie.root]

	def query(self, letter: str) -> bool:
		trie_curs_new = [self.trie.root]
		hit = False
		for cur in self.trie_curs:
			if letter in cur.children:
				trie_curs_new.append(cur.children[letter])
				if not hit and cur.children[letter].eow:
					hit = True
		self.trie_curs = trie_curs_new
		return hit


class StreamChecker:

	def __init__(self, words: List[str]):
		self.memo = defaultdict(dict)
		self.words = set()
		words_ = [word[::-1] for word in set(words)]
		for word in words_:
			for i in range(len(word) - 1):
				self.memo[i + 1][word[:i + 1]] = 0
		for word in words_:
			self.memo[len(word)][word] = 1
		self.hist = []

	def query(self, letter: str) -> bool:
		self.hist.append(letter)
		w = ''
		for i in range(len(self.hist) - 1, -1, -1):
			w += self.hist[i]
			c = self.memo[len(w)].get(w, None)
			if c is None:
				return False
			if c:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.flag = []
		self.trie = {}
		for word in words:
			cur = self.trie
			for c in word:
				if c not in cur:
					cur[c] = {}
				cur = cur[c]
			cur['.'] = {}

	def query(self, letter: str) -> bool:
		if letter in self.trie:
			self.flag.append(self.trie)
		res = False
		new_flag = []
		for f in self.flag:
			if letter in f:
				new_flag.append(f[letter])
				if '.' in f[letter]:
					res = True
		self.flag = new_flag
		return res


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = self.buildTrie(words)
		self.nodes = [self.trie]

	def buildTrie(self, words):
		trie = {}
		for word in words:
			curr = trie
			for char in word:
				if char not in curr:
					curr[char] = {}
				curr = curr[char]
			curr['isWord'] = True
		return trie

	def query(self, letter: str) -> bool:
		found = False
		newNodes = [self.trie]
		for (i, node) in enumerate(self.nodes):
			if letter in node:
				if 'isWord' in node[letter]:
					found = True
					if len(list(node[letter].keys())) > 1:
						newNodes.append(node[letter])
				else:
					newNodes.append(node[letter])
		self.nodes = newNodes
		return found


class Trie:

	def __init__(self):
		self.chars = {}
		self.isEnd = False
		self.current = self
		self.pointers = set([self])

	def add(self, word):
		trie = self
		for c in word:
			if c not in trie.chars:
				trie.chars[c] = Trie()
			trie = trie.chars[c]
		trie.isEnd = True

	def searchNext(self, char):
		newPointers = set([self])
		isWord = False
		for pointer in self.pointers:
			if char in pointer.chars:
				newPointer = pointer.chars[char]
				if newPointer.isEnd:
					isWord = True
				newPointers.add(newPointer)
		self.pointers = newPointers
		return isWord

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add(word)

	def query(self, letter: str) -> bool:
		return self.trie.searchNext(letter)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			cur = self.trie
			for char in word:
				if char not in cur:
					cur[char] = {}
				cur = cur[char]
			cur['#'] = {}
		self.candidates = deque()

	def query(self, letter: str) -> bool:
		size = len(self.candidates)
		for i in range(size):
			candidate = self.candidates.popleft()
			if letter in candidate:
				self.candidates.append(candidate[letter])
		if letter in self.trie:
			self.candidates.append(self.trie[letter])
		for candidate in self.candidates:
			if '#' in candidate:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.suffixTree = {}
		for word in set(words):
			node = self.suffixTree
			for ch in word[::-1]:
				if not ch in node:
					node[ch] = {}
				node = node[ch]
			node['$'] = True
		self.queries = []
		return

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		node = self.suffixTree
		for ch in self.queries[::-1]:
			if '$' in node:
				return True
			if not ch in node:
				return False
			else:
				node = node[ch]
		return '$' in node


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add(self, word):
		p = self.root
		for c in word:
			if c not in p.children:
				p.children[c] = TrieNode()
			p = p.children[c]
		p.isWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.queries = []
		self.trie = Trie()
		for word in words:
			self.trie.add(word[::-1])

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		p = self.trie.root
		for i in range(len(self.queries) - 1, -1, -1):
			c = self.queries[i]
			if c not in p.children:
				return False
			p = p.children[c]
			if p.isWord:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = dict()
		self.input = ''
		for word in words:
			ptr = self.trie
			for w in word[::-1]:
				if w not in list(ptr.keys()):
					ptr[w] = dict()
				ptr = ptr[w]
			ptr['$'] = word

	def query(self, letter: str) -> bool:
		self.input += letter
		node = self.trie
		for index in range(len(self.input)):
			ch = self.input[-(index + 1)]
			if '$' in list(node.keys()):
				return True
			elif ch not in list(node.keys()):
				return False
			node = node[ch]
		return '$' in list(node.keys())


class TrieNode:

	def __init__(self, c):
		self.c = c
		self.children = {}
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.char_list = []
		self.trie = TrieNode('')
		for word in words:
			node = self.trie
			for c in word[::-1]:
				if c not in node.children:
					node.children[c] = TrieNode(c)
				node = node.children[c]
			node.is_word = True

	def query(self, letter: str) -> bool:
		self.char_list.append(letter)
		i = len(self.char_list) - 1
		node = self.trie
		while i >= 0:
			if self.char_list[i] not in node.children:
				break
			node = node.children[self.char_list[i]]
			if node.is_word:
				return True
			i -= 1
		return node.is_word


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isend_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		curr = self.root
		for c in word:
			if c not in curr.children:
				curr.children[c] = TrieNode()
			curr = curr.children[c]
		curr.isend_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.search = []
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.search.append(letter)
		curr = self.trie.root
		for i in range(len(self.search) - 1, -1, -1):
			char = self.search[i]
			if curr.isend_word:
				return True
			if char not in curr.children:
				return False
			curr = curr.children[char]
		return curr.isend_word


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.isEnd = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		node = self.root
		for char in word:
			node = node.children[char]
		node.isEnd = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.letters = []
		self.trie = Trie()
		for w in words:
			self.trie.insert(w[::-1])

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		i = len(self.letters) - 1
		node = self.trie.root
		while i >= 0:
			if node.isEnd:
				return True
			if self.letters[i] not in node.children:
				return False
			node = node.children[self.letters[i]]
			i -= 1
		return node.isEnd


class TrieNode:

	def __init__(self, letter):
		self.letter = letter
		self.next_alpha = {}
		self.end = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode('root')
		self.stream = deque()
		self.p = None
		for word in words:
			buffer = TrieNode('a')
			if len(word) == 1:
				self.root.next_alpha[word[0]] = TrieNode(word[0])
				self.root.next_alpha[word[0]].end = True
				continue
			for (letter_ind, l) in enumerate(word[::-1]):
				if letter_ind == 0:
					if l not in list(self.root.next_alpha.keys()):
						self.root.next_alpha[l] = TrieNode(l)
					buffer = self.root.next_alpha[l]
					continue
				if l in list(buffer.next_alpha.keys()):
					buffer = buffer.next_alpha[l]
				else:
					nex = TrieNode(l)
					buffer.next_alpha[l] = nex
					buffer = nex
				if letter_ind == len(word) - 1:
					buffer.end = True

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		cur = self.root
		for c in self.stream:
			if c in cur.next_alpha:
				cur = cur.next_alpha[c]
				if cur.end:
					return True
			else:
				break
		return False


class StreamChecker:

	def __init__(self, words):
		self.words = []
		self.store = {}
		self.stack = ''
		self.sample = 25
		for w in words:
			if not w:
				continue
			self.insertStore(w[::-1], self.store)

	def query(self, letter):
		self.stack += letter
		return self.queryStore(self.stack, self.store)

	def insertStore(self, w, store):
		if w[:self.sample] not in store:
			store[w[:self.sample]] = {}
		if len(w) <= self.sample:
			store[w[:self.sample]]['!'] = None
		else:
			self.insertStore(w[self.sample:], store[w[:self.sample]])

	def queryStore(self, stack, store):
		if not stack:
			return False
		s = stack[:-self.sample - 1:-1]
		if s in store:
			if '!' in store[s]:
				return True
			if self.queryStore(stack[:-self.sample], store[s]):
				return True
		for i in range(1, self.sample):
			if s[:i] in store:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.stream = deque([])
		for word in set(words):
			node = self.trie
			for ch in word[::-1]:
				if not ch in node:
					node[ch] = {}
				node = node[ch]
			node['$'] = word

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.trie
		for ch in self.stream:
			if '$' in node:
				return True
			if not ch in node:
				return False
			node = node[ch]
		return '$' in node

class StreamChecker_TLE:

	def _reinit(self):
		newdict = collections.defaultdict(list)
		for w in self.words:
			newdict[w[0]].append((w, 0))
		return newdict

	def __init__(self, words: List[str]):
		self.words = words
		self.curdict = self._reinit()
		self.refdict = self._reinit()

	def query(self, letter: str) -> bool:
		if not self.curdict[letter]:
			self.curdict = self._reinit()
			return False
		expect = self.curdict[letter][:]
		self.curdict = self._reinit()
		ret = False
		for (w, idx) in expect:
			idx += 1
			if idx == len(w):
				ret = True
			else:
				nxtch = w[idx]
				self.curdict[nxtch].append((w, idx))
		return ret


class TrieNode:

	def __init__(self):
		(self.children, self.end_node) = ({}, 0)

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for symbol in word:
			root = root.children.setdefault(symbol, TrieNode())
		root.end_node = 1

class StreamChecker:

	def __init__(self, words):
		self.trie = Trie()
		self.Stream = deque()
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter):
		self.Stream.appendleft(letter)
		cur = self.trie.root
		for c in self.Stream:
			if c in cur.children:
				cur = cur.children[c]
				if cur.end_node:
					return True
			else:
				break
		return False


from collections import defaultdict
from collections import deque

class TrieNode:

	def __init__(self):
		self.is_word = False
		self.children = defaultdict(TrieNode)

class Trie:

	def __init__(self, words=None):
		self.root = TrieNode()
		if words:
			for word in words:
				self.insert(word)

	def insert(self, word):
		curr = self.root
		for char in word:
			curr = curr.children[char]
		curr.is_word = True

	def search(self, word):
		curr = self.root
		for char in word:
			if curr.children.get(char, None) is None:
				return False
			curr = curr.children[char]
		return curr.is_word

	def starts_with(self, prefix):
		curr = self.root
		for char in prefix:
			if curr.children.get(char, None) is None:
				return False
			curr = curr.children[char]
		return True

	def contains_stream(self, stream, index=0):
		curr = self.root
		while index < len(stream):
			if curr.is_word:
				return True
			char = stream[index]
			if curr.children.get(char, None) is None:
				return False
			curr = curr.children[char]
			index += 1
		return curr.is_word

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.trie.contains_stream(self.stream)


class TrieNode:

	def __init__(self):
		self.end = False
		self.children = [None] * 26

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.cand = []
		self.maxlen = float('-inf')
		for w in words:
			self.insert(w)
			self.maxlen = max(self.maxlen, len(w))

	def insert(self, s):
		node = self.root
		for c in s[::-1]:
			index = ord(c) - ord('a')
			if node.children[index] is None:
				node.children[index] = TrieNode()
			node = node.children[index]
		node.end = True

	def query(self, letter: str) -> bool:
		self.cand.insert(0, letter)
		if len(self.cand) > self.maxlen:
			self.cand.pop()
		curr = self.root
		for c in self.cand:
			if curr.children[ord(c) - ord('a')] is not None:
				curr = curr.children[ord(c) - ord('a')]
				if curr.end:
					return True
			else:
				return False
		return False


from collections import deque

class TrieNode:

	def __init__(self):
		(self.children, self.end_node) = ({}, 0)

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for symbol in word:
			root = root.children.setdefault(symbol, TrieNode())
		root.end_node = 1

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.Stream = deque()
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.Stream.appendleft(letter)
		cur = self.trie.root
		for c in self.Stream:
			if c in cur.children:
				cur = cur.children[c]
				if cur.end_node == 1:
					return True
			else:
				break
		return False


from collections import deque

class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_leaf = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for c in word:
			root = root.children.setdefault(c, TrieNode())
		root.is_leaf = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = deque()
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		curr = self.trie.root
		for c in self.stream:
			if c in curr.children:
				curr = curr.children[c]
				if curr.is_leaf:
					return True
			else:
				return False


class TrieNode:

	def __init__(self):
		(self.children, self.end_node) = ({}, 0)

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for symbol in word:
			root = root.children.setdefault(symbol, TrieNode())
		root.end_node = 1

class StreamChecker:

	def __init__(self, words):
		self.trie = Trie()
		self.Stream = deque()
		self.maxLength = len(max(words, key=lambda x: len(x)))
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter):
		self.Stream.appendleft(letter)
		if len(self.Stream) > self.maxLength:
			self.Stream.pop()
		cur = self.trie.root
		for c in self.Stream:
			if c in cur.children:
				cur = cur.children[c]
				if cur.end_node:
					return True
			else:
				break
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isend = 0

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for char in word:
			root = root.children.setdefault(char, TrieNode())
		root.isend = 1

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = deque()
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		cur = self.trie.root
		for c in self.stream:
			if c in cur.children:
				cur = cur.children[c]
				if cur.isend:
					return True
			else:
				break
		return False


class TrieNode:

	def __init__(self):
		self.children = [None for _ in range(26)]
		self.end = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		cur = self.root
		for w in word:
			idx = ord(w) - ord('a')
			if not cur.children[idx]:
				cur.children[idx] = TrieNode()
			cur = cur.children[idx]
		cur.end = True

	def match(self, word):
		cur = self.root
		for w in word:
			if cur.end:
				return True
			idx = ord(w) - ord('a')
			if cur.children[idx]:
				cur = cur.children[idx]
			else:
				return cur.end
		return cur.end

class StreamChecker:

	def __init__(self, words: List[str]):
		self.cache = ''
		self.size = 0
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
			self.size = max(self.size, len(word))

	def query(self, letter: str) -> bool:
		self.cache += letter
		if len(self.cache) > self.size:
			self.cache = self.cache[1:]
		return self.trie.match(self.cache[::-1])


class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_leaf = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = TrieNode()
		self.root = self.trie
		self.buf = []
		for word in words:
			self.trie = self.root
			for i in range(len(word) - 1, -1, -1):
				ch = word[i]
				if ch not in self.trie.children:
					self.trie.children[ch] = TrieNode()
				self.trie = self.trie.children[ch]
			self.trie.is_leaf = True

	def query(self, letter: str) -> bool:
		self.buf.append(letter)
		self.trie = self.root
		for i in range(len(self.buf) - 1, -1, -1):
			if self.buf[i] in self.trie.children:
				self.trie = self.trie.children[self.buf[i]]
				if self.trie and self.trie.is_leaf:
					return True
			else:
				return False


class Trie:

	def __init__(self):
		self.child = {}

	def insert(self, word):
		cur_trie = self
		for char in word + '$':
			if char not in cur_trie.child:
				cur_trie.child[char] = Trie()
			cur_trie = cur_trie.child[char]

	def search(self, word):
		cur_trie = self
		for char in word + '$':
			if '$' in cur_trie.child:
				return True
			if char not in cur_trie.child:
				return False
			cur_trie = cur_trie.child[char]
		return False

class StreamChecker:

	def __init__(self, words):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.buffer = ''

	def query(self, letter: str) -> bool:
		self.buffer = letter + self.buffer
		return self.trie.search(self.buffer)


class TrieNode:

	def __init__(self):
		self.children = {}
		self.ends_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		node = self.root
		for c in word:
			node = node.children.setdefault(c, TrieNode())
		node.ends_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.deque = deque()
		for w in words:
			self.trie.insert(reversed(w))

	def query(self, letter: str) -> bool:
		self.deque.appendleft(letter)
		if len(self.deque) > 2000:
			self.deque.pop()
		node = self.trie.root
		for c in self.deque:
			node = node.children.get(c)
			if not node:
				return False
			if node.ends_word:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for c in word:
			root = root.children.setdefault(c, TrieNode())
		root.isWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		curr = self.trie.root
		for c in self.stream:
			if c in curr.children:
				curr = curr.children[c]
				if curr.isWord:
					return True
			else:
				break
		return False


from collections import defaultdict

class TrieNode:

	def __init__(self):
		self.links = dict()
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie_root = build_trie(words)
		self.queries = []

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		index = -1
		node = self.trie_root
		while node.links:
			if index < -len(self.queries):
				break
			letter = self.queries[index]
			if letter not in node.links:
				break
			index -= 1
			node = node.links[letter]
			if node.is_word:
				return True
		return False

def build_trie(words):
	root = TrieNode()
	for word in words:
		insert(root, word)
	return root

def insert(root, word):
	node = root
	for char in reversed(word):
		if char not in node.links:
			node.links[char] = TrieNode()
		node = node.links[char]
	node.is_word = True


class TreeNode:

	def __init__(self):
		self.children = {}
		self.isend = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TreeNode()
		self.char_so_far = deque()
		for word in words:
			node = self.root
			for char in word[::-1]:
				if char not in node.children:
					node.children[char] = TreeNode()
				node = node.children[char]
			node.isend = True

	def check_word(self, l):
		node = self.root
		for char in l:
			if char not in node.children:
				return False
			node = node.children[char]
			if node.isend:
				return True
		return node.isend

	def query(self, letter: str) -> bool:
		node = self.root
		self.char_so_far.appendleft(letter)
		return self.check_word(self.char_so_far)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.max_len = 0
		for word in words:
			pointer = self.trie
			for char in reversed(word):
				if char not in pointer:
					pointer[char] = {}
				pointer = pointer[char]
			pointer['&'] = True
			self.max_len = max(self.max_len, len(word))
		self.queue = deque()

	def query(self, letter: str) -> bool:
		self.queue.append(letter)
		if len(self.queue) > self.max_len:
			self.queue.popleft()
		pointer = self.trie
		for char in reversed(list(self.queue)):
			if '&' in pointer:
				return True
			if char in pointer:
				pointer = pointer[char]
			else:
				return False
		if '&' in pointer:
			return True
		else:
			return False


from collections import deque

class Trie:

	def __init__(self):
		self.children = [None] * 26
		self.end = False

	def insert(self, S):
		t = self
		for c in S:
			if not t.children[ord(c) - ord('a')]:
				t.children[ord(c) - ord('a')] = Trie()
			t = t.children[ord(c) - ord('a')]
		t.end = True

	def search(self, stream):
		t = self
		for c in stream:
			if not t.children[ord(c) - ord('a')]:
				return False
			t = t.children[ord(c) - ord('a')]
			if t.end:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		self.stream = deque()
		for word in words:
			self.t.insert(reversed(word))

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.t.search(self.stream)


from collections import deque

class TrieNode:

	def __init__(self, val):
		self.end = False
		self.children = {}

class Trie:

	def __init__(self):
		self.root = TrieNode(-1)

	def add(self, word):
		root = self.root
		for ch in word:
			children = root.children
			if children.get(ch) == None:
				child = TrieNode(ch)
				children[ch] = child
				root = child
			else:
				root = children[ch]
		root.end = True

	def search(self, word):
		root = self.root
		for val in word:
			if root.children.get(val) == None:
				return False
			else:
				root = root.children[val]
				if root.end:
					return True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = Trie()
		self.maxlen = -1
		for w in words:
			self.maxlen = max(self.maxlen, len(w))
			rev = w[::-1]
			self.Trie.add(rev)
		self.searchArr = deque()

	def query(self, letter: str) -> bool:
		self.searchArr.insert(0, letter)
		if len(self.searchArr) > self.maxlen:
			self.searchArr.pop()
		return self.Trie.search(self.searchArr)


class Node:

	def __init__(self):
		self.list = [None] * 26
		self.period = False

class Trie:

	def __init__(self):
		self.root = Node()

	def add(self, word):
		curr = self.root
		for i in range(len(word) - 1, -1, -1):
			letter = word[i]
			asc = ord(letter) - ord('a')
			if not curr.list[asc]:
				curr.list[asc] = Node()
			curr = curr.list[asc]
		curr.period = True

	def query(self, word):
		curr = self.root
		for i in range(len(word) - 1, -1, -1):
			c = word[i]
			asc = ord(c) - ord('a')
			if not curr.list[asc]:
				return False
			curr = curr.list[asc]
			if curr.period == True:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.query_list = []
		self.words = words
		for word in words:
			self.trie.add(word)

	def query(self, letter: str) -> bool:
		self.update_queries(letter)
		return self.trie.query(self.query_list)

	def update_queries(self, letter):
		self.query_list.append(letter)


class Trie:

	def __init__(self):
		self.prev = {}

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		self.maxlens = 0
		self.backw = ''
		for w in words:
			wn = len(w)
			if wn > self.maxlens:
				self.maxlens = wn
			wt = self.t
			for i in range(wn - 1, -1, -1):
				if w[i] not in wt.prev:
					wt.prev[w[i]] = Trie()
				wt = wt.prev[w[i]]
			wt.prev['$'] = '$'

	def query(self, letter: str) -> bool:
		if len(self.backw) == self.maxlens:
			self.backw = self.backw[1:]
		self.backw += letter
		wn = len(self.backw)
		wt = self.t
		for i in range(wn - 1, -1, -1):
			if self.backw[i] not in wt.prev:
				return False
			wt = wt.prev[self.backw[i]]
			if '$' in wt.prev:
				return True
		return '$' in wt.prev


class Trie:

	def __init__(self):
		self.next = {}
		self.end_here = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		for word in words:
			cur = self.root
			for c in word[::-1]:
				if c not in cur.__next__:
					cur.next[c] = Trie()
				cur = cur.next[c]
			cur.end_here = True
		self.k = 2000
		self.history = deque([])

	def query(self, letter: str) -> bool:
		self.history.append(letter)
		if len(self.history) > self.k:
			self.history.popleft()
		if letter not in self.root.__next__:
			return False
		n = len(self.history)
		cur = self.root
		for i in range(n):
			c = self.history[n - 1 - i]
			if c not in cur.__next__:
				return False
			cur = cur.next[c]
			if cur.end_here:
				return True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.memo = {}
		self.words = set()
		for word in words:
			word_ = word[::-1]
			for i in range(len(word)):
				self.memo[word_[:i + 1]] = 0
			self.words.add(word_)
		self.hist = []
		print(self.memo)

	def query(self, letter: str) -> bool:
		self.hist.append(letter)
		w = ''
		for i in range(len(self.hist) - 1, -1, -1):
			w += self.hist[i]
			if w not in self.memo:
				return False
			if w in self.words:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_complete = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		cur = self.root
		for char in word[::-1]:
			cur = cur.children.setdefault(char, TrieNode())
		cur.is_complete = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = deque()
		for word in words:
			self.trie.insert(word)

	def query(self, letter: str) -> bool:
		cur = self.trie.root
		self.stream.appendleft(letter)
		for s in self.stream:
			if s in list(cur.children.keys()):
				cur = cur.children[s]
				if cur.is_complete:
					return True
			else:
				break
		return False


class Trie:

	def __init__(self):
		self.children = [None] * 26
		self.end = False

	def insert(self, word):
		cur = self
		for letter in word:
			if cur.children[ord(letter) - ord('a')] == None:
				cur.children[ord(letter) - ord('a')] = Trie()
			cur = cur.children[ord(letter) - ord('a')]
		cur.end = True

	def search(self, s):
		cur = self
		for letter in s:
			if cur.children[ord(letter) - ord('a')] == None:
				return False
			cur = cur.children[ord(letter) - ord('a')]
			if cur.end == True:
				return True
		return False

class StreamChecker:
	from collections import deque

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.words = words
		self.q = deque()
		for word in self.words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.q.appendleft(letter)
		return self.trie.search(self.q)


class TrieNode:

	def __init__(self):
		self.child = {}
		self.isWord = False

class Tree:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		cur = self.root
		for w in word:
			if w not in cur.child:
				cur.child[w] = TrieNode()
			cur = cur.child[w]
		cur.isWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = Tree()
		for word in words:
			self.tree.insert(word[::-1])
		self.letter = []

	def query(self, letter: str) -> bool:
		self.letter.append(letter)
		cur = self.tree.root
		i = len(self.letter) - 1
		while i >= 0:
			if cur.isWord:
				return True
			if self.letter[i] not in cur.child:
				return False
			cur = cur.child[self.letter[i]]
			i -= 1
		return cur.isWord


class Trie:

	def __init__(self):
		self.endOfWord = False
		self.children = [None] * 26

	def insert(self, s):
		t = self
		for c in s:
			if t.children[ord(c) - ord('a')] == None:
				t.children[ord(c) - ord('a')] = Trie()
			t = t.children[ord(c) - ord('a')]
		t.endOfWord = True

	def search(self, s):
		t = self
		for c in s:
			if t.children[ord(c) - ord('a')] == None:
				return False
			t = t.children[ord(c) - ord('a')]
			if t.endOfWord:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		self.stream = collections.deque()
		for w in words:
			self.t.insert(reversed(w))

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.t.search(self.stream)


class Node:

	def __init__(self):
		self.children = {}
		self.isEnd = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Node()
		for w in words:
			if len(w) > 0:
				self.insert(w[::-1])
		self.queryStack = []

	def insert(self, word):
		root = self.trie
		self.insertWord(root, word, 0)

	def insertWord(self, curNode, word, index):
		if index >= len(word):
			curNode.isEnd = True
		else:
			curChar = word[index]
			if curChar not in curNode.children:
				newNode = Node()
				curNode.children[curChar] = newNode
			self.insertWord(curNode.children[curChar], word, index + 1)

	def findIfPresent(self, curNode, index):
		if curNode.isEnd:
			return True
		if index < 0:
			return False
		if self.queryStack[index] not in curNode.children:
			return False
		return self.findIfPresent(curNode.children[self.queryStack[index]], index - 1)

	def query(self, letter: str) -> bool:
		self.queryStack.append(letter)
		root = self.trie
		return self.findIfPresent(root, len(self.queryStack) - 1)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.stream = collections.deque()
		for word in words:
			node = self.root
			for ch in word[::-1]:
				if ch not in node:
					node[ch] = {}
				node = node[ch]
			node['#'] = {}

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.root
		for ch in self.stream:
			if ch in node:
				node = node[ch]
				if '#' in node:
					return True
			else:
				return False
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.curQuery = ''
		self.Trie = Trie()
		for word in words:
			newWord = word[::-1]
			self.Trie.insert(newWord)

	def query(self, letter: str) -> bool:
		self.curQuery = letter + self.curQuery
		curNode = self.Trie.root
		i = 0
		while curNode and i < len(self.curQuery):
			curLetter = self.curQuery[i]
			ind = ord(curLetter) - ord('a')
			curNode = curNode.children[ind]
			if not curNode:
				return False
			if curNode.isWord:
				return True
			i += 1
		return False

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, word):
		curNode = self.root
		for i in range(len(word)):
			ind = ord(word[i]) - ord('a')
			if curNode.children[ind]:
				curNode = curNode.children[ind]
			else:
				curNode.children[ind] = Node()
				curNode = curNode.children[ind]
		curNode.isWord = True
		return

class Node:

	def __init__(self):
		self.children = [None] * 26
		self.isWord = False


class TN:

	def __init__(self, eow=False):
		self.c = {}
		self.e = eow
		self.i = id(self)

	def add(self, s):
		if len(s) == 0:
			self.e = True
		else:
			c = s[0]
			if c not in self.c:
				self.c[c] = TN()
			self.c[c].add(s[1:])

	def search(self, w):
		t = self
		for c in w:
			if c not in t.c:
				return False
			else:
				t = t.c[c]
				if t.e:
					return True
		return False

	def __contains__(self, i):
		return i in self.c

	def p(self, s=0):
		for (k, v) in self.c.items():
			print(' ' * s, k, '*' if v.e else '', sep='')
			v.p(s + 1)

	def __str__(self):
		return str(self.c) + ('*' if self.e else '')

	def __repr__(self):
		return repr(self.c) + ('*' if self.e else '')

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TN()
		for w in words:
			self.root.add(w[::-1])
		self.s = []

	def query(self, letter: str) -> bool:
		self.s.append(letter)
		return self.root.search(reversed(self.s))


class TrieNode:

	def __init__(self):
		self.children = dict()
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add(self, word):
		root = self.root
		for char in reversed(word):
			child = root.children.get(char, TrieNode())
			root.children[char] = child
			root = child
		root.isWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.letters = []
		self.trie = Trie()
		for word in words:
			self.trie.add(word)

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		root = self.trie.root
		for char in reversed(self.letters):
			if char not in root.children:
				return False
			root = root.children[char]
			if root.isWord:
				return True
		return False


class Node:

	def __init__(self):
		self.children = {}
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.cache = Node()
		self.stream = []
		for word in words:
			root = self.cache
			for i in range(len(word) - 1, -1, -1):
				if word[i] in root.children:
					root = root.children.get(word[i])
				else:
					root.children[word[i]] = Node()
					root = root.children.get(word[i])
			root.is_word = True

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		root = self.cache
		for i in range(len(self.stream) - 1, -1, -1):
			if self.stream[i] not in root.children:
				return False
			root = root.children.get(self.stream[i])
			if root.is_word:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.cache = ''

	def query(self, letter: str) -> bool:
		self.cache = letter + self.cache
		return self.trie.find(self.cache)

class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		cur = self.root
		for c in word:
			cur.children.setdefault(c, TrieNode())
			cur = cur.children[c]
		cur.is_word = True

	def find(self, word):
		cur = self.root
		for c in word:
			if cur.is_word:
				return True
			if c not in cur.children:
				return False
			cur = cur.children[c]
		return cur.is_word


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isEnd = False

class Trie:

	def __init__(self, corpus):
		self.root = TrieNode()
		for word in corpus:
			self.add(word)

	def add(self, word):
		x = self.root
		for w in word:
			if w not in x.children:
				x.children[w] = TrieNode()
			x = x.children[w]
		x.isEnd = True

class StreamChecker:

	def __init__(self, words: List[str]):
		words = [x[::-1] for x in words]
		self.trie = Trie(words)
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.trie.root
		for w in self.stream:
			if node.isEnd:
				return True
			elif w not in node.children:
				return False
			else:
				node = node.children[w]
		return node.isEnd


class TrieNode:

	def __init__(self):
		self.children = {}
		self.end_node = 0

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word: str):
		root = self.root
		for w in word:
			root = root.children.setdefault(w, TrieNode())
		root.end_node = 1

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		self.stream = deque()
		for w in words:
			self.t.insert(w[::-1])

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		t = self.t.root
		for curr in self.stream:
			if curr in t.children:
				t = t.children[curr]
				if t.end_node:
					return True
			else:
				break
		return False


class node:

	def __init__(self):
		self.child = [None] * 26
		self.end = False

	def insert(self, word):
		cur = self
		for i in range(len(word)):
			index = self.indexof(word[i])
			if cur.child[index] == None:
				cur.child[index] = node()
			cur = cur.child[index]
		cur.end = True

	def search(self, word):
		cur = self
		for w in word:
			index = self.indexof(w)
			cur = cur.child[index]
			if cur != None:
				if cur.end == True:
					return True
			else:
				break
		return False

	def indexof(self, c):
		return ord(c) - ord('a')

class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = words
		self.root = node()
		self.stack = collections.deque()
		for w in words:
			self.root.insert(w[::-1])

	def query(self, letter: str) -> bool:
		self.stack.appendleft(letter)
		return self.root.search(self.stack)


from collections import deque, defaultdict as dd

class TrieNode:

	def __init__(self):
		self.children = dd(TrieNode)
		self.is_end = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.q = deque()
		self.root = TrieNode()
		for word in words:
			node = self.root
			for char in word[::-1]:
				node = node.children[char]
			node.is_end = True

	def query(self, letter: str) -> bool:
		self.q.appendleft(letter)
		node = self.root
		for char in self.q:
			if char not in node.children:
				break
			node = node.children[char]
			if node.is_end:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = words
		self.cur = ''
		self.trie = {}
		for word in words:
			word = word[::-1]
			tmp = self.trie
			for w in word:
				if w not in tmp:
					tmp[w] = dict()
					tmp = tmp[w]
				else:
					tmp = tmp[w]
			tmp['#'] = None

	def query(self, letter: str) -> bool:
		self.cur += letter
		point = self.trie
		for i in range(len(self.cur) - 1, -1, -1):
			t = self.cur[i]
			if '#' in point:
				return True
			if t not in point:
				return False
			point = point[t]
		if '#' in point:
			return True
		else:
			return False


from collections import deque

class TrieNode:

	def __init__(self):
		self.children = {}
		self.isEndWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def addWord(self, word):
		head = self.root
		for char in word:
			head = head.children.setdefault(char, TrieNode())
		head.isEndWord = True

class StreamChecker:

	def __init__(self, words):
		self.vocab = Trie()
		self.queue = deque()
		for word in words:
			self.vocab.addWord(word[::-1])

	def query(self, char):
		self.queue.appendleft(char)
		cur = self.vocab.root
		for c in self.queue:
			if c in cur.children:
				cur = cur.children[c]
				if cur.isEndWord:
					return True
			else:
				break
		return False


class TrieNode:

	def __init__(self):
		self.child = [None] * 26
		self.is_leaf = False

class StreamChecker:

	def __init__(self, words: List[str]):
		root = TrieNode()
		for word in words:
			l = len(word)
			node = root
			for i in range(l - 1, -1, -1):
				index = ord(word[i]) - 97
				if node.child[index] is None:
					node.child[index] = TrieNode()
				node = node.child[index]
			node.is_leaf = True
		self.root = root
		self.queue = []

	def query(self, letter: str) -> bool:
		self.queue.append(letter)
		if len(self.queue) > 2000:
			self.queue.pop(0)
		index = len(self.queue) - 1
		node = self.root
		while index >= 0:
			current = ord(self.queue[index]) - 97
			node = node.child[current]
			if node is None:
				return False
			elif node.is_leaf:
				return True
			index -= 1
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			level = self.trie
			for char in reversed(word):
				if char not in level:
					level.update({char: {}})
				level = level[char]
			level.update({'.': {}})
		self.stream = ''

	def query(self, letter: str) -> bool:
		self.stream += letter
		level = self.trie
		for char in reversed(self.stream):
			if '.' in level:
				return True
			if char not in level:
				return False
			level = level[char]
		if '.' in level:
			return True
		return False


class TreeNode:

	def __init__(self):
		self.children = {}
		self.value = None

	def insert(self, string):
		root = self
		for char in string:
			if char not in root.children:
				root.children[char] = TreeNode()
			root = root.children[char]
		root.value = string

	def find(self, string):
		root = self
		for char in string:
			if char in root.children:
				root = root.children[char]
			else:
				return None
		return root.value

class StreamChecker:

	def __init__(self, words):
		self.root = TreeNode()
		self.characters = deque()
		for word in words:
			self.root.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.characters.appendleft(letter)
		node = self.root
		for char in self.characters:
			if char in node.children:
				node = node.children[char]
				if node.value:
					return True
			else:
				return False


class TrieNode:

	def __init__(self):
		self.is_word = False
		self.children = {}

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add_word(self, word):
		node = self.root
		for c in word:
			if c not in node.children:
				node.children[c] = TrieNode()
			node = node.children[c]
		node.is_word = True

	def search_word(self, word):
		node = self.root
		for c in word:
			if c not in node.children:
				return False
			if node.children[c].is_word:
				return True
			node = node.children[c]
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.input = deque()
		self.trie = Trie()
		self.max_len = 0
		for word in words:
			self.trie.add_word(word[::-1])
			self.max_len = max(self.max_len, len(word))

	def query(self, letter: str) -> bool:
		self.input.appendleft(letter)
		if len(self.input) > self.max_len:
			self.input.pop()
		return self.trie.search_word(self.input)


class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_word = False

	def add_child(self, c):
		return self.children.setdefault(c, TrieNode())

	def get_child(self, c):
		return self.children.get(c)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.data = []
		self.root = TrieNode()
		for word in words:
			node = self.root
			for c in reversed(word):
				node = node.add_child(c)
			node.is_word = True

	def query(self, letter: str) -> bool:
		self.data.append(letter)
		node = self.root
		for i in range(len(self.data) - 1, -1, -1):
			node = node.get_child(self.data[i])
			if node is not None and node.is_word:
				return True
			elif node is None:
				return False


class TrieNode:

	def __init__(self):
		self.neighbor = {}
		self.isend = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie_root = TrieNode()
		self.letters = []
		self.max_len = max(list(map(len, words)))
		for word in words:
			curr = self.trie_root
			for c in word[::-1]:
				if c not in curr.neighbor:
					curr.neighbor[c] = TrieNode()
				curr = curr.neighbor[c]
			curr.isend = True

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		self.letters = self.letters[max(0, len(self.letters) - self.max_len):]
		curr = self.trie_root
		for l in self.letters[::-1]:
			if l in curr.neighbor:
				curr = curr.neighbor[l]
				if curr.isend:
					return True
			else:
				return False
		return False


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def addWord(self, word):
		curr = self.root
		for letter in word:
			index = ord(letter) - ord('a')
			if not curr.children[index]:
				curr.children[index] = TrieNode()
			curr = curr.children[index]
		curr.isWord = True

	def search(self, word):
		curr = self.root
		for letter in word:
			index = ord(letter) - ord('a')
			if not curr.children[index]:
				return False
			curr = curr.children[index]
			if curr.isWord:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.maxLength = 0
		self.q = []
		self.size = 0
		for word in words:
			revWord = word[::-1]
			length = len(revWord)
			self.maxLength = max(self.maxLength, length)
			self.trie.addWord(revWord)

	def query(self, letter: str) -> bool:
		self.q.append(letter)
		self.size += 1
		if self.size > self.maxLength:
			self.q.pop(0)
			self.size -= 1
		return self.trie.search(self.q[::-1])


class Trie:

	def __init__(self):
		self.trie = {}

	def insert(self, w):
		t = self.trie
		for c in w + '\\0':
			if c not in t:
				t[c] = {}
			t = t[c]

	def match(self, w):
		t = self.trie
		for c in w:
			if c not in t:
				return False
			t = t[c]
			if '\\0' in t:
				return True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.trie.match(self.stream)


class Node:

	def __init__(self):
		self.m = {}
		self.end = False

	def get(self, c):
		return self.m.get(c)

	def put(self, c, node):
		self.m[c] = node

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, word):
		node = self.root
		for c in word[::-1]:
			if not node.get(c):
				node.put(c, Node())
			node = node.get(c)
		node.end = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = deque([])
		for word in words:
			self.trie.insert(word)

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.trie.root
		ans = ''
		for c in self.stream:
			if node.end:
				return True
			if not node.get(c):
				return False
			node = node.get(c)
		return node.end


class Node:

	def __init__(self):
		self.children = [None] * 26
		self.terminal = False

class Trie:

	def __init__(self):
		self.root = Node()
		self.size = 0

	def get_index(self, x):
		return ord(x) - ord('a')

	def insert(self, input_str):
		curr = self.root
		for x in input_str:
			c = self.get_index(x)
			if curr.children[c] is not None:
				curr = curr.children[c]
			else:
				curr.children[c] = Node()
				curr = curr.children[c]
		curr.terminal = True

	def search(self, input_str):
		curr = self.root
		for x in input_str:
			c = self.get_index(x)
			if curr.children[c] is not None:
				curr = curr.children[c]
				if curr.terminal:
					return True
			else:
				return False
		return curr.terminal

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.request_string = ''

	def query(self, letter: str) -> bool:
		self.request_string = letter + self.request_string
		result = self.trie.search(self.request_string)
		return result


class Trie:

	def __init__(self):
		self.head = {}

	def insert(self, word):
		cur = self.head
		for ch in word:
			if ch not in cur:
				cur[ch] = {}
			cur = cur[ch]
		cur['*'] = True

	def search(self, word):
		cur = self.head
		for ch in word:
			if ch not in cur:
				return False
			cur = cur[ch]
		if cur.get('*'):
			return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.max_len = 0
		words = list([s[::-1] for s in words])
		for word in words:
			self.max_len = max(self.max_len, len(word))
			self.trie.insert(word)
		self.query_store = deque(maxlen=self.max_len)

	def query(self, letter: str) -> bool:
		self.query_store.appendleft(letter)
		query = ''.join(self.query_store)
		cur = self.trie.head
		for ch in query:
			if ch not in cur:
				return False
			cur = cur[ch]
			if cur.get('*'):
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add_word(self, word: str) -> None:
		node = self.root
		for ch in word:
			if ch not in node.children:
				node.children[ch] = TrieNode()
			node = node.children[ch]
		node.is_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = collections.deque()
		self.size = 0
		for word in words:
			self.size = max(len(word), self.size)
			self.trie.add_word(word[::-1])

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		if len(self.stream) > self.size:
			self.stream.pop()
		node = self.trie.root
		for ch in self.stream:
			if node.is_word:
				return True
			if ch not in node.children:
				return False
			node = node.children[ch]
		return node.is_word


class TrieNode:

	def __init__(self):
		self.child = {}
		self.isword = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		node = self.root
		for w in word:
			if w not in node.child:
				node.child[w] = TrieNode()
			node = node.child[w]
		node.isword = True

	def search(self, word):
		node = self.root
		for w in word:
			if w in node.child:
				if node.child[w].isword == True:
					return True
				node = node.child[w]
			else:
				return False
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.stack = []
		self.size = max((len(word) for word in words))

	def query(self, letter: str) -> bool:
		if len(self.stack) >= self.size:
			self.stack.pop(0)
		self.stack.append(letter)
		return self.trie.search(''.join(self.stack[::-1]))


class TrieNode:

	def __init__(self, val):
		self.val = val
		self.links = {}
		self.is_end = False

	def getLink(self, char):
		return self.links.get(char)

	def addLink(self, node):
		self.links[node.val] = node

	def setEnd(self):
		self.is_end = True

class Trie:

	def __init__(self):
		self.roots = [None] * 26

	def insertWord(self, word):
		if self.roots[ord(word[0]) - ord('a')] is None:
			self.roots[ord(word[0]) - ord('a')] = TrieNode(word[0])
		currNode = self.roots[ord(word[0]) - ord('a')]
		for i in range(1, len(word)):
			char = word[i]
			nextNode = currNode.getLink(char)
			if nextNode is None:
				nextNode = TrieNode(char)
				currNode.addLink(nextNode)
			currNode = nextNode
		currNode.setEnd()

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		maxLen = 0
		for word in words:
			self.trie.insertWord(word[::-1])
			maxLen = max(maxLen, len(word))
		self.maxsize = maxLen
		self.stream = []
		self.streamsize = 0

	def query(self, letter: str) -> bool:
		if self.streamsize == self.maxsize:
			self.stream = self.stream[1:]
			self.streamsize -= 1
		self.stream.append(letter)
		self.streamsize += 1
		node = None
		for i in range(len(self.stream) - 1, -1, -1):
			rootChar = self.stream[i]
			if node is None:
				node = self.trie.roots[ord(rootChar) - ord('a')]
			else:
				nextNode = node.getLink(rootChar)
				node = nextNode
			if node is None:
				break
			elif node.is_end:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isEnd = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		curr = self.root
		for ch in word:
			if ch not in curr.children:
				curr.children[ch] = TrieNode()
			curr = curr.children[ch]
		curr.isEnd = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.letters = []
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		i = len(self.letters) - 1
		curr = self.trie.root
		while i >= 0:
			if curr.isEnd == True:
				return True
			if self.letters[i] not in curr.children:
				return False
			curr = curr.children[self.letters[i]]
			i -= 1
		return curr.isEnd


class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = {}
		for word in words:
			curnode = self.Trie
			word = word[::-1]
			for ch in word:
				if ch not in curnode:
					curnode[ch] = {}
				curnode = curnode[ch]
			curnode['#'] = 1
		self.pre = ''

	def query(self, letter: str) -> bool:
		self.pre = self.pre + letter
		curnode = self.Trie
		for i in range(0, len(self.pre)):
			if self.pre[-i - 1] not in curnode:
				break
			curnode = curnode[self.pre[-i - 1]]
			if '#' in curnode:
				return True
		return False


class Trie(defaultdict):

	def addWord(self, word):
		curr = self
		for ch in word:
			if ch not in curr:
				curr[ch] = Trie()
			curr = curr[ch]
		curr['*'] = True

	def searchWord(self, word) -> bool:
		curr = self
		for ch in word:
			if ch in curr:
				curr = curr[ch]
			else:
				return False
			if '*' in curr:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.maxLen = 0
		for word in words:
			self.maxLen = max(self.maxLen, len(word))
			self.trie.addWord(word[::-1])
		self.history = []

	def query(self, letter: str) -> bool:
		if len(self.history) == self.maxLen:
			self.history.pop(0)
		self.history.append(letter)
		return self.trie.searchWord(''.join(self.history[::-1]))


class TrieNode:

	def __init__(self) -> None:
		self.children = {}
		self.isWord = False

class Trie:

	def __init__(self) -> None:
		self.root = TrieNode()

	def insert(self, word: str) -> None:
		current = self.root
		for letter in word:
			if letter not in current.children:
				current.children[letter] = TrieNode()
			current = current.children[letter]
		current.isWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.queries = []
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		idx = len(self.queries) - 1
		current = self.trie.root
		while idx >= 0:
			letter = self.queries[idx]
			if letter not in current.children:
				return False
			current = current.children[letter]
			if current.isWord:
				return True
			idx -= 1
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.tries = {}
		self.hist = []
		for word in words:
			cur = self.tries
			for c in word[::-1]:
				if c not in cur:
					cur[c] = {}
				cur = cur[c]
			cur['#'] = word

	def query(self, letter: str) -> bool:
		self.hist.insert(0, letter)
		cur = self.tries
		for c in self.hist:
			if '#' in cur:
				return True
			if c in cur:
				cur = cur[c]
			else:
				return False
		return '#' in cur


class StreamChecker:

	def build_trie(self, words):
		for word in words:
			w = word[::-1]
			curr = self.trie
			for l in w:
				if curr[ord(l) - ord('a')]:
					curr = curr[ord(l) - ord('a')]
				else:
					curr[ord(l) - ord('a')] = [[] for i in range(27)]
					curr = curr[ord(l) - ord('a')]
			curr[26] = 1

	def __init__(self, words: List[str]):
		self.trie = [[] for i in range(27)]
		self.mlength = max([len(w) for w in words])
		self.mem = ''
		self.build_trie(words)

	def query(self, letter: str) -> bool:
		if len(self.mem) < self.mlength:
			self.mem += letter
		else:
			self.mem = self.mem[1:] + letter
		w = self.mem[::-1]
		curr = self.trie
		for l in w:
			if curr[26]:
				return True
			if curr[ord(l) - ord('a')]:
				curr = curr[ord(l) - ord('a')]
			else:
				return False
		if curr[26]:
			return True
		else:
			return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.maxn = max([len(x) for x in words])
		self.suffix = set()
		self.words = set(words)
		for i in words:
			for j in range(len(i) - 1, -1, -1):
				self.suffix.add(i[j:])
		self.queue = []

	def query(self, letter: str) -> bool:
		if len(self.queue) >= self.maxn:
			self.queue.pop(0)
		self.queue.append(letter)
		st = ''
		for i in range(len(self.queue) - 1, -1, -1):
			st = self.queue[i] + st
			if st in self.words:
				return True
			elif st not in self.suffix:
				return False
		return False


class Node:

	def __init__(self, flag=False):
		self.flag = False
		self.a = [None] * 26

	def insert(self, s, p):
		if p == len(s):
			self.flag = True
			return
		c = ord(s[p]) - ord('a')
		if self.a[c] is None:
			self.a[c] = Node()
		self.a[c].insert(s, p + 1)

	def query(self, s, p):
		if p == -1:
			return False
		c = ord(s[p]) - ord('a')
		if self.a[c] is None:
			return False
		if self.a[c].flag:
			return True
		return self.a[c].query(s, p - 1)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.node = Node()
		self.s = []
		for word in words:
			self.node.insert(word[::-1], 0)

	def query(self, letter: str) -> bool:
		self.s.append(letter)
		return self.node.query(self.s, len(self.s) - 1)


class TrieNode:

	def __init__(self):
		self.next = {}
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		for word in words:
			itr = self.root
			for c in word[::-1]:
				itr.next.setdefault(c, TrieNode())
				itr = itr.next[c]
			itr.is_word = True
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		curr = self.root
		for c in self.stream:
			if c in curr.__next__:
				curr = curr.next[c]
				if curr.is_word:
					return True
			else:
				break
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.stream = []
		self.word_end = -1
		for word in words:
			cur_node = self.root
			for char in word[::-1]:
				cur_node.setdefault(char, {})
				cur_node = cur_node[char]
			cur_node[self.word_end] = True

	def query(self, letter: str) -> bool:
		self.stream.insert(0, letter)
		cur_node = self.root
		for char in self.stream:
			if char not in cur_node:
				return False
			else:
				cur_node = cur_node[char]
				if self.word_end in cur_node:
					return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.stream = deque([])
		for w in words:
			node = self.trie
			for c in w[::-1]:
				if c not in node:
					node[c] = {}
				node = node[c]
			node['$'] = w

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.trie
		for c in self.stream:
			if '$' in node:
				return True
			if c not in node:
				return False
			node = node[c]
		return '$' in node


class Trie:
	d = {}
	record = {}

	def __init__(self):
		self.record = {}
		self.d = {}

	def add(self, word):
		if len(word) == 1:
			if word[0] not in self.d:
				self.d[word[0]] = Trie()
				self.d[word[0]].d[-1] = 1
				return
			else:
				self.d[word[0]].d[-1] = 1
				return
		letter = word[-1]
		if letter in self.d:
			self.d[letter].add(word[:-1])
			return
		self.d[letter] = Trie()
		self.d[letter].add(word[:-1])
		return

	def search(self, ls, index):
		if index in self.record:
			return self.record[index]
		val = False
		if -1 in self.d:
			val = True
		elif index < 0:
			val = False
		elif ls[index] in self.d:
			val = self.d[ls[index]].search(ls, index - 1)
		self.record[index] = val
		return val

class StreamChecker:
	root = None
	l = None
	size = 0

	def __init__(self, words: List[str]):
		self.root = Trie()
		self.l = collections.deque()
		self.size = 0
		for i in words:
			self.root.add(i)

	def query(self, letter: str) -> bool:
		self.size += 1
		self.l.append(letter)
		return self.root.search(self.l, self.size - 1)


class Trie:

	def __init__(self):
		self.children = dict()
		self.isend = False

	def insert(self, word):
		cur = self
		for ch in word:
			if ch not in cur.children:
				cur.children[ch] = Trie()
			cur = cur.children[ch]
		cur.isend = True

	def hasPrefix(self, stream):
		cur = self
		for ch in stream:
			if ch not in cur.children:
				return False
			cur = cur.children[ch]
			if cur.isend:
				return True
		return cur.isend

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.longest = 0
		for w in words:
			self.longest = max(self.longest, len(w))
			self.trie.insert(w[::-1])
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		if len(self.stream) > self.longest:
			self.stream.pop()
		return self.trie.hasPrefix(''.join(self.stream))


class Node:

	def __init__(self):
		self.children = collections.defaultdict(Node)
		self.isStart = False

class Trie:

	def __init__(self, words: List[str]):
		self.root = Node()
		self.maxLen = 0
		for word in words:
			self.maxLen = max(self.maxLen, self.insert(word))

	def insert(self, word: str) -> int:
		i = 0
		current = self.root
		for char in word[::-1]:
			current = current.children[char]
			i += 1
		current.isStart = True
		return i

	def endsWith(self, word: str) -> bool:
		current = self.root
		for char in word:
			if current.isStart:
				return True
			if char not in current.children:
				return False
			current = current.children[char]
		return current.isStart

class StreamChecker:

	def __init__(self, words: List[str]):
		self.wl = Trie(words)
		self.bufSize = self.wl.maxLen
		self.history = collections.deque()

	def query(self, letter: str) -> bool:
		self.history.appendleft(letter)
		if len(self.history) > self.bufSize:
			self.history.pop()
		return self.wl.endsWith(''.join(self.history))


from collections import defaultdict

class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = defaultdict(list)
		for word in words:
			self.words[word[-1]].append(word)
		self.l = ''

	def query(self, letter: str) -> bool:
		self.l += letter
		for i in self.words[letter]:
			if self.l.endswith(i):
				return True
		return False


from collections import defaultdict
import queue

class TrieNode:

	def __init__(self):
		self.children = defaultdict(TrieNode)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.cstream = []
		for word in words:
			curr = self.root
			for c in word[::-1]:
				curr = curr.children[c]
			curr = curr.children['$']

	def query(self, letter: str) -> bool:
		self.cstream.insert(0, letter)
		curr = self.root
		for c in self.cstream:
			if '$' in curr.children:
				return True
			if c not in curr.children:
				return False
			curr = curr.children[c]
		return '$' in curr.children
		pass


class Trie:

	def __init__(self):
		self.child = {}
		self.isEnd = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		self.str = []
		for word in words:
			node = self.root
			for ch in reversed(word):
				if ch not in node.child:
					node.child[ch] = Trie()
				node = node.child[ch]
			node.isEnd = True

	def query(self, letter: str) -> bool:
		self.str.insert(0, letter)
		node = self.root
		for ch in self.str:
			if ch not in node.child:
				return False
			node = node.child[ch]
			if node.isEnd:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.isWord = False
		self.child = {}

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add_word(self, word):
		cur = self.root
		for i in range(len(word)):
			if word[i] not in cur.child:
				cur.child[word[i]] = TrieNode()
			cur = cur.child[word[i]]
		cur.isWord = True

	def search_word(self, word):
		cur = self.root
		for i in range(len(word)):
			if word[i] not in cur.child:
				return False
			else:
				cur = cur.child[word[i]]
		return cur.isWord

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.max_length = max((len(word) for word in words))
		self.letters = collections.deque()
		for word in words:
			self.trie.add_word(word[::-1])

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		if len(self.letters) > self.max_length:
			self.letters.popleft()
		t = list(self.letters)[::-1]
		cur = self.trie.root
		for letter in t:
			if letter not in cur.child:
				return False
			cur = cur.child[letter]
			if cur.isWord:
				return True
		return False


class TreeNode:

	def __init__(self, val=0):
		self.val = 0
		self.children = [None] * 26

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TreeNode(0)
		for word in words:
			word = word[::-1]
			current = self.root
			for letter in word:
				temp = current.children[ord(letter) - ord('a')]
				if temp is None:
					new = TreeNode(0)
					current.children[ord(letter) - ord('a')] = new
					current = new
				else:
					current = temp
			current.val = 1
		self.query_str = ''

	def query(self, letter: str) -> bool:
		self.query_str = letter + self.query_str
		return self.search()

	def search(self):
		current = self.root
		for letter in self.query_str:
			temp = current.children[ord(letter) - ord('a')]
			if temp is None:
				return False
			elif temp.val == 1:
				return True
			else:
				current = temp


class Node:

	def __init__(self):
		self.child = {}
		self.isword = False

class Trie:

	def __init__(self):
		self.root = Node()

	def addword(self, word):
		node = self.root
		for char in word:
			if char not in node.child:
				node.child[char] = Node()
			node = node.child[char]
		node.isword = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = Trie()
		for word in words:
			self.Trie.addword(word[::-1])
		self.queue = []

	def query(self, letter: str) -> bool:
		self.queue.insert(0, letter)
		node = self.Trie.root
		for char in self.queue:
			if char not in node.child:
				return False
			else:
				node = node.child[char]
				if node.isword:
					return True
		return False


class Trie:

	def __init__(self):
		self.node = TrieNode()

	def add(self, wordadd):
		node = self.node
		for char in wordadd:
			if char not in node.child:
				node.child[char] = TrieNode()
			node = node.child[char]
		node.isEnd = True

class TrieNode:

	def __init__(self):
		self.child = {}
		self.isEnd = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.letters = []
		self.rootNode = Trie()
		for word in words:
			self.rootNode.add(word[::-1])

	def query(self, letter: str) -> bool:
		self.letters.insert(0, letter)
		node = self.rootNode.node
		for letter in self.letters:
			if node.isEnd:
				return True
			if letter not in node.child:
				return False
			node = node.child[letter]
		return node.isEnd


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.buffer = ''
		self.letters = []
		for word in words:
			word = word[::-1]
			node = self.root
			for c in word:
				node = node.children[c]
			node.isEnd = True

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		i = len(self.letters) - 1
		node = self.root
		while i >= 0:
			if node.isEnd:
				return True
			if self.letters[i] not in node.children:
				return False
			node = node.children[self.letters[i]]
			i -= 1
		return node.isEnd

class TrieNode:

	def __init__(self):
		self.children = defaultdict(TrieNode)
		self.isEnd = False


class Node:

	def __init__(self, val=None, children={}, is_end=False):
		self.val = val
		self.children = children
		self.is_end = is_end

class StreamChecker:

	def __add_word(self, word):
		curr = self.root
		for char in word[::-1]:
			if char not in curr.children:
				curr.children[char] = Node(val=char, children={}, is_end=False)
			curr = curr.children[char]
		curr.is_end = True

	def __init__(self, words: List[str]):
		self.root = Node(None, {}, False)
		self.buffer = []
		for word in words:
			self.__add_word(word)

	def __is_present_k(self):
		curr = self.root
		for char in self.buffer:
			if char not in curr.children:
				return False
			curr = curr.children[char]
			if curr.is_end:
				return True
		return False

	def query(self, letter: str) -> bool:
		self.buffer.insert(0, letter)
		return self.__is_present_k()


class StreamChecker:

	def __init__(self, words: List[str]):
		self.s = ''
		self.maxLen = max((len(word) for word in words))
		self.dic = defaultdict(set)
		for word in words:
			self.dic[word[-1]].add(word)

	def query(self, letter: str) -> bool:
		self.s += letter
		self.s = self.s[-self.maxLen:]
		return any((self.s.endswith(w) for w in self.dic[letter]))


import collections

class Trie:

	def __init__(self):
		self.children = [None] * 26
		self.isEnd = False

	def add_word(self, word):
		curr = self
		for c in word:
			if curr.children[ord(c) - ord('a')] == None:
				curr.children[ord(c) - ord('a')] = Trie()
			curr = curr.children[ord(c) - ord('a')]
		curr.isEnd = True

	def search(self, word):
		curr = self
		for c in word:
			if curr.children[ord(c) - ord('a')] == None:
				return False
			curr = curr.children[ord(c) - ord('a')]
			if curr.isEnd:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = collections.deque()
		for w in words:
			self.trie.add_word(w[::-1])

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.trie.search(self.stream)


class TrieNode:

	def __init__(self):
		(self.children, self.end_node) = ({}, 0)

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for symbol in word:
			root = root.children.setdefault(symbol, TrieNode())
		root.end_node = 1

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = deque()
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		current = self.trie.root
		for char in self.stream:
			if char in current.children:
				current = current.children[char]
				if current.end_node:
					return True
			else:
				break
		return False


class TrieHolder:

	def __init__(self):
		self.flag = 0
		self.next = [None] * 26

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieHolder()
		for i in words:
			self.insert(i[::-1])
		self.searchLetters = []
		self.l = 0

	def insert(self, word: str) -> None:
		t = self.root
		for i in word:
			if t.next[ord(i) - 97] == None:
				t.next[ord(i) - 97] = TrieHolder()
			t = t.next[ord(i) - 97]
		t.flag = 1

	def query(self, letter: str) -> bool:
		self.searchLetters.append(letter)
		self.l += 1
		t = self.root
		for j in range(self.l - 1, -1, -1):
			letter = self.searchLetters[j]
			if t.next[ord(letter) - 97] == None:
				return False
			elif t.next[ord(letter) - 97].flag == 1:
				return True
			else:
				t = t.next[ord(letter) - 97]
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.s = ''
		self.dic = collections.defaultdict(set)
		for w in words:
			self.dic[w[-1]].add(w)

	def query(self, letter: str) -> bool:
		self.s += letter
		return any((self.s.endswith(w) for w in self.dic[letter]))


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.eow = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for i in range(length):
			if pCrawl.children[ord(key[i]) - ord('a')] is None:
				pCrawl.children[ord(key[i]) - ord('a')] = TrieNode()
			pCrawl = pCrawl.children[ord(key[i]) - ord('a')]
		pCrawl.eow = True

	def search(self, key):
		pCrawl = prev = self.root
		length = len(key)
		for i in range(length):
			if pCrawl.children[ord(key[i]) - ord('a')] is None:
				return False
			prev = pCrawl
			pCrawl = pCrawl.children[ord(key[i]) - ord('a')]
		return prev.children[ord(key[i]) - ord('a')].eow

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for key in words:
			self.trie.insert(key[::-1])
		self.word = ''

	def query(self, letter: str) -> bool:
		self.word = letter + self.word
		pCrawl = self.trie.root
		for c in self.word:
			if pCrawl.children[ord(c) - ord('a')] is not None:
				if pCrawl.children[ord(c) - ord('a')].eow:
					return True
				pCrawl = pCrawl.children[ord(c) - ord('a')]
			else:
				return False


class Trie:

	def __init__(self):
		self.Trie = {}

	def insert(self, word):
		curr = self.Trie
		for i in word:
			if i not in curr:
				curr[i] = {}
			curr = curr[i]
		curr['#'] = 1

	def search(self, word):
		curr = self.Trie
		for i in word:
			if i not in curr:
				return False
			elif '#' in curr[i]:
				return True
			curr = curr[i]
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.dictTree = Trie()
		self.queryStream = list()
		for i in words:
			self.dictTree.insert(i[::-1])

	def query(self, letter: str) -> bool:
		self.queryStream[0:0] = letter
		return self.dictTree.search(self.queryStream)


class Tri:

	def __init__(self):
		self.root = TriNode()

	def add(self, word):
		node = self.root
		for letter in word:
			if letter not in node.__next__:
				node.next[letter] = TriNode()
			node = node.next[letter]
		node.isWord = True

	def check(self, letters):
		node = self.root
		for letter in letters:
			if node.isWord:
				return True
			if letter not in node.__next__:
				return False
			node = node.next[letter]
		if node.isWord:
			return True
		return False

class TriNode:

	def __init__(self):
		self.next = {}
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.tri = Tri()
		self.recent = []
		self.maxLength = -1
		self.length = 0
		for word in words:
			if len(word) > self.maxLength:
				maxLength = len(word)
			self.tri.add(word[::-1])

	def query(self, letter: str) -> bool:
		if self.length == self.maxLength:
			self.recent.pop()
			self.recent.insert(0, letter)
		else:
			self.recent.insert(0, letter)
			self.length += 1
		return self.tri.check(self.recent)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.str = ''
		self.dict_set = collections.defaultdict(set)
		for word in words:
			self.dict_set[word[-1]].add(word)

	def query(self, letter: str) -> bool:
		self.str += letter
		return any((self.str.endswith(word) for word in self.dict_set[letter]))


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.val = -1

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		current = self.root
		for char in word:
			current = current.children[char]
		current.val = 10

	def search(self, word):
		current = self.root
		for char in word:
			current = current.children.get(char)
			if not current:
				return False
		return current.val > 1

	def starts_with(self, word):
		current = self.root
		for char in word:
			current = current.children.get(char)
			if not current:
				return False
		return True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.letters = []
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.letters += letter
		i = len(self.letters) - 1
		node = self.trie.root
		curr_word = ''
		for i in range(len(self.letters) - 1, -1, -1):
			curr_word += self.letters[i]
			has_sub_word = self.trie.starts_with(curr_word)
			if not has_sub_word:
				return False
			if has_sub_word and self.trie.search(curr_word):
				return True
		return False


class Node:

	def __init__(self, end=False):
		self.end = end
		self.children = [None] * 26

	def search(self, word):
		curr = self
		for w in word:
			slot = ord(w) - ord('a')
			if curr.children[slot]:
				curr = curr.children[slot]
				if curr.end:
					return True
			else:
				return False
		return curr.end

class StreamChecker:

	def __init__(self, words: List[str]):
		self.letters = ''
		self.root = Node('')

		def add(node, word):
			for w in word:
				slot = ord(w) - ord('a')
				if node.children[slot] is None:
					node.children[slot] = Node()
				node = node.children[slot]
			node.end = True
		for word in words:
			add(self.root, word[::-1])

	def query(self, letter: str) -> bool:
		self.letters = letter + self.letters
		return self.root.search(self.letters)


class TrieNode:

	def __init__(self, char):
		self.node = char
		self.children = {}
		self.end_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = TrieNode('*')
		for word in words:
			curr_node = self.trie
			for char in word[::-1]:
				node = curr_node.children.get(char, TrieNode(char))
				curr_node.children[char] = node
				curr_node = node
			curr_node.end_word = True
		self.queried = ''

	def exists(self, word):
		curr_node = self.trie
		for char in word:
			if char not in curr_node.children:
				return False
			curr_node = curr_node.children[char]
			if curr_node.end_word:
				return True
		if curr_node.end_word:
			return True
		return False

	def query(self, letter: str) -> bool:
		root = self.trie
		self.queried = letter + self.queried
		if self.exists(self.queried):
			return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isLeaf = False

	def add(self, word) -> None:
		if not word:
			return
		first = word[0]
		if first not in self.children:
			self.children[first] = TrieNode()
		if len(word) == 1:
			self.children[first].isLeaf = True
		self.children[first].add(word[1:])

	def search(self, word) -> bool:
		if not word:
			return self.isLeaf
		first = word[0]
		if first not in self.children:
			return False
		return self.children[first].search(word[1:])

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trieRoot = TrieNode()
		for word in words:
			self.trieRoot.add(word[::-1])
		self.stream = []

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		node = self.trieRoot
		for ch in reversed(self.stream):
			if ch in node.children:
				node = node.children[ch]
				if node.isLeaf:
					return True
			else:
				break
		return False


class Node:

	def __init__(self):
		self.child = [None] * 26
		self.w = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Node()
		for w in words:
			p = self.root
			for ch in w[::-1]:
				k = ord(ch) - ord('a')
				if not p.child[k]:
					p.child[k] = Node()
				p = p.child[k]
			p.w = True
		self.q = []

	def query(self, letter: str) -> bool:
		self.q.append(letter)
		p = self.root
		for i in range(len(self.q) - 1, -1, -1):
			k = ord(self.q[i]) - ord('a')
			if not p.child[k]:
				return False
			p = p.child[k]
			if p.w:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in set(words):
			node = self.trie
			for c in word[::-1]:
				node = node.setdefault(c, {})
			node['$'] = word
		self.nodelist = []

	def query(self, letter: str) -> bool:
		self.nodelist.insert(0, letter)
		node = self.trie
		for ch in self.nodelist:
			if '$' in node:
				return True
			if not ch in node:
				return False
			node = node[ch]
		return '$' in node


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEnd = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.s = []
		self.root = TrieNode()
		for w in words:
			node = self.root
			for i in range(len(w) - 1, -1, -1):
				idx = ord(w[i]) - ord('a')
				if not node.children[idx]:
					node.children[idx] = TrieNode()
				node = node.children[idx]
			node.isEnd = True

	def query(self, letter: str) -> bool:
		self.s.append(letter)
		node = self.root
		for i in range(len(self.s) - 1, -1, -1):
			idx = ord(self.s[i]) - ord('a')
			node = node.children[idx]
			if node != None and node.isEnd:
				return True
			if node == None:
				return False
		return False


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.end = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		for word in words:
			node = self.root
			for ch in word[::-1]:
				ind = ord(ch) - ord('a')
				if node.children[ind] is None:
					node.children[ind] = TrieNode()
				node = node.children[ind]
			node.end = True
		self.hist = ''

	def query(self, letter: str) -> bool:
		self.hist = letter + self.hist
		node = self.root
		for ch in self.hist:
			ind = ord(ch) - ord('a')
			if node.children[ind] is None:
				return False
			node = node.children[ind]
			if node.end:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.terminal = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.history = []
		for word in words:
			node = self.root
			for char in word[::-1]:
				if char not in node.children:
					node.children[char] = TrieNode()
				node = node.children[char]
			node.terminal = True

	def query(self, letter: str) -> bool:
		self.history.append(letter)
		node = self.root
		for i in reversed(list(range(len(self.history)))):
			if self.history[i] in node.children:
				node = node.children[self.history[i]]
				if node.terminal:
					return True
			else:
				return False
		return False


class TNode(object):

	def __init__(self):
		(self.d, self.f) = ({}, False)

class StreamChecker(object):

	def __init__(self, words):
		self.root = TNode()
		self.max_ = max(map(len, words))
		for w in words:
			cur = self.root
			for j in range(len(w) - 1, -1, -1):
				if w[j] not in cur.d:
					cur.d[w[j]] = TNode()
				cur = cur.d[w[j]]
			cur.f = True
		self.q = collections.deque([])

	def query(self, letter):
		self.q.appendleft(letter)
		if len(self.q) > self.max_:
			self.q.pop()
		cur = self.root
		for l in self.q:
			if l not in cur.d:
				return False
			cur = cur.d[l]
			if cur.f:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.end = 0

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for char in word:
			root = root.children.setdefault(char, TrieNode())
		root.end = 1

class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = Trie()
		self.QueryBuffer = deque()
		for word in words:
			self.Trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.QueryBuffer.appendleft(letter)
		cur = self.Trie.root
		for char in self.QueryBuffer:
			if char in cur.children:
				cur = cur.children[char]
				if cur.end == 1:
					return True
			else:
				break
		return False


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEnd = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = self.getNode()
		for word in words:
			self.addWord(word[::-1])
		self.currStr = deque([])

	def addWord(self, word):
		node = self.root
		for c in word:
			ind = self.getInd(c)
			if node.children[ind] == None:
				node.children[ind] = self.getNode()
			node = node.children[ind]
		node.isEnd = True

	def getInd(self, c):
		return ord(c) - ord('a')

	def getNode(self):
		return TrieNode()

	def findWord(self, letter):
		self.currStr.appendleft(letter)
		node = self.root
		for ch in self.currStr:
			ind = self.getInd(ch)
			if node.children[ind] == None:
				return False
			elif node.children[ind].isEnd:
				return True
			node = node.children[ind]
		return node and node.isEnd

	def query(self, letter: str) -> bool:
		return self.findWord(letter)


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def charToIndex(self, c):
		return ord('a') - ord(c)

	def insert(self, word):
		start = self.root
		n = len(word)
		for i in range(n):
			idx = self.charToIndex(word[i])
			if not start.children[idx]:
				start.children[idx] = TrieNode()
			start = start.children[idx]
		start.isEndOfWord = True

	def search(self, word):
		start = self.root
		n = len(word)
		for i in range(n):
			idx = self.charToIndex(word[i])
			if start.children[idx] == None:
				return False
			start = start.children[idx]
			if start.isEndOfWord:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		for word in words:
			self.t.insert(word[::-1])
		self.stream = collections.deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.t.search(self.stream)


class Trie:

	def __init__(self, letter):
		self.next_letters = dict()
		self.letter = letter
		self.is_end_word = False

	def add_word(self, word, idx):
		if idx == -1:
			self.is_end_word = True
			return
		if word[idx] not in self.next_letters:
			self.next_letters[word[idx]] = Trie(word[idx])
		self.next_letters[word[idx]].add_word(word, idx - 1)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie('')
		for w in words:
			self.trie.add_word(w, len(w) - 1)
		self.history = list()

	def query(self, letter: str) -> bool:
		self.history.append(letter)
		cur_node = self.trie
		idx = len(self.history) - 1
		while idx >= 0 and self.history[idx] in cur_node.next_letters:
			c = self.history[idx]
			cur_node = cur_node.next_letters[c]
			if cur_node.is_end_word:
				return True
			idx -= 1
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.tab = collections.defaultdict(set)
		for w in words:
			self.tab[w[-1]].add(w[::-1])
		self.queried = ''

	def query(self, letter: str) -> bool:
		self.queried = letter + self.queried
		if letter not in self.tab:
			return False
		candidates = self.tab[letter]
		for can in candidates:
			if can == self.queried[:len(can)]:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.s_list = []
		self.word_len = 0
		for w in words:
			tmp = self.trie
			for c in w[::-1]:
				tmp = tmp.setdefault(c, {})
			tmp['#'] = True
			self.word_len = max(self.word_len, len(w))

	def query(self, letter: str) -> bool:
		self.s_list += (letter,)
		self.s_list = self.s_list[-self.word_len:]
		curr = self.trie
		for c in self.s_list[::-1]:
			if c in curr:
				curr = curr[c]
				if curr.get('#', False):
					return True
			else:
				break
		return False


import collections

class Trie:

	def __init__(self):
		self.children = [None] * 26
		self.is_end = False

	def insert(self, s):
		root = self
		for c in s:
			if root.children[ord(c) - ord('a')] is None:
				root.children[ord(c) - ord('a')] = Trie()
			root = root.children[ord(c) - ord('a')]
		root.is_end = True

	def search(self, s):
		root = self
		for c in s:
			if root.children[ord(c) - ord('a')] is None:
				return False
			root = root.children[ord(c) - ord('a')]
			if root.is_end:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		self.stream = collections.deque()
		for w in words:
			self.root.insert(reversed(w))

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.root.search(self.stream)


from collections import deque

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.max_word_len = 0
		for word in words:
			node = self.trie
			for i in reversed(list(range(len(word)))):
				if word[i] not in node:
					node[word[i]] = {}
				node = node[word[i]]
			node[True] = True
			self.max_word_len = max(self.max_word_len, len(word))
		self.queue = deque()

	def query(self, letter: str) -> bool:
		self.queue.appendleft(letter)
		if len(self.queue) > self.max_word_len:
			self.queue.pop()
		node = self.trie
		for char in self.queue:
			if True in node:
				return True
			if char not in node:
				return False
			node = node[char]
		return True in node


class node:

	def __init__(self):
		self.children = {}
		self.end = 0

class Trie:

	def __init__(self):
		self.root = node()

	def insert(self, w):
		r = self.root
		for l in w:
			r = r.children.setdefault(l, node())
		r.end = 1

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.s = deque()
		for w in words:
			self.trie.insert(w[::-1])

	def query(self, letter: str) -> bool:
		self.s.appendleft(letter)
		cur = self.trie.root
		for c in self.s:
			if c in cur.children:
				cur = cur.children[c]
				if cur.end:
					return True
			else:
				break
		return False


class Node:

	def __init__(self, char):
		self.char = char
		self.nxts = {}
		self.end = False

class Trie:

	def __init__(self):
		self.root = Node('')

	def _add(self, node, word):
		if word == '':
			node.end = True
			return
		if word[0] not in node.nxts:
			node.nxts[word[0]] = Node(word[0])
		self._add(node.nxts[word[0]], word[1:])

	def add(self, word):
		self._add(self.root, word)

	def _search(self, node, word):
		if node.end is True:
			return True
		if word == '':
			return node.end is True
		if word[0] in node.nxts:
			return self._search(node.nxts[word[0]], word[1:])
		return False

	def search(self, word):
		return self._search(self.root, word)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for w in words:
			self.trie.add(w[::-1])
		self.sofar = ''

	def query(self, letter: str) -> bool:
		self.sofar = letter + self.sofar
		return self.trie.search(self.sofar)


class TrieNode:

	def __init__(self):
		self.fin = ''
		self.children = {}

	def add(self, s, i=0):
		if i == len(s):
			self.fin = s
			return
		if s[i] not in self.children:
			self.children[s[i]] = TrieNode()
		self.children[s[i]].add(s, i + 1)

	def get(self, c):
		result = self.children.get(c, None)
		return result

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		for w in words:
			self.root.add(w[::-1])
		self.hist = []

	def query(self, letter: str) -> bool:
		self.hist.append(letter)
		node = self.root
		for l in reversed(self.hist):
			node = node.get(l)
			if node is None:
				return False
			if node.fin:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add(word[::-1])
		self.sb = []

	def query(self, letter: str) -> bool:
		self.sb.append(letter)
		i = len(self.sb) - 1
		node = self.trie
		while i >= 0:
			if node.is_word:
				return True
			if self.sb[i] in node.words:
				node = node.words[self.sb[i]]
				i -= 1
			else:
				return False
		return node.is_word

class Trie:

	def __init__(self):
		self.is_word = False
		self.prefix = ''
		self.words = {}

	def add(self, word):
		if not word:
			self.is_word = True
			return
		if word[0] in self.words:
			self.words[word[0]].add(word[1:])
		else:
			n = Trie()
			n.prefix = self.prefix + word[0]
			self.words[word[0]] = n
			self.words[word[0]].add(word[1:])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.prefix = {}
		self.maxlen = 0
		for word in words:
			for i in range(len(word) - 1, -1, -1):
				if word[i:][::-1] not in self.prefix:
					self.prefix[word[i:][::-1]] = 0
			self.prefix[word[::-1]] = 1
			self.maxlen = max(self.maxlen, len(word))
		self.stack = []

	def query(self, letter: str) -> bool:
		self.stack.append(letter)
		for i in range(len(self.stack) - 1, -1, -1):
			if len(self.stack[i:]) > self.maxlen:
				break
			temp = ''.join(self.stack[i:][::-1])
			if temp not in self.prefix:
				return False
			if self.prefix[temp] == 1:
				return True
		return False


class StreamChecker:

	def __init__(self, words):
		self.trie = dict()
		for word in words:
			t = self.trie
			for c in reversed(word):
				if c not in t:
					t[c] = dict()
				t = t[c]
			t['#'] = True
		self.stack = []

	def query(self, letter):
		self.stack.append(letter)
		t = self.trie
		for c in reversed(self.stack):
			if '#' in t:
				return True
			if c not in t:
				return False
			t = t[c]
		return '#' in t

class StreamChecker:

	def __init__(self, words):
		self.trie = dict()
		for word in words:
			t = self.trie
			for c in reversed(word):
				if c not in t:
					t[c] = dict()
				t = t[c]
			t['#'] = True
		self.stack = []

	def query(self, letter):
		self.stack.append(letter)
		t = self.trie
		for c in reversed(self.stack):
			if '#' in t:
				return True
			elif c in t:
				t = t[c]
			else:
				return False
		return '#' in t


class StreamChecker:

	def __init__(self, words: List[str]):
		self.q = ''
		self.m = defaultdict(set)
		for w in words:
			self.m[w[-1]].add(w)

	def query(self, letter: str) -> bool:
		self.q += letter
		return any(filter(lambda w: self.q.endswith(w), self.m[letter]))


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(lambda : TrieNode())
		self.word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		node = self.root
		for c in word:
			node = node.children[c]
		node.word = True

	def search(self, word, prefix=False):
		node = self.root
		for c in word:
			if c not in node.children:
				return False
			node = node.children[c]
		if prefix:
			return True
		return node.word

	def starswith(self, prefix):
		return self.search(prefix, True)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for w in words:
			self.trie.insert(w[::-1])
		self.stream = []

	def query(self, letter: str) -> bool:
		self.stream += [letter]
		for i in range(1, len(self.stream) + 1):
			if not self.trie.search(self.stream[-i:][::-1], True):
				return False
			if self.trie.search(self.stream[-i:][::-1]):
				return True


class Trie:

	def __init__(self):
		self.isWord = False
		self.children = {}

class StreamChecker:

	def __init__(self, words: List[str]):
		self.StreamChecker = Trie()
		self.letters = []
		for word in words:
			self.addWord(word)

	def query(self, letter: str) -> bool:
		self.letters.insert(0, letter)
		node = self.StreamChecker
		for letter in self.letters:
			if node.isWord == True:
				return True
			if letter not in node.children:
				return False
			node = node.children[letter]
		if node.isWord == True:
			return True
		else:
			return False

	def addWord(self, word):
		node = self.StreamChecker
		word = word[::-1]
		for letter in word:
			if letter not in node.children:
				node.children[letter] = Trie()
			node = node.children[letter]
		node.isWord = True


from typing import List, Dict, Optional, Iterator

class TrieNode:

	def __init__(self, value: Optional[str]) -> None:
		self.value = value
		self.children: Dict[str, TrieNode] = {}
		self.end = False
		self.word: Optional[str] = None

def build_trie(words: List[str]) -> TrieNode:
	trie_root = TrieNode(None)
	for word in words:
		trie_node = trie_root
		for char in word:
			trie_node.children.setdefault(char, TrieNode(char))
			trie_node = trie_node.children[char]
		trie_node.end = True
		trie_node.word = word
	return trie_root

class StreamCheckerForward:

	def __init__(self, words: List[str]) -> None:
		self._trie = build_trie(words)
		self._trie_ptrs = [self._trie]

	def query(self, letter: str) -> bool:
		trie_ptrs_new = [self._trie]
		result = False
		for trie_node in self._trie_ptrs:
			if letter in trie_node.children:
				result = result or trie_node.children[letter].end
				trie_ptrs_new.append(trie_node.children[letter])
		self._trie_ptrs = trie_ptrs_new
		return result

class StreamChecker:

	def __init__(self, words: List[str]) -> None:
		self._trie = build_trie(list(map(reversed, words)))
		self._stream = []

	def _search_trie(self, stream: Iterator[str]) -> bool:
		node = self._trie
		for char in stream:
			if node.end:
				return True
			if char in node.children:
				node = node.children[char]
			else:
				break
		return node.end

	def query(self, letter: str) -> bool:
		self._stream.append(letter)
		return self._search_trie(reversed(self._stream))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.now = ''
		self.trie = defaultdict()
		for w in words:
			cur = self.trie
			for i in w[::-1]:
				if i not in cur:
					cur[i] = defaultdict()
				cur = cur[i]
			cur['#'] = None

	def query(self, letter: str) -> bool:
		self.now += letter
		s = self.now[::-1]
		cur = self.trie
		for i in s:
			if i in cur:
				cur = cur[i]
				if '#' in cur:
					return True
			else:
				break
		return False


class trie:

	def __init__(self):
		self.endofword = False
		self.children = [None] * 26

	def insert(self, s):
		t = self
		for i in s:
			if t.children[ord(i) - ord('a')] == None:
				t.children[ord(i) - ord('a')] = trie()
			t = t.children[ord(i) - ord('a')]
		t.endofword = True

	def search(self, w):
		t = self
		for i in w:
			if t.children[ord(i) - ord('a')] == None:
				return False
			t = t.children[ord(i) - ord('a')]
			if t.endofword:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = trie()
		self.dequeue = []
		for i in words:
			self.t.insert(reversed(i))

	def query(self, letter: str) -> bool:
		self.dequeue.insert(0, letter)
		return self.t.search(self.dequeue)


class StreamChecker:

	class Node:

		def __init__(self):
			self.node = [None] * 26
			self.end = False

	def __init__(self, words: List[str]):
		self.s = ''
		self.root = self.Node()
		for word in words:
			node = self.root
			n = len(word)
			word = word[::-1]
			for i in range(n):
				a = ord(word[i]) - ord('a')
				if not node.node[a]:
					node.node[a] = self.Node()
				if i == n - 1:
					node.node[a].end = True
				else:
					node = node.node[a]
			print((node.node, node.end))

	def query(self, letter: str) -> bool:
		self.s = letter + self.s
		node = self.root
		for i in self.s:
			a = ord(i) - ord('a')
			if not node.node[a]:
				return False
			if node.node[a].end:
				print(not node.node[a])
				return True
			node = node.node[a]
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.map = {}
		self.history = ''
		for w in words:
			curr = self.map
			w = w[::-1]
			w += '0'
			for c in w:
				if c not in curr:
					curr[c] = {}
				curr = curr[c]

	def query(self, letter: str) -> bool:
		self.history += letter
		curr = self.map
		for c in self.history[::-1]:
			if c not in curr:
				break
			curr = curr[c]
			if '0' in curr:
				return True
		return False


from collections import defaultdict

def tree():
	return defaultdict(tree)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = defaultdict(tree)
		for word in words:
			node = self.trie
			for char in word[::-1]:
				node = node[char]
			node['*'] = 1
		self.current_list = []

	def query(self, letter: str) -> bool:
		match_found = True
		end_found = False
		self.current_list.insert(0, letter)
		i = 0
		node = self.trie
		while i < len(self.current_list):
			if self.current_list[i] in list(node.keys()):
				node = node[self.current_list[i]]
				if '*' in list(node.keys()):
					end_found = True
					break
			else:
				match_found = False
				break
			i += 1
		if '*' in list(node.keys()):
			end_found = True
		return end_found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.end = -1
		for w in words:
			self.insert(w)
		self.qhist = ''

	def insert(self, word):
		curNode = self.root
		for c in word[::-1]:
			if not c in curNode:
				curNode[c] = {}
			curNode = curNode[c]
		curNode[self.end] = True

	def query(self, letter: str) -> bool:
		self.qhist += letter
		curNode = self.root
		for c in self.qhist[::-1]:
			if not c in curNode:
				return False
			curNode = curNode[c]
			if self.end in curNode:
				return True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.history = ''
		self.map = {}
		for word in words:
			curr_node = self.map
			for letter in word[::-1]:
				if letter not in curr_node:
					curr_node[letter] = {}
				curr_node = curr_node[letter]
			curr_node['#'] = {}

	def query(self, letter: str) -> bool:
		self.history += letter
		curr_node = self.map
		for l in self.history[::-1]:
			if l not in curr_node:
				return False
			curr_node = curr_node[l]
			if '#' in curr_node:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isEnd = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.q = ''
		self.root = TrieNode()
		for word in words:
			root = self.root
			for w in word[::-1]:
				root = root.children.setdefault(w, TrieNode())
			root.isEnd = True

	def dfs(self, node, n) -> bool:
		if n > len(self.q):
			return False
		if self.q[-n] in node.children:
			if node.children[self.q[-n]].isEnd:
				return True
			else:
				return self.dfs(node.children[self.q[-n]], n + 1)
		else:
			return False

	def query(self, letter: str) -> bool:
		self.q += letter
		return self.dfs(self.root, 1)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.stream = []
		for word in words:
			node = self.trie
			for ch in word[::-1]:
				if ch not in node:
					node[ch] = {}
				node = node[ch]
			node['#'] = {}

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		node = self.trie
		for i in range(len(self.stream) - 1, -1, -1):
			if self.stream[i] not in node:
				return False
			else:
				node = node[self.stream[i]]
				if '#' in node:
					return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = words
		self.trie = {}
		self.history = ''
		for w in words:
			p = self.trie
			for c in w[::-1]:
				if c not in p:
					p[c] = {}
				p = p[c]
			p['#'] = ''

	def query(self, letter: str) -> bool:
		self.history += letter
		p = self.trie
		for c in self.history[::-1]:
			if c not in p:
				break
			p = p[c]
			if '#' in p:
				return True
		return False


class TrieNode:

	def __init__(self, ch):
		self.ch = None
		self.children = {}
		self.isWord = False

class Trie:

	def __init__(self, words):
		self.root = TrieNode(None)
		for word in words:
			self.insert(word[::-1])
		return

	def insert(self, word):
		cur = self.root
		for ch in word:
			if ch not in cur.children:
				newNode = TrieNode(ch)
				cur.children[ch] = newNode
			cur = cur.children[ch]
		cur.isWord = True

	def search(self, word):
		cur = self.root
		for ch in word[::-1]:
			if cur.isWord:
				return True
			if ch not in cur.children:
				return False
			cur = cur.children[ch]
		return cur.isWord

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trieTree = Trie(words)
		self.curr = ''

	def query(self, letter: str) -> bool:
		self.curr += letter
		return self.trieTree.search(self.curr)


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.is_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add_words(self, words):
		for word in words:
			cur = self.root
			for l in word:
				cur = cur.children[l]
			cur.is_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.trie.add_words([word[::-1] for word in words])
		self.prefix = ''

	def query(self, letter: str) -> bool:
		self.prefix += letter
		cur = self.trie.root
		for l in self.prefix[::-1]:
			if l not in cur.children:
				break
			cur = cur.children[l]
			if cur.is_word:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.dict = collections.defaultdict(TrieNode)
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = ''
		self.root = TrieNode()
		for word in words:
			curr = self.root
			for char in word[::-1]:
				curr = curr.dict[char]
			curr.isWord = True

	def query(self, letter: str) -> bool:
		self.stream += letter
		curr = self.root
		for char in self.stream[::-1]:
			if char not in curr.dict:
				return False
			curr = curr.dict[char]
			if curr.isWord:
				return True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = Tree()
		for item in words:
			curr = self.tree
			for letter in item[::-1]:
				if letter not in curr.subtrees:
					curr.subtrees[letter] = Tree()
				curr = curr.subtrees[letter]
			curr.food = True
		self.q = ''

	def query(self, letter: str) -> bool:
		self.q += letter
		reverse_q = self.q[::-1]
		curr = self.tree
		for letter in reverse_q:
			if letter not in curr.subtrees:
				return False
			else:
				curr = curr.subtrees[letter]
				if curr.food:
					return True
		return False

class Tree:

	def __init__(self):
		self.food = False
		self.subtrees = {}


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.is_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def build(self, words):
		for word in words:
			self._insert(word[::-1])

	def _insert(self, word):
		cur = self.root
		for char in word:
			cur = cur.children[char]
		cur.is_word = True

	def search(self, stream):
		cur = self.root
		for char in stream[::-1]:
			if char in cur.children:
				cur = cur.children[char]
				if cur.is_word:
					return True
			else:
				return False
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.trie.build(words)
		self.stream = ''

	def query(self, letter: str) -> bool:
		self.stream += letter
		found = self.trie.search(self.stream)
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			trie = self.trie
			for letter in word[::-1]:
				if letter in trie:
					trie = trie[letter]
				else:
					trie[letter] = {}
					trie = trie[letter]
			trie['$'] = ''
		print(self.trie)
		self.queue = deque()
		self.max_len = max((len(word) for word in words))

	def query(self, letter: str) -> bool:
		self.queue.appendleft(letter)
		if len(self.queue) > self.max_len:
			self.queue.pop()
		trie = self.trie
		for letter in self.queue:
			trie = trie.get(letter)
			if trie is None:
				return False
			if '$' in trie:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		r = self.root
		self.maxLen = 0
		self.q = []
		for w in words:
			w = w[::-1]
			self.maxLen = max(len(w), self.maxLen)
			temp = r
			for c in w:
				if c in temp.c:
					temp = temp.c[c]
				else:
					temp.c[c] = Trie()
					temp = temp.c[c]
			temp.end = True

	def query(self, l: str) -> bool:
		q = self.q
		n = self.maxLen
		r = self.root
		q.append(l)
		if len(q) > n:
			q.pop(0)

		def dfs(s, node):
			if len(s) == 0:
				return node.end
			if node.end:
				return True
			if s[0] in node.c:
				return dfs(s[1:], node.c[s[0]])
			else:
				return False
		ans = dfs(q[::-1], r)
		return ans

class Trie:

	def __init__(self):
		self.end = False
		self.c = {}


class Trie:

	def __init__(self, end=False):
		self.child = dict()
		self.end = end

	def add(self, word, i=0):
		if i >= len(word):
			return
		if word[i] not in self.child:
			self.child[word[i]] = Trie()
		if i == len(word) - 1:
			self.child[word[i]].end = True
		self.child[word[i]].add(word, i + 1)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		for word in words:
			self.root.add(word[::-1])
		self.stream = ''

	def query(self, letter: str) -> bool:
		self.stream = letter + self.stream
		ptr = self.root
		for c in self.stream:
			if c in ptr.child:
				ptr = ptr.child[c]
				if ptr.end:
					return True
			else:
				break
		return False


class TrieNode:

	def __init__(self):
		self.dict = defaultdict(TrieNode)
		self.word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.prefix = ''
		for word in words:
			node = self.root
			for char in word[::-1]:
				node = node.dict[char]
			node.word = True

	def query(self, letter: str) -> bool:
		self.prefix += letter
		node = self.root
		for char in self.prefix[::-1]:
			if char not in node.dict:
				break
			node = node.dict[char]
			if node.word == True:
				return True
		return False


from collections import deque

class TreeNode:

	def __init__(self, val):
		self.val = val
		self.children = []

	def add_child(self, node):
		self.children.append(node)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TreeNode(0)
		self.max_len = 0
		for w in words:
			self.max_len = max(self.max_len, len(w))
			self.make_trie(w)
		self.history = deque([])

	def make_trie(self, word):
		parent = self.root
		for char in word[::-1]:
			current = None
			for node in parent.children:
				if node.val == char:
					current = node
			if not current:
				current = TreeNode(char)
				parent.add_child(current)
			parent = current
		parent.add_child(TreeNode('.'))

	def query(self, letter: str) -> bool:
		self.history.append(letter)
		if len(self.history) > self.max_len:
			self.history.popleft()
		parent = self.root
		for i in range(len(self.history) - 1, -1, -1):
			char = self.history[i]
			if i < len(self.history) - 1 and any([child.val == '.' for child in parent.children]):
				return True
			found = False
			for node in parent.children:
				if node.val == char:
					found = True
					parent = node
			if not found:
				return False
		return any([child.val == '.' for child in parent.children])


class TrieNode:

	def __init__(self):
		self.is_leaf = False
		self.mp = {}

class StreamChecker:

	def __init__(self, words: List[str]):

		def insert(root, w):
			for c in w:
				if c not in root.mp:
					root.mp[c] = TrieNode()
				root = root.mp[c]
			root.is_leaf = True
		self.root = TrieNode()
		for w in words:
			insert(self.root, w[::-1])
		self.s = ''

	def query(self, letter: str) -> bool:

		def find(root, s):
			for c in s[::-1]:
				if c not in root.mp:
					return False
				root = root.mp[c]
				if root.is_leaf:
					return True
			return False
		self.s += letter
		return find(self.root, self.s)


class TrieNode:

	def __init__(self):
		self.is_word = False
		self.children = {}

class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = deque()
		self.root = TrieNode()
		for word in words:
			node = self.root
			for ch in word[::-1]:
				if ch not in node.children:
					node.children[ch] = TrieNode()
				node = node.children[ch]
			node.is_word = True

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.root
		for ch in self.stream:
			if ch not in node.children:
				return False
			node = node.children[ch]
			if node.is_word:
				return True
		return False


class Node:

	def __init__(self):
		self.isLast = False
		self.child = {}

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, ind, word, node=None):
		if node == None:
			node = self.root
		if ind == len(word):
			node.isLast = True
		else:
			if word[ind] not in node.child:
				node.child[word[ind]] = Node()
			self.insert(ind + 1, word, node.child[word[ind]])

	def isPrefix(self, ind, word, node=None):
		if node == None:
			node = self.root
		if node.isLast:
			return True
		if ind == len(word):
			return False
		if word[ind] in node.child:
			return self.isPrefix(ind + 1, word, node.child[word[ind]])
		return False
from collections import deque

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(0, word[::-1])
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		return self.trie.isPrefix(0, self.stream)


class Node:

	def __init__(self):
		self.children = [None] * 26
		self.isWord = False

	def calculateIndex(self, char):
		return ord(char) - ord('a')

class Trie:

	def __init__(self):
		self.root = Node()

	def addWordInReverse(self, word):
		wordLength = len(word)
		currentNode = self.root
		for i in range(wordLength - 1, -1, -1):
			currentChar = word[i]
			currentIndex = currentNode.calculateIndex(currentChar)
			if currentNode.children[currentIndex] == None:
				currentNode.children[currentIndex] = Node()
			currentNode = currentNode.children[currentIndex]
		currentNode.isWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = []
		self.reverseTrie = Trie()
		for word in words:
			self.reverseTrie.addWordInReverse(word)

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		streamLength = len(self.stream)
		currentNode = self.reverseTrie.root
		for i in range(streamLength - 1, -1, -1):
			currentChar = self.stream[i]
			currentIndex = currentNode.calculateIndex(currentChar)
			if currentNode.children[currentIndex] != None:
				currentNode = currentNode.children[currentIndex]
				if currentNode.isWord:
					return True
			else:
				return False
		return False


class Trie:

	def __init__(self):
		self.children = {}
		self.endSymbol = '*'

	def insert(self, word):
		children = self.children
		for char in word[::-1]:
			if char not in children:
				children[char] = {}
			children = children[char]
		children[self.endSymbol] = word

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word)
		self.array = []

	def query(self, letter: str) -> bool:
		self.array.insert(0, letter)
		nodes = self.trie.children
		for char in self.array:
			if self.trie.endSymbol in nodes:
				return True
			if char not in nodes:
				return False
			nodes = nodes[char]
		return self.trie.endSymbol in nodes


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			node = self.trie
			for ch in reversed(word):
				node.setdefault(ch, {})
				node = node[ch]
			node['#'] = {}
		self.letters = []

	def query(self, letter: str) -> bool:
		self.letters.insert(0, letter)
		node = self.trie
		for ch in self.letters:
			if ch in node:
				node = node[ch]
				if '#' in node:
					return True
			else:
				break
		return False


class StreamChecker:

	class Trie_node:

		def __init__(self, value):
			self.value = value
			self.children = {}

		def add_child(self, node, letter):
			self.children[letter] = node

		def is_child(self, letter):
			if letter in self.children:
				return True
			return False

		def get_child(self, letter):
			return self.children[letter]

	def __init__(self, words: List[str]):
		self.trie = self.Trie_node(-1)
		self.max_length = 0
		self.letter_list = []
		for word in words:
			current_node = self.trie
			self.max_length = self.max_length if len(word) < self.max_length else len(word)
			for (index, letter) in enumerate(word[::-1]):
				if current_node.is_child(letter):
					if index == len(word) - 1:
						current_node.get_child(letter).value = 0
				elif index == len(word) - 1:
					current_node.add_child(self.Trie_node(0), letter)
				else:
					current_node.add_child(self.Trie_node(-1), letter)
				current_node = current_node.get_child(letter)
		print(self.trie.children)

	def query(self, letter: str) -> bool:
		self.letter_list.append(letter)
		if len(self.letter_list) > self.max_length:
			self.letter_list.pop(0)
		top_node = self.trie
		for letter in self.letter_list[::-1]:
			if top_node.is_child(letter):
				top_node = top_node.get_child(letter)
				if top_node.value == 0:
					return True
			else:
				return False
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = set(words)
		self.letters = ''
		self.fragments = {}
		for w in words:
			temp = self.fragments
			for i in range(len(w)):
				sec = w[len(w) - 1 - i:]
				if sec not in temp:
					temp[sec] = {}
				temp = temp[sec]

	def query(self, letter: str) -> bool:
		self.letters += letter
		chars = self.letters
		temp = self.fragments
		for i in range(len(chars)):
			sec = chars[len(chars) - 1 - i:]
			if sec not in temp:
				return False
			temp = temp[sec]
			if sec in self.words:
				return True


class TrieNode:

	def __init__(self):
		self.children = defaultdict(TrieNode)
		self.is_end = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.s = []
		for word in words:
			current = self.root
			for w in reversed(word):
				current = current.children[w]
			current.is_end = True

	def query(self, letter: str) -> bool:
		self.s.append(letter)
		current = self.root
		for w in reversed(self.s):
			if w not in current.children:
				break
			current = current.children[w]
			if current.is_end:
				return True
		return False


class TrieNode:

	def __init__(self, val):
		self.val = val
		self.isEnd = False
		self.children = dict()

class Trie:

	def __init__(self):
		self.root = TrieNode(None)

	def insert(self, word):
		i = 0
		curr = self.root
		while i < len(word):
			if word[i] not in curr.children:
				curr.children[word[i]] = TrieNode(word[i])
			curr = curr.children[word[i]]
			i += 1
		curr.isEnd = True

	def exists(self, stream):

		def solve(node, i):
			if i < 0 or stream[i] != node.val:
				return False
			elif node.val == stream[i] and node.isEnd:
				return True
			for (key, val) in node.children.items():
				if solve(val, i - 1):
					return True
			return False
		return any([solve(child, len(stream) - 1) for child in self.root.children.values()])

	def dfs(self):

		def solve(node, lvl):
			for child in node.children.values():
				solve(child, lvl + 1)
		solve(self.root, 0)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = []
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.trie.dfs()

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		return self.trie.exists(self.stream)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.s = ''
		self.dic = {}
		for word in words:
			try:
				if self.dic[word[-1]] == 1:
					pass
				self.dic[word[-1]].append(word)
			except:
				self.dic[word[-1]] = [word]

	def query(self, letter: str) -> bool:
		self.s += letter
		ans = False
		try:
			for w in self.dic[letter]:
				if self.s.endswith(w):
					ans = True
		except:
			pass
		return ans


class StreamChecker:

	class TrieNode:

		def __init__(self):
			self.children = {}
			self.endOfWord = False

	def __init__(self, words: List[str]):
		self.root = self.TrieNode()
		self.stream = []
		for word in words:
			node = self.root
			for char in word[::-1]:
				if char not in node.children:
					node.children[char] = self.TrieNode()
				node = node.children[char]
			node.endOfWord = True

	def query(self, letter: str) -> bool:
		node = self.root
		self.stream.insert(0, letter)
		for char in self.stream:
			if char not in node.children:
				return False
			node = node.children[char]
			if node.endOfWord == True:
				return True
				break
		return False


class StreamChecker:

	def char_int(self, char):
		return ord(char) - ord('a')

	def add_trie(self, trie, word, idx):
		if idx == len(word):
			trie[26] = True
			return
		idx_trie = self.char_int(word[idx])
		if not trie[idx_trie]:
			trie[idx_trie] = [None] * 27
		self.add_trie(trie[idx_trie], word, idx + 1)

	def __init__(self, words: List[str]):
		self.trie = [None] * 27
		for word in words:
			self.add_trie(self.trie, word[::-1], 0)
		self.str = []

	def query(self, letter: str) -> bool:
		self.str.append(letter)
		i = len(self.str) - 1
		trie = self.trie
		while i >= 0 and trie:
			t = trie[:]
			for j in range(len(t)):
				if t[j]:
					t[j] = chr(ord('a') + j)
			if trie[26]:
				return True
			trie = trie[self.char_int(self.str[i])]
			i -= 1
		if trie and trie[26]:
			return True
		return False


class Trie:

	def __init__(self, words: List[str]):
		self.trie = lambda : collections.defaultdict(self.trie)
		self.root = self.trie()
		for w in words:
			self.add(w[::-1])

	def add(self, s: str):
		curr = self.root
		for c in s:
			curr = curr[c]
		curr['#']

	def find(self, s: List[str]):
		curr = self.root
		for i in range(len(s) - 1, -1, -1):
			val = s[i]
			if '#' in curr:
				return True
			elif val in curr:
				curr = curr[val]
			else:
				return False
		return '#' in curr

class StreamChecker:

	def __init__(self, words: List[str]):
		self.queries = []
		self.trie = Trie(words)

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		return self.trie.find(self.queries)


class TrieNode:

	def __init__(self):
		self.children = {}
		self.isEnd = False

class Trie:

	def __init__(self):
		self.root = TrieNode()
		self.currentMatch = frozenset([self.root])

	def insert(self, word):
		cur = self.root
		for i in range(len(word)):
			idx = ord(word[i]) - ord('a')
			if idx not in cur.children:
				cur.children[idx] = TrieNode()
			cur = cur.children[idx]
		cur.isEnd = True

	@lru_cache(None)
	def matchNextChar(self, currentMatch, c):
		idx = ord(c) - ord('a')
		new_set = set([self.root])
		found = False
		for node in currentMatch:
			if idx in node.children:
				new_set.add(node.children[idx])
				if node.children[idx].isEnd:
					found = True
		return (frozenset(new_set), found)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word)

	def query(self, letter: str) -> bool:
		(match, found) = self.trie.matchNextChar(self.trie.currentMatch, letter)
		self.trie.currentMatch = match
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.s = ''
		self.trie = Trie()
		for wrd in words:
			self.trie.insert(wrd[::-1])

	def query(self, letter: str) -> bool:
		self.s += letter
		node = self.trie.root
		for ch in self.s[::-1]:
			if ch in node.childs:
				node = node.childs[ch]
				if node.isWord:
					return True
			else:
				break
		return False

class TrieNode:

	def __init__(self):
		self.childs = {}
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		r = self.root
		for ch in word:
			r = r.childs.setdefault(ch, TrieNode())
		r.isWord = True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		self.letters = ''
		for word in words:
			self.insert(word)

	def query(self, letter: str) -> bool:
		self.letters += letter
		curr = self.root
		for i in range(len(self.letters) - 1, -1, -1):
			if curr.child[ord(self.letters[i]) - ord('a')] == None:
				return False
			curr = curr.child[ord(self.letters[i]) - ord('a')]
			if curr.end:
				return True
		return False

	def insert(self, word):
		curr = self.root
		for i in range(len(word) - 1, -1, -1):
			c = word[i]
			nextt = curr.child[ord(c) - ord('a')]
			if nextt == None:
				nextt = Trie()
				curr.child[ord(c) - ord('a')] = nextt
			curr = curr.child[ord(c) - ord('a')]
		curr.end = True

class Trie:

	def __init__(self):
		self.end = False
		self.child = [None for i in range(26)]


class TrieNode:

	def __init__(self):
		self.child = collections.defaultdict(TrieNode)
		self.is_end = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.last_k = collections.deque()
		self.k = max((len(word) for word in words))
		for word in words:
			self._insert(word[::-1])

	def _insert(self, word):
		curr = self.root
		for ch in word:
			curr = curr.child[ch]
		curr.is_end = True

	def query(self, letter: str) -> bool:
		self.last_k.appendleft(letter)
		if len(self.last_k) > self.k:
			self.last_k.pop()
		curr = self.root
		for ch in self.last_k:
			if ch not in curr.child:
				return False
			curr = curr.child[ch]
			if curr.is_end:
				return True
		return curr.is_end


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie(words)
		self.maxLen = len(max(words, key=lambda s: (len(s), s)))
		self.queue = deque([])

	def query(self, letter: str) -> bool:
		if len(self.queue) >= self.maxLen:
			self.queue.popleft()
		self.queue.append(letter)
		if self.trie.findWord(self.trie.store, len(self.queue) - 1, ''.join(self.queue)):
			return True
		return False

class Trie:

	def __init__(self, words):
		self.store = {}
		for w in words:
			self.insertWord(self.store, len(w) - 1, w)

	def insertWord(self, trie, p, w):
		if p < 0:
			return
		if w[p] not in trie:
			trie[w[p]] = {}
		self.insertWord(trie[w[p]], p - 1, w)
		if p == 0:
			trie[w[p]]['$'] = True

	def findWord(self, trie, p, w):
		if p < 0 or w[p] not in trie:
			return False
		if '$' in trie[w[p]]:
			return True
		isAvail = False
		if w[p] in trie:
			isAvail = self.findWord(trie[w[p]], p - 1, w)
		return isAvail


class Node:

	def __init__(self, c):
		self.c = c
		self.childs = [None for i in range(26)]
		self.end = False

class Trie:

	def __init__(self):
		self.root = None
		self.p = []

	def insert(self, word):
		if self.root == None:
			self.root = Node('#')
		r = self.root
		for i in word[::-1]:
			if r.childs[ord(i) - 97] == None:
				r.childs[ord(i) - 97] = Node(i)
			r = r.childs[ord(i) - 97]
		r.end = True

	def search(self, word):
		if self.root == None:
			return False
		r = self.root
		for i in word[::-1]:
			if r.childs[ord(i) - 97] != None:
				r = r.childs[ord(i) - 97]
			else:
				return False
			if r.end:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = Trie()
		self.s = ''
		for word in words:
			self.tree.insert(word)

	def query(self, letter: str) -> bool:
		self.s += letter
		return self.tree.search(self.s)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.wordSet = set(words)
		lSet = set()
		self.lastL = set()
		for word in words:
			lSet.add(len(word))
			self.lastL.add(word[-1])
		self.l = list(lSet)
		self.l.sort()
		self.q = ''

	def query(self, letter: str) -> bool:
		self.q = self.q + letter
		if letter in self.lastL:
			for i in range(len(self.l)):
				if self.l[i] > len(self.q):
					break
				elif self.q[-self.l[i]:] in self.wordSet:
					return True
		return False


from collections import defaultdict

class TrieNode:

	def __init__(self):
		self.children = defaultdict(TrieNode)
		self.end = False

class Trie:

	def __init__(self, words):
		self.root = TrieNode()
		for word in words:
			self.add(word[::-1])
		self.word = ''
		self.l = max((len(word) for word in words))

	def add(self, word):
		node = self.root
		for c in word:
			node = node.children[c]
		node.end = True

	def search(self, c):
		self.word = (c + self.word)[:self.l]
		node = self.root
		for c in self.word:
			if c not in node.children:
				return False
			node = node.children[c]
			if node.end:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie(words)

	def query(self, letter: str) -> bool:
		return self.trie.search(letter)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.d = {}
		for i in words:
			if i[-1] not in self.d:
				self.d[i[-1]] = [i[:-1]]
			else:
				self.d[i[-1]].append(i[:-1])
		self.s = ''

	def query(self, letter: str) -> bool:
		self.s += letter
		if letter in self.d:
			for i in self.d[letter]:
				word = i + letter
				if len(self.s) >= len(word) and word == self.s[-len(word):]:
					return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		node = self.trie
		for i in words:
			for ch in i[::-1]:
				if ch not in node:
					node[ch] = {}
				node = node[ch]
			node['$'] = True
			node = self.trie
		self.stream = ''

	def query(self, letter: str) -> bool:
		self.stream += letter
		node = self.trie

		def check(s, node):
			if '$' in node:
				return True
			for ch in s:
				if ch in node:
					return check(s[1:], node[ch])
				else:
					return False
		return check(self.stream[::-1], node)


class Node:

	def __init__(self):
		self.children = {}
		self.stop = False

class Trie:

	def __init__(self):
		self.word = Node()

	def insert(self, word):
		word = word[::-1]
		curr = self.word
		for l in word:
			node = curr.children.get(l, Node())
			curr.children[l] = node
			curr = node
		curr.stop = True

	def search(self, word):
		curr = self.word
		for l in word:
			node = curr.children.get(l, None)
			if not node:
				return False
			elif node.stop:
				return True
			curr = node
		return curr.stop

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		for word in words:
			self.t.insert(word)
		self.pointer = ''

	def query(self, letter: str) -> bool:
		self.pointer += letter
		return self.t.search(self.pointer[::-1])


from collections import defaultdict

class StreamChecker:

	def __init__(self, words: List[str]):
		self.Queries = ''
		self.Words = defaultdict(list)
		for w in words:
			self.Words[w[-1]].append(w)

	def query(self, letter: str) -> bool:
		self.Queries += letter
		if len(self.Queries) > 2000:
			self.Queries = self.Queries[1:]
		for k in self.Words[letter]:
			i = len(k)
			if i <= len(self.Queries):
				if self.Queries[-i:] == k:
					return True
		return False


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add(self, word):
		cur = self.root
		for c in word:
			if cur.children[ord(c) - ord('a')] == None:
				cur.children[ord(c) - ord('a')] = TrieNode()
			cur = cur.children[ord(c) - ord('a')]
		cur.isWord = True

	def search(self, word):
		return self.__searchHelper(self.root, word, 0)

	def __searchHelper(self, cur, word, word_index):
		if cur.isWord:
			return True
		if len(word) == word_index:
			return cur.isWord
		char = word[word_index]
		temp = cur.children[ord(char) - ord('a')]
		return temp != None and self.__searchHelper(temp, word, word_index + 1)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.storage = Trie()
		self.prefix = ''
		for word in words:
			self.storage.add(word[::-1])

	def query(self, letter: str) -> bool:
		self.prefix += letter
		return self.storage.search(self.prefix[::-1])


class StreamChecker:
	from collections import deque

	def __init__(self, words: List[str]):
		self.max = len(max(words, key=len))
		self.words = tuple(words)
		self.letters = deque(maxlen=self.max)

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		l = ''.join(self.letters)
		return l.endswith(self.words)


class Node:

	def __init__(self, val=None, is_end=False):
		self.child = dict()
		self.val = val
		self.is_end = is_end

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Node()
		for w in words:
			tmp = self.root
			for l in w[::-1]:
				if l not in tmp.child:
					tmp.child[l] = Node(val=l)
				tmp = tmp.child[l]
			tmp.is_end = True
		self.q = []

	def query(self, letter: str) -> bool:
		if len(self.q) >= 2000:
			self.q.pop(0)
		self.q.append(letter)
		tmp = self.root
		for i in self.q[::-1]:
			if i not in tmp.child:
				return False
			tmp = tmp.child[i]
			if tmp.is_end:
				return True
		return tmp.is_end


import collections

class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = collections.defaultdict(list)
		self.characters = ''
		for word in words:
			self.words[word[-1]].append(word)

	def query(self, letter: str) -> bool:
		self.characters += letter
		for word in self.words[letter]:
			length = len(word)
			if self.characters[-length:] == word:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = {}
		for w in words:
			if len(w) not in list(self.words.keys()):
				self.words[len(w)] = []
			self.words[len(w)].append(w)
		self.buffer = ''
		self.i = 0
		self.max = max(list(self.words.keys()))
		self.res = {}

	def query(self, letter: str) -> bool:
		self.buffer += letter
		if len(self.buffer) > self.max:
			self.buffer = self.buffer[-self.max:]
		if self.buffer in list(self.res.keys()):
			return self.res[self.buffer]
		if len(self.buffer) not in list(self.words.keys()):
			return False
		for i in self.words:
			if len(self.buffer) >= i and self.buffer[-i:] in self.words[i]:
				self.res[self.buffer] = True
				return True
		self.res[self.buffer] = False
		return False


class TreeNode:

	def __init__(self):
		self.dict = defaultdict(TreeNode)
		self.result = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = TreeNode()
		self.text = ''
		for word in words:
			curr = self.tree
			for c in word[::-1]:
				curr = curr.dict[c]
			curr.result = True

	def query(self, letter: str) -> bool:
		self.text += letter
		curr = self.tree
		for c in self.text[::-1]:
			if c not in curr.dict:
				break
			curr = curr.dict[c]
			if curr.result:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.dictionary = {}
		for word in words:
			last_letter = word[-1]
			if last_letter not in self.dictionary:
				self.dictionary[last_letter] = [word]
			else:
				self.dictionary[last_letter].append(word)
		self.queries = ''

	def query(self, letter: str) -> bool:
		self.queries += letter
		if letter not in self.dictionary:
			return False
		else:
			for word in self.dictionary[letter]:
				length = len(word)
				if self.queries[-length:] == word:
					return True
			return False


class TrieNode:

	def __init__(self):
		self.kids = {}
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.q = collections.deque(maxlen=max((len(x) for x in words)))
		self.root = TrieNode()
		for x in words:
			cur = self.root
			for c in x[::-1]:
				if c not in cur.kids:
					cur.kids[c] = TrieNode()
				cur = cur.kids[c]
				if cur.isWord:
					break
			cur.isWord = True

	def query(self, letter: str) -> bool:

		def helper(word):
			cur = self.root
			for c in word:
				if c in cur.kids:
					cur = cur.kids[c]
					if cur.isWord:
						return True
				else:
					return False
			return False
		self.q.appendleft(letter)
		cur = self.root
		for c in self.q:
			if c in cur.kids:
				cur = cur.kids[c]
				if cur.isWord:
					return True
			else:
				return False
		return False


from collections import defaultdict

class Trie:

	def __init__(self):
		self.children = defaultdict(Trie)
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = ''
		self.trie = Trie()
		for word in words:
			curr = self.trie
			for ch in word[::-1]:
				curr = curr.children[ch]
			curr.is_word = True

	def query(self, letter: str) -> bool:
		self.stream += letter
		curr = self.trie
		for ch in self.stream[::-1]:
			if ch not in curr.children:
				return False
			curr = curr.children[ch]
			if curr.is_word:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.child = collections.defaultdict(TrieNode)
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.res = []
		for word in words:
			self.add(word)

	def query(self, letter: str) -> bool:
		curr = self.root
		self.res.insert(0, letter)
		for char in self.res:
			if curr.isWord:
				return True
			if not curr.child.get(char):
				return False
			curr = curr.child[char]
		return curr.isWord

	def add(self, word):
		curr = self.root
		for char in word[::-1]:
			curr = curr.child[char]
		curr.isWord = True


class Trie:

	def __init__(self, words):
		self.trie = {}
		for word in words:
			self.add(word)

	def add(self, word):

		def add_helper(prev, i):
			if i == len(word) - 1:
				prev[word[i]] = {None: None}
				return
			char = word[i]
			if char not in prev:
				prev[char] = {}
			add_helper(prev[char], i + 1)
		add_helper(self.trie, 0)

	def contains(self, word):

		def dfs(prev, i):
			if i == len(word):
				return False
			if word[i] in prev and None in prev[word[i]]:
				return True
			return dfs(prev[word[i]], i + 1) if word[i] in prev else False
		return dfs(self.trie, 0)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie([''.join(reversed(w)) for w in words])
		self.word = ''

	def query(self, letter: str) -> bool:
		self.word = ''.join([letter, self.word])
		return self.trie.contains(self.word)


from collections import deque, defaultdict

class StreamChecker:

	def __init__(self, words: List[str]):
		words = sorted(words, key=len)
		self.words_by_len = defaultdict(set)
		for word in words:
			if all((word[-length:] not in words_set for (length, words_set) in list(self.words_by_len.items()))):
				self.words_by_len[len(word)].add(word)
		self.search_history = deque(maxlen=max(self.words_by_len.keys()))

	def query(self, letter: str) -> bool:
		self.search_history.append(letter)
		history = ''.join(self.search_history)
		return any((history[-length:] in words_set for (length, words_set) in list(self.words_by_len.items())))


class TrieNode:

	def __init__(self):
		self.endOfWord = False
		self.children = {}

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = TrieNode()
		self.stream = collections.deque()
		for word in words:
			node = self.trie
			for c in word[::-1]:
				if not c in node.children:
					childNode = TrieNode()
					node.children[c] = childNode
				node = node.children[c]
			node.endOfWord = True

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.trie
		for c in self.stream:
			if not c in node.children:
				return False
			node = node.children[c]
			if node.endOfWord:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = set(words)
		self.max_len = max((len(x) for x in self.words), default=0)
		self.q = ''
		self.num_end = min(1, self.max_len)
		self.ends = {}
		for i in range(1, self.num_end + 1):
			self.ends[i] = set((x[-i:] for x in self.words if len(x) >= i))

	def query(self, letter: str) -> bool:
		self.q += letter
		if len(self.q) > self.max_len:
			self.q = self.q[1:]
		for i in range(1, self.num_end + 1):
			if self.q[-i:] not in self.ends[i]:
				return False
		for i in range(0, self.max_len):
			if self.q[~i:] in self.words:
				return True
		return False


class Trie:

	def __init__(self):
		self.wordEndsHere = False
		self.children = dict()

	def addWord(self, word):
		if not word:
			self.wordEndsHere = True
		else:
			if word[0] not in self.children:
				self.children[word[0]] = Trie()
			self.children[word[0]].addWord(word[1:])

	def findWord(self, word):
		if self.wordEndsHere:
			return True
		if not word:
			return self.wordEndsHere
		elif word[0] not in self.children:
			return False
		else:
			return self.children[word[0]].findWord(word[1:])

class StreamChecker:

	def __init__(self, words: List[str]):
		self.maxLength = 0
		self.letters = []
		self.trie = Trie()
		for w in words:
			self.trie.addWord(w[::-1])
			self.maxLength = max(self.maxLength, len(w))

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		sliceLength = min(len(self.letters), self.maxLength)
		target = ''.join(self.letters[len(self.letters) - sliceLength:])[::-1]
		return self.trie.findWord(target)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.memo = {}
		self.words = set()
		words_ = [word[::-1] for word in words]
		for word in words_:
			for i in range(len(word) - 1):
				self.memo[word[:i + 1]] = 0
		for word in words_:
			self.memo[word] = 1
		self.hist = []

	def query(self, letter: str) -> bool:
		self.hist.insert(0, letter)
		for i in range(len(self.hist)):
			c = self.memo.get(''.join(self.hist[:i + 1]), None)
			if c is None:
				return False
			if c:
				return True
		return False


from collections import deque, defaultdict

class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = words
		self.stream = ''
		(self.letters, self.end_letter) = self.prep()

	def prep(self):
		letters = set()
		end_letter = defaultdict(list)
		for (i, word) in enumerate(self.words):
			letters = letters.union(set(word))
			end_letter[word[-1]].append(i)
		return (letters, end_letter)

	def query(self, letter: str) -> bool:
		self.stream += letter
		if letter not in self.letters:
			return False
		for i in self.end_letter.get(letter, []):
			if self.stream[-len(self.words[i]):] == self.words[i]:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.tries = [False, [[] for _ in range(26)]]
		for word in words:
			self.build(word[::-1])
		self.stream = ''

	def build(self, word):
		temp_level = self.tries
		for c in word:
			if len(temp_level[1][ord(c) - ord('a')]) == 0:
				temp_level[1][ord(c) - ord('a')] = [False, [[] for _ in range(26)]]
			temp_level = temp_level[1][ord(c) - ord('a')]
		temp_level[0] = True

	def query(self, letter: str) -> bool:
		self.stream += letter
		temp_level = self.tries
		for c in self.stream[::-1]:
			if len(temp_level[1][ord(c) - ord('a')]) == 0:
				return False
			temp_level = temp_level[1][ord(c) - ord('a')]
			if temp_level[0]:
				return True
		return False


class Node:

	def __init__(self, val):
		self.val = val
		self.children = [None] * 26
		self.isWord = False

class Trie:

	def __init__(self, words):
		self.root = Node('')
		for word in words:
			p = self.root
			for ind in range(len(word) - 1, -1, -1):
				ch = word[ind]
				if p.children[ord(ch) - ord('a')] is None:
					new_node = Node(ch)
					p.children[ord(ch) - ord('a')] = new_node
				p = p.children[ord(ch) - ord('a')]
				if ind == 0:
					p.isWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie(words)
		self.pointer_queue = []
		self.stream = []

	def query(self, letter: str) -> bool:
		self.stream.insert(0, letter)
		p = self.trie.root
		for ch in self.stream:
			if p.children[ord(ch) - ord('a')]:
				p = p.children[ord(ch) - ord('a')]
			else:
				return False
			if p.isWord:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.q = ''
		for i in words:
			if i[-1] not in self.trie:
				self.trie[i[-1]] = [i[:-1]]
			else:
				self.trie[i[-1]].append(i[:-1])

	def query(self, letter: str) -> bool:
		trie = self.trie
		self.q += letter
		if letter not in trie:
			return False
		for i in trie[letter]:
			l = len(i)
			if self.q[-l - 1:-1] == i:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = set(words)
		self.maxx = len(max(self.words, key=len))
		self.tails = {word[-1] for word in words}
		self.qs = ''

	def query(self, letter: str) -> bool:
		self.qs += letter
		if len(self.qs) > self.maxx:
			self.qs = self.qs[1:]
		if letter not in self.tails:
			return False
		N = len(self.qs)
		return any((self.qs[i:N] in self.words for i in range(N - 1, -1, -1)))


from collections import defaultdict

class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = words
		self.rev_words = {}
		self.n = 0
		self.cur_word = ''
		self.dic = defaultdict(list)
		for (i, word) in enumerate(words):
			rev_word = word[::-1]
			self.rev_words[i] = rev_word
			self.dic[rev_word[0]].append(i)
			if len(word) > self.n:
				self.n = len(word)

	def query(self, letter: str) -> bool:
		if len(self.cur_word) == self.n:
			self.cur_word = self.cur_word[1:]
		self.cur_word += letter
		if letter in self.dic:
			index = self.dic[letter]
			for i in index:
				word = self.rev_words[i]
				if len(word) <= len(self.cur_word):
					if self.cur_word[::-1][:len(word)] == word:
						return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.dict = {}
		self.longest = 0
		for word in words:
			if self.longest < len(word):
				self.longest = len(word)
			for i in range(len(word) - 1, 0, -1):
				if self.dict.get(len(word[i:])) == None:
					self.dict[len(word[i:])] = {word[i:]: 0}
				elif self.dict[len(word[i:])].get(word[i:]) == None:
					self.dict[len(word[i:])][word[i:]] = 0
			if self.dict.get(len(word)) == None:
				self.dict[len(word)] = {word: 1}
			else:
				self.dict[len(word)][word] = 1
		self.letters = ''

	def query(self, letter: str) -> bool:
		if len(self.letters) == self.longest:
			self.letters = self.letters[1:]
		self.letters += letter
		i = len(self.letters) - 1
		while i >= 0 and self.dict[len(self.letters[i:])].get(self.letters[i:]) != None:
			if self.dict[len(self.letters[i:])][self.letters[i:]] == 1:
				return True
			i -= 1
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie([word[::-1] for word in words])
		self.stream = []

	def query(self, letter: str) -> bool:
		self.stream.insert(0, letter)
		curr = self.trie.head
		i = 0
		while curr and i < len(self.stream):
			curr = curr.get_element(self.stream[i])
			if not curr:
				return False
			if curr.is_word:
				return True
			i += 1

class TrieNode:
	ORDA = ord('a')

	def __init__(self, is_word: bool=False):
		self.is_word = is_word
		self.children = [None] * 26

	def get_element(self, c: str):
		idx = ord(c) - TrieNode.ORDA
		return self.children[idx]

	def ensure_element(self, c: str):
		idx = ord(c) - TrieNode.ORDA
		if not self.children[idx]:
			self.children[idx] = TrieNode()
		return self.children[idx]

class Trie:

	def __init__(self, words: List[str]):
		self.head = TrieNode()
		for word in words:
			self.add_word(word)

	def add_word(self, word: str):
		curr = self.head
		for c in word:
			curr = curr.ensure_element(c)
		curr.is_word = True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.end = '#'
		self.chars = ''
		for word in words:
			word = word[::-1]
			cnode = self.root
			word += self.end
			for c in word:
				if c in cnode:
					cnode = cnode[c]
				else:
					cnode[c] = {}
					cnode = cnode[c]

	def query(self, letter: str) -> bool:
		self.chars += letter
		cnode = self.root
		for c in self.chars[::-1]:
			if c in cnode:
				cnode = cnode[c]
			else:
				return False
			if '#' in cnode:
				return True


class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return 0
			pCrawl = pCrawl.children[index]
		if pCrawl != None and pCrawl.isEndOfWord:
			return 1
		if pCrawl != None:
			return 2
		return 0

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		for word in words:
			self.root.insert(word[::-1])
		self.streams = []
		self.l = 0

	def query(self, letter: str) -> bool:
		self.streams.append(letter)
		self.l += 1
		for i in range(self.l, -1, -1):
			word = list(reversed(self.streams[i:self.l]))
			if word == []:
				continue
			k = self.root.search(word)
			if k == 1:
				return True
			if k == 0:
				break
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.tire = {}
		self.tire_reverse = {}
		self.max_size = 0
		for w in words:
			self._insert(w)
			self._insert_reverse(w[::-1])
			self.max_size = max(self.max_size, len(w))
		self.q = collections.deque([], self.max_size)
		self.curr = self.tire

	def _insert(self, word):
		curr = self.tire
		for c in word:
			if c not in curr:
				curr[c] = {}
			curr = curr[c]
		curr['$'] = True

	def _insert_reverse(self, word):
		curr = self.tire_reverse
		for c in word:
			if c not in curr:
				curr[c] = {}
			curr = curr[c]
		curr['$'] = True

	def _search(self, w):
		curr = self.tire_reverse
		for c in w:
			if c in curr:
				curr = curr[c]
				if '$' in curr:
					return True
			else:
				return False
		return False

	def query(self, letter: str) -> bool:
		self.q.append(letter)
		if letter in self.curr:
			self.curr = self.curr[letter]
			if '$' in self.curr:
				return True
			else:
				self.curr = self.tire
		w = [c for c in self.q]
		return self._search(w[::-1])


import itertools

class TrieNode:

	def __init__(self):
		self.end = False
		self.nodes = {}

	def insert(self, word):
		if len(word) == 0:
			self.end = True
			return
		if word[0] not in self.nodes:
			self.nodes[word[0]] = TrieNode()
		self.nodes[word[0]].insert(word[1:])

	def startsWith(self, word):
		if len(word) == 0:
			return True
		if word[0] not in self.nodes:
			return False
		return self.nodes[word[0]].startsWith(word[1:])

	def exists(self, word):
		if self.end and len(word) == 0:
			return True
		if len(word) == 0 or word[0] not in self.nodes:
			return False
		return self.nodes[word[0]].exists(word[1:])

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.longest = 0
		for word in words:
			self.longest = max(self.longest, len(word))
			self.root.insert(word[::-1])
		self.word = ''

	def query(self, letter: str) -> bool:
		self.word = (self.word + letter)[-self.longest:]
		for i in range(len(self.word) - 1, -1, -1):
			other = self.word[i:][::-1]
			if not self.root.startsWith(other):
				break
			if self.root.exists(other):
				return True
		return False


class Trie:

	def __init__(self):
		self.is_word = False
		self.child = defaultdict(Trie)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = ''
		for w in words:
			cur_node = self.trie
			for c in w[::-1]:
				cur_node = cur_node.child[c]
			cur_node.is_word = True

	def query(self, letter: str) -> bool:
		self.stream += letter
		node = self.trie
		for c in reversed(self.stream):
			if c not in node.child:
				return False
			node = node.child[c]
			if node.is_word:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for w in words:
			self.trie.insert(w[::-1])
		self.history = ''

	def query(self, letter: str) -> bool:
		self.history += letter
		return self.trie.search(self.history[::-1])

class Node:

	def __init__(self):
		self.children = {}
		self.exist = 0

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, w):
		curr_node = self.root
		for c in w:
			if c not in curr_node.children:
				curr_node.children[c] = Node()
			curr_node = curr_node.children[c]
		curr_node.exist = 1

	def search(self, w):
		curr_node = self.root
		for c in w:
			if c not in curr_node.children:
				return False
			curr_node = curr_node.children[c]
			if curr_node.exist:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add(word[::-1])
		self.stream = ''

	def query(self, letter: str) -> bool:
		node = self.trie.root
		self.stream += letter
		for char in self.stream[::-1]:
			if char in node.c:
				node = node.c[char]
			else:
				break
			if node.isword:
				return True
		return False

class Trie:

	def __init__(self):
		self.root = TrieNode('')

	def add(self, word):
		node = self.root
		for char in word:
			if char not in node.c:
				node.c[char] = TrieNode(char)
			node = node.c[char]
		node.isword = True

class TrieNode:

	def __init__(self, v):
		self.v = v
		self.c = dict()
		self.isword = False


from collections import defaultdict

class TrieNode:

	def __init__(self):
		self.children = defaultdict(TrieNode)
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.mx_length = 0
		for word in words:
			self.insert(word[::-1])
			self.mx_length = max(self.mx_length, len(word))
		self.buffer = ''

	def insert(self, word):
		curr_node = self.root
		for ch in word:
			curr_node = curr_node.children[ch]
		curr_node.isWord = True

	def search(self, word):
		cur_node = self.root
		for ch in word:
			cur_node = cur_node.children.get(ch)
			if not cur_node:
				return False
			if cur_node.isWord:
				return True
		return False

	def query(self, letter: str) -> bool:
		self.buffer += letter
		return self.search(self.buffer[::-1])


class Trie:

	def __init__(self):
		self.dict = {}
		self.leaf = False

	def addWord(self, word):
		newTrie = Trie()
		if word[0] in self.dict:
			newTrie = self.dict[word[0]]
		else:
			self.dict[word[0]] = newTrie
		if len(word) == 1:
			newTrie.leaf = True
		else:
			newTrie.addWord(word[1:])

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.queryStr = ''
		for word in words:
			self.trie.addWord(word[::-1])

	def query(self, letter: str) -> bool:
		self.queryStr += letter
		curTrie = self.trie
		for ch in self.queryStr[::-1]:
			if ch in curTrie.dict:
				curTrie = curTrie.dict[ch]
				if curTrie.leaf == True:
					return True
			else:
				return False


class TrieNode:

	def __init__(self, parent):
		self.parent = parent
		self.children = collections.defaultdict(lambda : TrieNode(self))
		self.c = None

class Trie:

	def __init__(self):
		self.root = TrieNode(None)
		self.ends = collections.defaultdict(list)

	def add(self, word):
		curr = self.root
		for c in word:
			curr = curr.children[c]
			curr.c = c
		if len(word) > 0:
			self.ends[word[-1]].append(curr)

	def isMember(self, letters):
		if letters[-1] not in self.ends:
			return False
		poss = self.ends[letters[-1]]
		for p in poss:
			curr = p
			index = len(letters) - 2
			while curr and index >= 0:
				curr = curr.parent
				if curr == self.root:
					return True
				if curr.c != letters[index]:
					curr = None
				index -= 1
			if index == -1 and curr != None and (curr.parent == self.root):
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for w in words:
			self.trie.add(w)
		self.maxLen = max(list([len(x) for x in words]))
		self.queries = []

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		if len(self.queries) > self.maxLen:
			del self.queries[0]
		return self.trie.isMember(self.queries)


class TrieNode:

	def __init__(self):
		self.nodes = dict()
		self.is_leaf = False

	def insert(self, word):
		curr = self
		for char in word:
			if char not in curr.nodes:
				curr.nodes[char] = TrieNode()
			curr = curr.nodes[char]
		curr.is_leaf = True

	def check(self, word):
		curr = self
		for c in word:
			if c not in curr.nodes:
				return False
			curr = curr.nodes[c]
			if curr.is_leaf:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		for word in words:
			self.root.insert(word[::-1])
		self.sb = ''

	def query(self, letter: str) -> bool:
		self.sb += letter
		node = self.root
		return node.check(self.sb[::-1])


class StreamTrie:

	def __init__(self, mychar='ROOT'):
		self.char = mychar
		self.children = {}
		self.mass = 0
		if mychar == 'ROOT':
			self.max_len = 0

	def add(self, word, top_level=True):
		if len(word) == 0:
			self.children['END'] = True
		else:
			self.mass += 1
			c = word[0]
			if not c in self.children:
				self.children[c] = StreamTrie(c)
			self.children[c].add(word[1:], top_level=False)
		if top_level:
			self.max_len = max(self.max_len, len(word))

	def get_max_len(self):
		return self.max_len

	def matches(self, word):
		if len(word) == 0 and 'END' in self.children:
			return True
		if len(word) > 0 and word[0] in self.children:
			return self.children[word[0]].matches(word[1:])
		return False

	def all_matches_from_beginning(self, query_string):
		match_list = []
		first = query_string[0]
		if first in self.children:
			self.children[first]._match_all(query_string, 0, match_list)
		return match_list

	def _match_all(self, query_string, cur_ind, match_set):
		if 'END' in self.children:
			match_set.append(query_string[:cur_ind + 1])
		if cur_ind + 1 < len(query_string):
			nxt = query_string[cur_ind + 1]
			if nxt in self.children:
				self.children[nxt]._match_all(query_string, cur_ind + 1, match_set)

	def any_match_from_beginning(self, query_string):
		return self.children[query_string[0]]._match_any(query_string, 0) if query_string[0] in self.children else False

	def _match_any(self, query_string, cur_ind):
		if 'END' in self.children:
			return True
		if cur_ind + 1 < len(query_string) and query_string[cur_ind + 1] in self.children:
			return self.children[query_string[cur_ind + 1]]._match_any(query_string, cur_ind + 1)
		else:
			return False

	def __repr__(self):
		return self.__str__()

	def __str__(self):
		if 'END' in self.children:
			return f'[{self.char}*]'
		s = f'[{self.char}:{self.mass} '
		if self.char == 'ROOT':
			s = f'Trie({self.mass} '
		kids = []
		for k in self.children:
			if not k == 'END':
				kids.append(str(self.children[k]))
		s += ', '.join(kids)
		s += ')' if self.char == 'ROOT' else ']'
		return s

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = StreamTrie()
		self.longest = 0
		for word in words:
			self.trie.add(word[::-1])
			if len(word) > self.longest:
				self.longest = len(word)
		self.stream = deque()

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		if len(self.stream) > self.longest:
			self.stream.pop()
		return self.trie.any_match_from_beginning(''.join(self.stream))


class Trie:

	def __init__(self):
		self.chars = {}

	def add(self, word):
		cur = self.chars
		for ch in word:
			cur.setdefault(ch, {})
			cur = cur[ch]
		cur['#'] = '#'

	def find(self, word):
		cur = self.chars
		for ch in word:
			if ch not in cur:
				return False
			cur = cur[ch]
		return '#' in cur

class StreamChecker:

	def __init__(self, words):
		self.trie = Trie()
		for word in words:
			self.trie.add(reversed(word))
		self.max_len = max(words, key=lambda x: len(x))
		self.buf = []

	def query(self, letter):
		self.buf.append(letter)
		cur = self.trie.chars
		for i in range(len(self.buf) - 1, -1, -1):
			ch = self.buf[i]
			if ch not in cur:
				return False
			if '#' in cur[ch]:
				return True
			cur = cur[ch]
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.curr = ''
		self.words = words
		temp = []
		for w in words:
			temp.append(w[-1])
		self.last = temp
		self.MAX = 1
		self.lenth = []
		self.storage = collections.defaultdict(list)
		for w in words:
			if len(w) not in self.lenth:
				self.lenth.append(len(w))
			self.storage[len(w)].append(w)
			self.MAX = max(self.MAX, len(w))

	def query(self, letter: str) -> bool:
		if len(self.curr) < self.MAX:
			self.curr += letter
		elif len(self.curr) == self.MAX:
			self.curr = self.curr[1:] + letter
		if letter in self.last:
			for i in range(1, len(self.curr) + 1):
				if self.curr[-i:] in self.storage[i]:
					return True
				else:
					continue
			return False
		else:
			return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			trie = self.trie
			for letter in word:
				trie = trie.setdefault(letter, {})
			trie[' '] = {}
		self.check = []

	def query(self, letter: str) -> bool:
		self.check.append(self.trie)
		self.check = [trie[letter] for trie in self.check if letter in trie]
		for trie in self.check:
			if ' ' in trie:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = {}
		for word in words:
			trieNode = self.Trie
			for ch in word[::-1]:
				if ch not in trieNode:
					trieNode[ch] = {}
				trieNode = trieNode[ch]
			trieNode['END'] = True
		self.letter = ''

	def query(self, letter: str) -> bool:
		self.letter += letter
		trieNode = self.Trie
		for ch in self.letter[::-1]:
			if ch not in trieNode:
				return False
			trieNode = trieNode[ch]
			if 'END' in trieNode:
				return True
		return False


class Trie:

	def __init__(self):
		self.dict = {}
		self.leaf = False

	def addWord(self, word):
		curTrie = self
		for ch in word:
			if ch not in curTrie.dict:
				curTrie.dict[ch] = Trie()
			curTrie = curTrie.dict[ch]
		curTrie.leaf = True

	def search(self, query):
		curTrie = self
		for ch in query:
			if ch in curTrie.dict:
				curTrie = curTrie.dict[ch]
				if curTrie.leaf == True:
					return True
			else:
				return False
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.queryStr = ''
		for word in words:
			self.trie.addWord(word[::-1])

	def query(self, letter: str) -> bool:
		self.queryStr += letter
		curTrie = self.trie
		return curTrie.search(self.queryStr[::-1])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = defaultdict(dict)
		for word in words:
			node = self.trie
			for i in word[::-1]:
				if i not in node:
					node[i] = {}
				node = node[i]
			node['#'] = word
		self.q = ''

	def query(self, letter: str) -> bool:
		self.q += letter
		node = self.trie
		for i in self.q[::-1]:
			if '#' in node:
				return True
			if i not in node:
				return False
			node = node[i]
		return '#' in node


class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = ''
		self.trie = {}
		curr = self.trie
		for word in words:
			for char in word[::-1]:
				curr.setdefault(char, {})
				curr = curr[char]
			curr['end'] = True
			curr = self.trie

	def query(self, letter: str) -> bool:
		self.stream += letter

		def _search(string, curr):
			if 'end' in curr:
				return True
			if string and string[0] in curr:
				return _search(string[1:], curr[string[0]])
			return False
		return _search(self.stream[::-1], self.trie)


from collections import defaultdict

class TrieNode:

	def __init__(self):
		self.nexts = defaultdict(TrieNode)
		self.isword = False

class Trie:

	def __init__(self, words):
		self.root = TrieNode()
		for w in words:
			self.insert(w[::-1])

	def insert(self, w):
		cur = self.root
		for c in w:
			cur = cur.nexts[c]
		cur.isword = True

	def search(self, w):
		cur = self.root
		for i in range(len(w) - 1, -1, -1):
			if w[i] in cur.nexts:
				cur = cur.nexts[w[i]]
				if cur.isword:
					return True
			else:
				return False
		return cur.isword

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie(words)
		self.cache = list()

	def query(self, letter: str) -> bool:
		self.cache.append(letter)
		return self.t.search(self.cache)


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		p = self.root
		for w in word[::-1]:
			if w not in p.children:
				p.children[w] = TrieNode()
			p = p.children[w]
		p.isWord = True

	def search(self, word):
		p = self.root
		for w in word[::-1]:
			if w not in p.children:
				break
			p = p.children[w]
			if p.isWord:
				return True
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word)
		self.letters = ''

	def query(self, letter: str) -> bool:
		self.letters += letter
		return self.trie.search(self.letters)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.d = dict()
		for word in words:
			cur_d = self.d
			for ch in word[::-1]:
				if ch in cur_d:
					cur_d = cur_d[ch]
				else:
					cur_d[ch] = {}
					cur_d = cur_d[ch]
			cur_d['end'] = True
		self.query_cache = ''

	def query(self, letter: str) -> bool:
		self.query_cache += letter
		cur_d = self.d
		for ch in self.query_cache[::-1]:
			if ch in cur_d:
				if cur_d[ch].get('end'):
					return True
				cur_d = cur_d[ch]
			else:
				break
		return False


class TrieNode:

	def __init__(self):
		self.children = dict()
		self.is_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def add_word(self, word):
		node = self.root
		for c in word:
			if c not in node.children:
				node.children[c] = TrieNode()
			node = node.children[c]
		node.is_word = True

	def find_any(self, word):
		node = self.root
		for c in word:
			if node.is_word:
				return True
			if c not in node.children:
				return False
			node = node.children[c]
		return node.is_word

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.add_word(word[::-1])
		self.cur = ''

	def query(self, letter: str) -> bool:
		self.cur += letter
		return self.trie.find_any(self.cur[::-1])


class TrieNode:

	def __init__(self):
		self.children = {}
		self.terminal = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.queue = collections.deque()
		self.words = words
		self.root = TrieNode()
		for word in self.words:
			node = self.root
			for c in reversed(word):
				if c not in node.children:
					node.children[c] = TrieNode()
				node = node.children[c]
			node.terminal = True

	def query(self, letter: str) -> bool:
		self.queue.appendleft(letter)
		node = self.root
		if letter in node.children:
			for l in self.queue:
				if l in node.children:
					node = node.children[l]
					if node.terminal:
						return True
				else:
					return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = set(words)
		self.queries = ''

	def query(self, letter: str) -> bool:
		self.queries += letter
		for word in self.stream:
			if letter == word[~0]:
				if word == self.queries[-len(word):]:
					return True
		return False


EOW = '*'

class StreamChecker:
	queries = []

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			node = self.root
			for c in word:
				if c in node:
					node = node[c]
				else:
					node[c] = {}
					node = node[c]
			if not EOW in node:
				node[EOW] = True

	def query(self, letter: str) -> bool:
		self.queries = [q[letter] for q in self.queries if letter in q]
		if letter in self.root:
			self.queries.append(self.root[letter])
		return any([q for q in self.queries if EOW in q])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.END = '0'
		self.q = ''
		for word in words:
			curr_dict = self.root
			for letter in word[::-1]:
				curr_dict = curr_dict.setdefault(letter, {})
			curr_dict[self.END] = self.END

	def query(self, letter: str) -> bool:
		self.q += letter
		curr_node = self.root
		for l in self.q[::-1]:
			if l in curr_node:
				curr_node = curr_node.get(l)
			else:
				return False
			if self.END in curr_node:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.memo = ''
		for w in words:
			ptr = self.root
			for c in w[::-1]:
				if c not in ptr:
					ptr[c] = {}
				ptr = ptr[c]
			ptr['#'] = ''

	def query(self, letter: str) -> bool:
		self.memo += letter
		ptr = self.root
		for c in self.memo[::-1]:
			if c not in ptr:
				return False
			ptr = ptr[c]
			if '#' in ptr:
				return True
		return False


from collections import deque

class Node:

	def __init__(self) -> object:
		self.children = dict()
		self.isEND = False
		self.fail = None

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, words):
		current_node = self.root
		for w in words:
			if current_node.children.get(w, None) is None:
				current_node.children[w] = Node()
			current_node = current_node.children[w]
		current_node.isEND = True

	def built_failover(self):
		q = deque()
		current = self.root
		for ch in self.root.children:
			self.root.children[ch].fail = self.root
			q.append(self.root.children[ch])
		while q:
			node = q.popleft()
			for ch in node.children:
				q.append(node.children[ch])
				failover = node.fail
				while True:
					if failover is None:
						node.children[ch].fail = self.root
						break
					if ch in failover.children:
						node.children[ch].fail = failover.children[ch]
						break
					else:
						failover = failover.fail

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word)
		self.trie.built_failover()
		self.curr = self.trie.root

	def query(self, letter: str) -> bool:
		result = False
		while True:
			if letter in self.curr.children:
				self.curr = self.curr.children[letter]
				if self.curr.isEND:
					result = True
				if self.curr.fail != None and self.curr.fail.isEND:
					result = True
				break
			elif self.curr.fail == None:
				self.curr = self.trie.root
				break
			else:
				self.curr = self.curr.fail
		return result


class StreamChecker:

	def __init__(self, words: List[str]):
		T = lambda : collections.defaultdict(T)
		self.trie = T()
		self.waiting = []
		for w in words:
			d = self.trie
			for c in w:
				d = d[c]
			d = d['#']

	def query(self, letter: str) -> bool:
		self.waiting = [node[letter] for node in self.waiting + [self.trie] if letter in node]
		return any((d for d in self.waiting if '#' in d))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = {}
		for word in words:
			curr = self.t
			for i in word[::-1]:
				if i not in curr:
					curr[i] = {}
				curr = curr[i]
			curr['end'] = True
		self.s = ''

	def query(self, letter: str) -> bool:
		self.s += letter
		curr = self.t
		for i in self.s[::-1]:
			if i not in curr:
				return False
			if i in curr:
				curr = curr[i]
			if 'end' in curr:
				return True
		return False


class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.prefix = ''
		for word in words:
			node = self.root
			for c in word[::-1]:
				if c not in node.children:
					node.children[c] = TrieNode()
				node = node.children[c]
			node.is_word = True

	def query(self, letter: str) -> bool:
		self.prefix += letter
		node = self.root
		for c in self.prefix[::-1]:
			if c not in node.children:
				return False
			node = node.children[c]
			if node.is_word:
				return True
		return False


class Trie:

	def __init__(self, word, end):
		self.children = word
		self.end = end

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie({}, False)
		for word in words:
			cur = self.trie
			for char in word[::-1]:
				if not char in cur.children:
					cur.children[char] = Trie({}, False)
				cur = cur.children[char]
			cur.end = True
		self.history = ''

	def query(self, letter: str) -> bool:
		self.history += letter
		streamer = self.trie
		for c in self.history[::-1]:
			if c not in streamer.children:
				return False
			elif streamer.children[c].end:
				return True
			streamer = streamer.children[c]
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.words = [word[::-1] for word in words]
		self.string = ''
		self.l = 0
		self.createTrie()

	def createTrie(self):
		for i in self.words:
			t = self.trie
			for j in i:
				if j not in t:
					t[j] = dict()
				t = t[j]
			t['*'] = dict()

	def query(self, letter: str) -> bool:
		self.string += letter
		self.l += 1
		trie = self.trie
		for i in self.string[::-1]:
			if i in trie:
				trie = trie[i]
				if '*' in trie:
					return True
			else:
				return False
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			node = self.trie
			for c in word[::-1]:
				if c not in node:
					node[c] = {}
				node = node[c]
			node['$'] = True
		self.queries = ''
		self.W = max(list(map(len, words)))

	def query(self, letter: str) -> bool:
		ret = False
		self.queries += letter
		node = self.trie
		for c in self.queries[::-1][:self.W]:
			if c in node:
				node = node[c]
				if '$' in node:
					return True
			else:
				return False
		return False


from collections import deque, defaultdict

class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = words
		self.stream = deque(maxlen=2000)
		self.words_dict = defaultdict(list)
		for word in self.words:
			self.words_dict[word[-1]].append(word)

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		for word in self.words_dict[letter]:
			if len(word) <= len(self.stream):
				for i in range(len(word)):
					if word[-(i + 1)] != self.stream[-(i + 1)]:
						break
				else:
					return True
		return False


class TreeNode:

	def __init__(self):
		self.children = dict()
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TreeNode()
		self.sofar = ''
		for word in words:
			self.add(word)

	def add(self, word):
		node = self.root
		for w in word[::-1]:
			if w not in node.children:
				node.children[w] = TreeNode()
			node = node.children[w]
		node.isWord = True

	def query(self, letter: str) -> bool:
		self.sofar += letter
		node = self.root
		for c in self.sofar[::-1]:
			if c in node.children:
				node = node.children[c]
				if node.isWord:
					return True
			else:
				return False
		return node.isWord


class Node(object):

	def __init__(self):
		self.children = {}
		self.end = False

	def add(self, word):
		cur = self
		for c in word:
			if c not in cur.children:
				cur.children[c] = Node()
			cur = cur.children[c]
		cur.end = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Node()
		for word in words:
			self.root.add(word[::-1])
		self.s = []
		self.cur = self.root

	def query(self, letter: str) -> bool:
		self.s.append(letter)
		i = len(self.s) - 1
		cur = self.root
		while i >= 0:
			c = self.s[i]
			if c not in cur.children:
				return False
			cur = cur.children[c]
			if cur.end:
				return True
			i -= 1
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.pointers = []
		for word in words:
			curr = self.trie
			for char in word + '0':
				if char not in curr:
					curr[char] = {}
				curr = curr[char]

	def query(self, letter: str) -> bool:
		new_pointers = []
		hasFound = False
		for pointer in self.pointers:
			if letter in pointer:
				new_pointers.append(pointer[letter])
		if letter in self.trie:
			new_pointers.append(self.trie[letter])
		self.pointers = new_pointers
		for pointer in self.pointers:
			if '0' in pointer:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = dict()
		for word in words:
			node = self.trie
			for c in word:
				if c not in node:
					node[c] = dict()
				node = node[c]
			node['.'] = '.'
		self.curs = []

	def query(self, letter: str) -> bool:
		(trie, curs) = (self.trie, self.curs)
		ans = False
		new_curs = []
		for cur in curs:
			if letter in cur:
				new_cur = cur[letter]
				ans = ans or '.' in new_cur
				new_curs.append(new_cur)
		if letter in trie:
			new_cur = trie[letter]
			ans = ans or '.' in new_cur
			new_curs.append(new_cur)
		self.curs = new_curs
		return ans


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			p = self.trie
			for c in word:
				if c not in p:
					p[c] = {}
				p = p[c]
			p['$'] = True
		self.pointers = []

	def query(self, letter: str) -> bool:
		self.pointers.append(self.trie)
		new_pointers = []
		found = False
		for pointer in self.pointers:
			if letter in pointer:
				new_pointers.append(pointer[letter])
				if '$' in pointer[letter]:
					found = True
		self.pointers = new_pointers
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.active = []
		self.root = {}
		for word in words:
			current = self.root
			for c in word:
				if c not in current:
					current[c] = {}
				current = current[c]
			current['word'] = True

	def query(self, letter: str) -> bool:
		self.active = [node[letter] for node in self.active if letter in node]
		if letter in self.root:
			self.active.append(self.root[letter])
		return any(('word' in node for node in self.active))


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.end = False

class ReverseTrie:

	def __init__(self, words):
		self.root = TrieNode()
		for word in words:
			self.add_reverse_word(word)

	def add_reverse_word(self, word):
		curr_node = self.root
		for idx in range(len(word) - 1, -1, -1):
			char = word[idx]
			curr_node = curr_node.children[char]
		curr_node.end = True

	def reverse_word_exists(self, reverse_letters):
		curr_trie_node = self.root
		for char in reverse_letters:
			if char in curr_trie_node.children:
				curr_trie_node = curr_trie_node.children[char]
				if curr_trie_node.end:
					return True
			else:
				return False
		return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.last_n_queries = collections.deque([])
		self.trie_reverse_words = ReverseTrie(words)
		self.max_word_length = 2000

	def query(self, letter: str) -> bool:
		self.last_n_queries.appendleft(letter)
		if len(self.last_n_queries) > self.max_word_length:
			self.last_n_queries.pop()
		return self.trie_reverse_words.reverse_word_exists(self.last_n_queries)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			node = self.root
			for char in word:
				if char not in node:
					node[char] = {}
				node = node[char]
			node['#'] = 1
		self.candidates = []

	def query(self, letter: str) -> bool:
		found = False
		new_candidates = []
		self.candidates.append(self.root)
		for node in self.candidates:
			if letter in node:
				node = node[letter]
				new_candidates.append(node)
				if '#' in node:
					found = True
		self.candidates = new_candidates
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		Trie = lambda : collections.defaultdict(Trie)
		self.trie = Trie()
		for word in words:
			reduce(dict.__getitem__, word, self.trie)['eow'] = True
		self.trie_list = []

	def query(self, letter: str) -> bool:
		self.trie_list = [node[letter] for node in self.trie_list + [self.trie] if letter in node]
		return any(('eow' in trie for trie in self.trie_list))


class StreamChecker:

	def __init__(self, words: List[str]):
		T = lambda : collections.defaultdict(T)
		self.trie = T()
		for w in words:
			reduce(dict.__getitem__, w, self.trie)['#'] = True
		self.waiting = []

	def query(self, letter: str) -> bool:
		self.waiting = [node[letter] for node in self.waiting + [self.trie] if letter in node]
		return any(('#' in node for node in self.waiting))


from collections import defaultdict
from functools import reduce

class StreamChecker:

	def __init__(self, words: List[str]):
		Trie = lambda : defaultdict(Trie)
		self.trie = Trie()
		for word in words:
			reduce(dict.__getitem__, word, self.trie)['#'] = True
		self.waiting = []

	def query(self, letter: str) -> bool:
		self.waiting = [node[letter] for node in self.waiting + [self.trie] if letter in node]
		return any(('#' in node for node in self.waiting))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.nodes = []
		for word in words:
			cur = self.root
			for char in word:
				if char not in cur:
					cur[char] = {}
				cur = cur[char]
			cur['*'] = True

	def query(self, letter: str) -> bool:
		self.nodes.append(self.root)
		newNodes = []
		ans = False
		for node in self.nodes:
			if letter in node:
				node = node[letter]
				if '*' in node:
					ans = True
				newNodes.append(node)
		self.nodes = newNodes
		return ans


from itertools import chain

class StreamChecker:
	prefixes = []

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			d = self.trie
			for letter in word:
				if letter not in d:
					d[letter] = {}
				d = d[letter]
			d['end'] = True

	def query(self, letter: str) -> bool:
		self.prefixes = [node[letter] for node in chain([self.trie], self.prefixes) if letter in node]
		return any(('end' in node for node in self.prefixes))


class StreamChecker:

	def __init__(self, words: List[str]):
		trie = {}
		for w in words:
			node = trie
			for c in w:
				if c not in node:
					node[c] = {}
					node = node[c]
					node['fail'] = trie
				else:
					node = node[c]
			node['end'] = True
		que = [trie[x] for x in trie]
		for node in que:
			for x in node:
				if x not in ('end', 'fail'):
					p = node['fail']
					while p != trie and x not in p:
						p = p['fail']
					if x in p:
						node[x]['fail'] = p[x]
						if 'end' in p[x]:
							node[x]['end'] = True
					que.append(node[x])
		self.trie = trie
		self.node = trie

	def query(self, x: str) -> bool:
		trie = self.trie
		node = self.node
		while node != trie and x not in node:
			node = node['fail']
		if x in node:
			node = node[x]
		self.node = node
		return 'end' in node


class StreamChecker:

	def __init__(self, words: List[str]):
		Trie = lambda : collections.defaultdict(Trie)
		self.trie = Trie()
		for word in words:
			functools.reduce(dict.__getitem__, word, self.trie)['#'] = True
		self.heads = []

	def query(self, letter: str) -> bool:
		self.heads = [h[letter] for h in self.heads if letter in h]
		if letter in self.trie:
			self.heads.append(self.trie[letter])
		return any(('#' in h for h in self.heads))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.d = {}
		for word in words:
			ptr = self.d
			for c in word:
				if c not in ptr:
					ptr[c] = {}
				ptr = ptr[c]
			ptr[''] = True
		self.curr = self.d
		self.q = []

	def query(self, letter: str) -> bool:
		self.q.append(self.d)
		temp = []
		flag = False
		for ptr in self.q:
			if letter in ptr:
				ptr = ptr[letter]
				temp.append(ptr)
				if '' in ptr:
					flag = True
		self.q = temp
		return flag


class TrieNode:

	def __init__(self):
		self.isEnd = False
		self.children = dict()

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def addNode(self, word):
		curNode = self.root
		for char in word:
			if char not in curNode.children:
				curNode.children[char] = TrieNode()
			curNode = curNode.children[char]
		curNode.isEnd = True

	def search(self, word):
		curNode = self.root
		for char in word:
			if curNode.isEnd:
				return True
			elif char not in curNode.children:
				return False
			else:
				curNode = curNode.children[char]
		return curNode.isEnd

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.addNode(word[::-1])
		self.curWord = ''

	def query(self, letter: str) -> bool:
		self.curWord = letter + self.curWord
		return self.trie.search(self.curWord)


class StreamChecker:

	def __init__(self, words: List[str]):
		head = {}
		for w in words:
			current = head
			for c in w:
				if c not in current:
					current[c] = {}
				current = current[c]
			current['X'] = None
		self.head = head
		self.current = [head]

	def query(self, letter: str) -> bool:
		new_current = [self.head]
		for e in self.current:
			if letter in e:
				new_current.append(e[letter])
		self.current = new_current
		for e in new_current:
			if 'X' in e:
				return True
		return False


class Node:

	def __init__(self, char, parent):
		self.children = [None for _ in range(26)]
		self.char = char
		self.parent = parent

class StreamChecker:

	def __init__(self, words: List[str]):
		self.map = {}
		self.root = Node(' ', None)
		self.buffer = deque()
		self.max_size = max([len(word) for word in words])
		self.add(words)

	def add(self, words):
		for word in words:
			curr = self.root
			for c in word:
				if not curr.children[ord(c) - ord('a')]:
					curr.children[ord(c) - ord('a')] = Node(c, curr)
				curr = curr.children[ord(c) - ord('a')]
			if word[-1] not in self.map:
				self.map[word[-1]] = []
			self.map[word[-1]].append(curr)

	def query(self, letter: str) -> bool:
		self.buffer.append(letter)
		if len(self.buffer) > self.max_size:
			self.buffer.popleft()
		if letter not in self.map:
			return False
		for node in self.map[letter]:
			curr = node
			for i in range(len(self.buffer) - 2, -1, -1):
				if curr is not self.root and curr.parent.char == self.buffer[i]:
					curr = curr.parent
				else:
					break
			if curr.parent is self.root:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.chars = []
		self.trie = {}
		for word in words:
			curr = self.trie
			for c in reversed(word):
				if c not in curr:
					curr[c] = {}
				curr = curr[c]
			curr['#'] = True

	def search(self):
		ind = len(self.chars) - 1
		curr = self.trie
		while curr and ind >= 0:
			if '#' in curr:
				return True
			c = self.chars[ind]
			if c not in curr:
				return False
			curr = curr[c]
			ind -= 1
		return curr and '#' in curr

	def query(self, letter: str) -> bool:
		self.chars.append(letter)
		return self.search()


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for w in words:
			d = self.trie
			for c in w:
				if c in d:
					d = d[c]
				else:
					d[c] = {}
					d = d[c]
			d['$'] = None
		self.l = []

	def query(self, letter: str) -> bool:
		self.l = [j[letter] for j in self.l if letter in j]
		if letter in self.trie:
			self.l.append(self.trie[letter])
		return any(['$' in i for i in self.l])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = {}
		for word in words:
			t = self.t
			for ch in word:
				t = t.setdefault(ch, {})
			t['#'] = 'END'
		self.cursors = [self.t]

	def query(self, letter: str) -> bool:
		newcursors = []
		ans = False
		for cur in self.cursors:
			if letter not in cur:
				continue
			cur = cur[letter]
			if '#' in cur:
				ans = True
			newcursors.append(cur)
		self.cursors = newcursors
		self.cursors.append(self.t)
		return ans


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			t = self.trie
			for c in word + '\\0':
				if c not in t:
					t[c] = {}
				t = t[c]
		self.nodes = [self.trie]

	def query(self, letter: str) -> bool:
		newNodes = [self.trie]
		ret = False
		for node in self.nodes:
			if letter in node:
				newNodes.append(node[letter])
				if '\\0' in node[letter]:
					ret = True
		self.nodes = newNodes
		return ret


class StreamChecker:

	def __init__(self, words: List[str]):
		self.waitList = []
		self.trie = {}
		for w in words:
			curr = self.trie
			for c in w:
				if c not in curr:
					curr[c] = {}
				curr = curr[c]
			curr['#'] = {'#'}

	def query(self, letter: str) -> bool:
		newWaitList = []
		if letter in self.trie:
			newWaitList.append(self.trie[letter])
		for ele in self.waitList:
			if letter in ele:
				newWaitList.append(ele[letter])
		self.waitList = newWaitList
		for ele in self.waitList:
			if '#' in ele:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			t = self.trie
			for c in word:
				if c not in t:
					t[c] = {}
				t = t[c]
			t['$'] = {}
		self.ptrs = deque()
		self.ptrs.append(self.trie)
		return

	def query(self, letter: str) -> bool:
		ret = False
		nextPtrs = deque()
		nextPtrs.append(self.trie)
		for ptr in self.ptrs:
			if letter in ptr:
				nextPtrs.append(ptr[letter])
				if '$' in ptr[letter]:
					ret = True
		self.ptrs = nextPtrs
		return ret


class StreamChecker:

	def __init__(self, words: List[str]):
		root = {}
		nodes = [root]
		for w in words:
			prev = root
			for s in w:
				if s in prev:
					node = prev[s]
				else:
					node = {}
					prev[s] = node
				prev = node
			prev['None'] = None
		self.array_nodes = []
		self.root = root

	def query(self, letter: str) -> bool:
		if letter in self.root:
			self.array_nodes.append(self.root)
		nodes = []
		result = False
		for node in self.array_nodes:
			if letter in node:
				nodes.append(node[letter])
				if 'None' in node[letter]:
					result = True
		self.array_nodes = nodes
		return result


class StreamChecker:

	def __init__(self, words: List[str]):
		self.s = ''
		self.c = 0
		self.ld = dict()
		self.lths = set()
		for x in list(set(words)):
			lim = len(x)
			self.lths.add(lim)
			try:
				self.ld[lim][x[-1]].add(x)
			except:
				try:
					self.ld[lim][x[-1]] = set()
					self.ld[lim][x[-1]].add(x)
				except:
					self.ld[lim] = dict()
					self.ld[lim][x[-1]] = set()
					self.ld[lim][x[-1]].add(x)

	def query(self, letter: str) -> bool:
		self.s += letter
		self.c += 1
		for y in self.lths:
			if y <= self.c:
				if self.s[-1] in self.ld[y]:
					if self.s[-y:] in self.ld[y][self.s[-1]]:
						return True
		return False


_end = '_end_'

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.nodes_for_letters = []
		for word in words:
			self.add_to_trie(word)

	def add_to_trie(self, word):
		current = self.trie
		for letter in word:
			if letter not in current:
				current[letter] = {}
			current = current[letter]
		current[_end] = _end

	def query(self, letter: str) -> bool:
		potential_nodes = self.nodes_for_letters + [self.trie]
		self.nodes_for_letters = [node[letter] for node in potential_nodes if letter in node]
		return any((_end in node for node in self.nodes_for_letters))


class TrieNode:

	def __init__(self, ch):
		self.val = ch
		self.children = {}
		self.is_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode('')

	def insert(self, word):
		curr = self.root
		for ch in word:
			if ch in curr.children:
				curr = curr.children[ch]
			else:
				curr.children[ch] = TrieNode(ch)
				curr = curr.children[ch]
		curr.is_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
		self.string = ''

	def query(self, letter: str) -> bool:
		self.string += letter
		curr = self.trie.root
		for ch in self.string[::-1]:
			if ch in curr.children:
				curr = curr.children[ch]
				if curr.is_word:
					return True
			else:
				return False
		return curr.is_word


class TrieNode:
	import collections

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.is_word = False

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def construct(self, word):
		node = self.root
		for c in word[::-1]:
			node = node.children[c]
		node.is_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = Trie()
		self.letters = []
		for word in words:
			self.tree.construct(word)

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		if letter in self.tree.root.children:
			node = self.tree.root
			for l in self.letters[::-1]:
				node = node.children.get(l)
				if not node:
					return False
				if node.is_word:
					return True
			return node.is_word
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			cur = self.trie
			for char in word:
				if char not in cur:
					cur[char] = {}
				cur = cur[char]
			cur['~'] = word
		self.candidates = []

	def query(self, letter: str) -> bool:
		newCands = []
		foundWord = False
		if letter in self.trie:
			newCands.append(self.trie[letter])
			if '~' in self.trie[letter]:
				foundWord = True
		for cand in self.candidates:
			if letter in cand:
				newCands.append(cand[letter])
				if '~' in cand[letter]:
					foundWord = True
		self.candidates = newCands
		return foundWord


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			cur = self.trie
			for ch in word:
				if ch not in cur:
					cur[ch] = {}
				cur = cur[ch]
			cur['#'] = '#'
		self.queue = []

	def query(self, letter: str) -> bool:
		res = False
		nxt = []
		for top in self.queue:
			if letter in top:
				if '#' in top[letter]:
					res = True
				nxt.append(top[letter])
		if letter in self.trie:
			if '#' in self.trie[letter]:
				res = True
			nxt.append(self.trie[letter])
		self.queue = nxt
		return res


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = self._trie()
		self._fillTrie(words)
		self.q = []

	def _trie(self):
		return defaultdict(self._trie)

	def _fillTrie(self, words):
		for w in words:
			t = self.trie
			for c in w:
				t = t[c]
			t['*']

	def query(self, letter: str) -> bool:
		(found, newQ) = (False, [])
		self.q.append(self.trie)
		for t in self.q:
			if letter in t:
				t = t[letter]
				newQ.append(t)
				found |= '*' in t
		self.q = newQ
		return found


END = '_'

class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = {}
		self.queries = []
		for w in words:
			sub = self.tree
			for c in w:
				if c not in sub:
					sub[c] = {}
				sub = sub[c]
			sub[END] = END

	def query(self, l: str) -> bool:
		found = False
		self.queries.append(self.tree)
		qs = []
		for q in self.queries:
			if l in q:
				qs.append(q[l])
				if END in q[l]:
					found = True
		self.queries = qs
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = collections.defaultdict(dict)
		self.construct_trie(words)
		self.l = []

	def construct_trie(self, words):
		for word in words:
			t = self.trie
			for w in word:
				if w not in t:
					t[w] = {}
				t = t[w]
			t['$'] = True
		print(self.trie)

	def query(self, letter: str) -> bool:
		t = self.trie
		ans = False
		temp = []
		for d in self.l:
			if letter in d:
				if '$' in d[letter]:
					ans = True
				temp.append(d[letter])
		self.l = temp
		if letter in t:
			self.l.append(t[letter])
			if '$' in t[letter]:
				return True
		return ans


class StreamChecker:

	def __init__(self, words: List[str]):
		from collections import defaultdict
		self.trie = dict()
		for word in words:
			trie = self.trie
			for c in word:
				if c not in trie:
					trie[c] = dict()
					trie[c]['word'] = False
				trie = trie[c]
			trie['word'] = True
		self.tries = list()

	def query(self, letter: str) -> bool:
		newtries = list()
		self.tries.append(self.trie)
		found = False
		for trie in self.tries:
			if letter in trie:
				newtries.append(trie[letter])
				if trie[letter]['word']:
					found = True
		self.tries = newtries
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = dict()
		for word in words:
			node = self.trie
			for c in word:
				if c not in node:
					node[c] = dict()
				node = node[c]
			node['.'] = '.'
		self.curs = []

	def query(self, letter: str) -> bool:
		(trie, curs) = (self.trie, self.curs)
		curs = [cur[letter] for cur in curs if letter in cur]
		if letter in trie:
			curs.append(trie[letter])
		self.curs = curs
		return any(('.' in cur for cur in curs))


from collections import defaultdict

class StreamChecker:

	def __init__(self, words):
		tr = lambda : defaultdict(tr)
		self.trie = tr()
		for w in words:
			cur_trie = self.trie[w[0]]
			for i in range(1, len(w)):
				cur_trie = cur_trie[w[i]]
			cur_trie['end'] = True
		self.tail = []

	def query(self, letter):
		self.tail = [node[letter] for node in self.tail + [self.trie] if letter in node]
		for node in self.tail:
			if 'end' in node:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie_head = {}
		self.trie_leaf = '$'
		for word in words:
			trie_node = self.trie_head
			for c in word:
				trie_node = trie_node.setdefault(c, {})
			trie_node[self.trie_leaf] = self.trie_leaf
		self.trie_pointers = []

	def query(self, letter: str) -> bool:
		self.trie_pointers = [trie_node[letter] for trie_node in self.trie_pointers if letter in trie_node]
		if letter in self.trie_head:
			self.trie_pointers.append(self.trie_head[letter])
		return any([self.trie_leaf in trie_node for trie_node in self.trie_pointers])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.check = []
		for w in words:
			d = self.trie
			for c in w:
				_d = d.get(c, {})
				d[c] = _d
				d = _d
			d['.'] = True
		print(self.trie)

	def query(self, letter: str) -> bool:
		next_check = []
		found = False
		for d in self.check + [self.trie]:
			if letter in d:
				_d = d[letter]
				if '.' in _d:
					found = True
				next_check.append(_d)
		self.check = next_check
		return found


class StreamChecker:
	END = 0

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			ptr = self.trie
			for letter in word:
				if letter not in ptr:
					ptr[letter] = {}
				ptr = ptr[letter]
			ptr[StreamChecker.END] = True
		self.working_list = []

	def query(self, letter: str) -> bool:
		is_word = False
		new_working_list = []
		self.working_list.append(self.trie)
		for working_ptr in self.working_list:
			if letter in working_ptr:
				new_working_ptr = working_ptr[letter]
				new_working_list.append(new_working_ptr)
				if StreamChecker.END in new_working_ptr:
					is_word = True
		self.working_list = new_working_list
		return is_word


class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = dict()
		for word in words:
			t = self.Trie
			for c in word:
				if c not in t:
					t[c] = dict()
				t = t[c]
			t['$'] = word
		self.cache = []

	def query(self, letter: str) -> bool:
		newcache = []
		ret = False
		for t in self.cache + [self.Trie]:
			if letter in t:
				newcache.append(t[letter])
				if '$' in t[letter]:
					ret = True
		self.cache = newcache
		return ret


from typing import List

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = dict()
		for word in words:
			root = self.root
			for c in word:
				if c not in root:
					root[c] = dict()
				root = root[c]
			root['$'] = '.'
		self.searches = []

	def query(self, letter: str) -> bool:
		searches = self.searches
		searches.append(self.root)
		found = False
		updated_searches = []
		for search in searches:
			if letter not in search:
				continue
			search = search[letter]
			updated_searches.append(search)
			if '$' in search:
				found = True
		self.searches = updated_searches
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.d = {}
		for w in words:
			cur = self.d
			for c in w:
				cur.setdefault(c, dict())
				cur = cur[c]
			cur[''] = True
		self.q = []

	def query(self, letter: str) -> bool:
		nq = []
		if letter in self.d:
			nq += [self.d[letter]]
		for pq in self.q:
			if letter in pq:
				nq += [pq[letter]]
		self.q = nq
		for qq in self.q:
			if '' in qq:
				return True
		return False


import collections

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.stream = collections.deque()
		self.prefixes = []
		for w in words:
			ptr = self.trie
			for c in w:
				if c not in ptr:
					ptr[c] = {}
				ptr = ptr[c]
			ptr['#'] = w

	def _find_word(self, k: int) -> bool:
		ptr = self.trie
		for c in list(self.stream)[-k:]:
			if c not in ptr:
				return False
			ptr = ptr[c]
		return '#' in ptr

	def query(self, letter: str) -> bool:
		if letter in self.trie:
			self.prefixes.append(self.trie)
		(valid_prefixes, found) = ([], False)
		for (i, ptr) in enumerate(self.prefixes):
			if letter in ptr:
				ptr = ptr[letter]
				valid_prefixes.append(ptr)
				if '#' in ptr:
					found = True
		self.prefixes = valid_prefixes
		return found


class TrieNode:

	def __init__(self):
		self.children = {}
		self.char = None
		self.isEnd = False

	def contains(self, c):
		return c in self.children

	def set(self, c):
		self.children[c] = TrieNode()

	def getChild(self, c):
		return self.children[c]

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		if not word:
			return
		word = word[::-1]
		it = self.root
		for c in word:
			if not c in it.children:
				it.children[c] = TrieNode()
				it.children[c].char = c
			it = it.children[c]
		it.isEnd = True

	def hasAnyPrefix(self, letters):
		it = self.root
		for c in letters:
			if it.isEnd:
				return True
			if not it.contains(c):
				return False
			it = it.getChild(c)
		return it.isEnd

class StreamChecker:

	def __init__(self, words: List[str]):
		from collections import deque
		self.letters = deque([])
		self.trie = Trie()
		for word in words:
			self.trie.insert(word)

	def query(self, letter: str) -> bool:
		self.letters.appendleft(letter)
		return self.trie.hasAnyPrefix(self.letters)


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.pointers = [self.trie]
		for word in words:
			t = self.trie
			for w in word:
				if w not in t:
					t[w] = {}
				t = t[w]
			t['#'] = '#'
		print(self.trie)

	def query(self, letter: str) -> bool:
		s = [self.trie]
		for ele in self.pointers:
			if letter in ele:
				s.append(ele[letter])
		self.pointers = s
		return any(['#' in ele for ele in self.pointers])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.s = set()
		for w in words:
			self.s = self.s.union(set(w))
		self.d = {}
		for w in words:
			insert(self.d, w)
		self.l = []

	def query(self, letter: str) -> bool:
		if letter not in self.s:
			return False
		d = self.d
		l = self.l
		b = False
		for i in reversed(list(range(len(l)))):
			if letter in l[i]:
				l[i] = l[i][letter]
				if '#' in l[i]:
					b = True
			else:
				del l[i]
		if letter in d:
			l.append(d[letter])
			if '#' in l[-1]:
				b = True
		return b

def insert(d, s):
	if len(s) == 0:
		d['#'] = {}
	else:
		if s[0] not in d:
			d[s[0]] = {}
		insert(d[s[0]], s[1:])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for w in words:
			t = self.trie
			for c in w:
				if c not in t:
					t[c] = {}
				t = t[c]
			t['#'] = '#'
		self.poss = []

	def query(self, letter: str) -> bool:
		waitlist = []
		if letter in self.trie:
			waitlist.append(self.trie[letter])
		for t in self.poss:
			if letter in t:
				waitlist.append(t[letter])
		self.poss = waitlist
		return any(('#' in t for t in waitlist))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.wordlist = []
		for w in words:
			self.wordlist.append(w[::-1])
		self.wordlist.sort()
		self.history = ''

	def query2(self, letter: str) -> bool:
		self.history = letter + self.history
		k = 0
		for i in range(len(self.history)):
			w = self.history[:i + 1]
			while w > self.wordlist[k]:
				k += 1
				if k == len(self.wordlist):
					return False
			if w == self.wordlist[k]:
				return True
		return False

	def query(self, letter: str) -> bool:
		self.history = letter + self.history
		s = 0
		f = len(self.wordlist)
		from bisect import bisect_left
		for i in range(len(self.history)):
			indx = bisect_left(self.wordlist[s:f], self.history[:i + 1]) - 1
			if indx == -1:
				if self.wordlist[s:f][0] == self.history[:i + 1]:
					return True
			if indx + 1 < len(self.wordlist[s:f]) and self.wordlist[s:f][indx + 1] == self.history[:i + 1]:
				return True
			tmp = s + indx + 1
			f = s + bisect_left(self.wordlist[s:f], self.history[:i] + chr(ord(self.history[i]) + 1))
			s = tmp
			if s >= f:
				return False
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.start = []
		for word in words:
			self.add(word)

	def add(self, word):
		curr = self.trie
		for c in word:
			if c not in curr:
				curr[c] = {}
			curr = curr[c]
		curr['#'] = True

	def query(self, letter: str) -> bool:
		start = []
		if letter in self.trie:
			start.append(self.trie[letter])
		for p in self.start:
			if letter in p:
				start.append(p[letter])
		self.start = start
		return any(('#' in p for p in self.start))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.tries = {}
		for word in words:
			tries = self.tries
			for c in word:
				if c not in tries:
					tries[c] = {}
				tries = tries[c]
			tries['END'] = 1
		self.pool = []

	def query(self, letter: str) -> bool:
		new_pool = []
		self.pool.append(self.tries)
		for tries in self.pool:
			if letter in tries:
				new_pool.append(tries[letter])
		self.pool = new_pool
		return any(('END' in tries for tries in self.pool))


from typing import List

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = dict()
		for word in words:
			root = self.root
			for c in word:
				if c not in root:
					root[c] = dict()
				root = root[c]
			root['$'] = '.'
		self.searches = []

	def query(self, letter: str) -> bool:
		self.searches.append(self.root)
		found = False
		failed = list()
		i = 0
		for search in self.searches:
			if letter not in search:
				failed.append(i)
			else:
				search = search[letter]
				self.searches[i] = search
				if '$' in search:
					found = True
			i += 1
		o = 0
		for i in failed:
			del self.searches[i - o]
			o += 1
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = dict()
		self.pointers = []
		for word in words:
			if len(word) == 0:
				continue
			for (i, ch) in enumerate(word):
				if i == 0:
					self.trie[ch] = self.trie.get(ch, dict())
					cur = self.trie[ch]
				else:
					cur[ch] = cur.get(ch, dict())
					cur = cur[ch]
			cur['end'] = 1

	def query(self, letter: str) -> bool:
		result = False
		pointers_new = []
		if letter in self.trie:
			pointers_new.append(self.trie[letter])
			if 'end' in self.trie[letter]:
				result = True
		for pointer in self.pointers:
			if letter not in pointer:
				continue
			pointers_new.append(pointer[letter])
			if 'end' in pointer[letter]:
				result = True
		self.pointers = pointers_new
		return result


class Node:

	def __init__(self):
		self.children = {}
		self.terminal = False

	def add(self, val):
		self.children[val] = Node()

	def has(self, val):
		return val in list(self.children.keys())

	def get(self, val):
		return self.children[val]

	def set_terminal(self):
		self.terminal = True

	def is_terminal(self):
		return len(self.children) == 0 or self.terminal

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Node()
		for word in words:
			curr = self.root
			for sym in word[::-1]:
				if not curr.has(sym):
					curr.add(sym)
				curr = curr.get(sym)
			curr.set_terminal()
		self.prefix = ''

	def query(self, letter: str) -> bool:
		self.prefix += letter
		curr = self.root
		for sym in self.prefix[::-1]:
			if not curr.has(sym):
				return False
			curr = curr.get(sym)
			if curr.is_terminal():
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = set(words)
		self.hist = ''

	def query(self, letter: str) -> bool:
		self.hist += letter
		for word in self.words:
			if self.hist[-len(word):] == word:
				return True
		return False


class Node:

	def __init__(self):
		self.next = {}
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Node()
		self.ptr = self.root
		self.letters = []
		for w in words:
			node = self.root
			for c in w[::-1]:
				if c not in node.__next__:
					node.next[c] = Node()
				node = node.next[c]
			node.isWord = True

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		(idx, ptr) = (0, self.root)
		N = len(self.letters)
		for i in range(N - 1, -1, -1):
			c = self.letters[i]
			idx += 1
			if c not in ptr.__next__:
				return False
			ptr = ptr.next[c]
			if idx > 0 and ptr.isWord:
				return True
		return False


class StreamChecker:

	def __init__(self, words):
		self.waitlist = []
		self.trie = dict()
		for word in words:
			tmp_dict = self.trie
			for c in word:
				tmp_dict = tmp_dict.setdefault(c, dict())
			tmp_dict['#'] = '#'

	def query(self, letter: str) -> bool:
		waitlist = []
		if letter in self.trie:
			waitlist.append(self.trie[letter])
		for item in self.waitlist:
			if letter in item:
				waitlist.append(item[letter])
		self.waitlist = waitlist
		return any(('#' in item for item in self.waitlist))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.waitlist = []
		self.trie = dict()
		for word in words:
			temp_dict = self.trie
			for letter in word:
				temp_dict = temp_dict.setdefault(letter, dict())
			temp_dict['#'] = '#'

	def query(self, letter: str) -> bool:
		waitlist = []
		if letter in self.trie:
			waitlist.append(self.trie[letter])
		for item in self.waitlist:
			if letter in item:
				waitlist.append(item[letter])
		self.waitlist = waitlist
		return any(('#' in item for item in self.waitlist))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			node = self.trie
			for c in word:
				if c not in node:
					node[c] = {}
				node = node[c]
			node['#'] = {}
		self.table = []

	def query(self, letter: str) -> bool:
		tmp = []
		for node in self.table + [self.trie]:
			if letter in node:
				tmp.append(node[letter])
		self.table = tmp
		return any(('#' in node for node in self.table))


def ctoi(c):
	return ord(c) - 97

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = [None] * 27
		for w in words:
			curr_trie = self.trie
			for c in w:
				c_idx = ctoi(c)
				if not curr_trie[c_idx]:
					curr_trie[c_idx] = [None] * 27
				curr_trie = curr_trie[c_idx]
			curr_trie[-1] = True
		self.ptrs = []

	def query(self, letter: str) -> bool:
		found = False
		self.ptrs.append(self.trie)
		new_ptrs = []
		c_idx = ctoi(letter)
		for ptr in self.ptrs:
			if ptr[c_idx]:
				new_ptrs.append(ptr[c_idx])
				if ptr[c_idx][-1]:
					found = True
		self.ptrs = new_ptrs
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			ptr = self.trie
			for ch in word:
				if ch not in ptr:
					ptr[ch] = {}
				ptr = ptr[ch]
			ptr[None] = {}
		self.ptrs = []

	def query(self, letter: str) -> bool:
		ptrs = []
		ret = False
		for ptr in self.ptrs:
			if letter in ptr:
				ptrs.append(ptr[letter])
				ret |= None in ptr[letter]
		if letter in self.trie:
			ptrs.append(self.trie[letter])
			ret |= None in self.trie[letter]
		self.ptrs = ptrs
		return ret


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = dict()
		for w in words:
			curr = self.trie
			for c in w:
				curr = curr.setdefault(c, {})
			curr['$'] = '$'
		self.ptrs = []

	def query(self, letter: str) -> bool:
		(ret, new_list) = (False, [])
		self.ptrs.append(self.trie)
		for ptr in self.ptrs:
			if letter in ptr:
				ptr = ptr[letter]
				if '$' in ptr:
					ret = True
				new_list.append(ptr)
		self.ptrs = new_list
		return ret


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			root = self.trie
			for w in word:
				if w not in root:
					root[w] = {}
				root = root[w]
			root['#'] = {}
		self.q = []

	def query(self, letter: str) -> bool:
		i = 0
		self.q = [p[letter] for p in self.q if letter in p]
		if letter in self.trie:
			self.q.append(self.trie[letter])
		for p in self.q:
			if '#' in p:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.wait_list = []
		self.trie = dict()
		for w in words:
			temp_dict = self.trie
			for c in w:
				temp_dict = temp_dict.setdefault(c, dict())
			temp_dict['#'] = '#'

	def query(self, letter: str) -> bool:
		new_wait = []
		if letter in self.trie:
			new_wait.append(self.trie[letter])
		for item in self.wait_list:
			if letter in item:
				new_wait.append(item[letter])
		self.wait_list = new_wait
		return any(('#' in wait for wait in self.wait_list))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			curr = self.root
			for c in word:
				curr = curr.setdefault(c, {})
			curr['$'] = '$'
		self.stack = [self.root]

	def query(self, letter: str) -> bool:
		stack = []
		flag = False
		for node in self.stack:
			child = node.get(letter)
			if child:
				stack.append(child)
				flag |= '$' in child
		self.stack = stack
		self.stack.append(self.root)
		return flag


class Node:

	def __init__(self):
		self.children = {}
		self.isWord = False

class Trie:

	def __init__(self):
		self.root = Node()

	def add(self, word):
		cur = self.root
		for ch in word:
			if ch not in cur.children:
				cur.children[ch] = Node()
			cur = cur.children[ch]
		cur.isWord = True

	def check(self, word):
		cur = self.root
		for ch in word:
			if cur.isWord:
				return True
			if ch not in cur.children:
				return False
			cur = cur.children[ch]
		return cur.isWord

class StreamChecker:

	def __init__(self, words: List[str]):
		self.T = Trie()
		for word in words:
			self.T.add(word[::-1])
		self.arr = []

	def query(self, letter: str) -> bool:
		self.arr.append(letter)
		return self.T.check(self.arr[::-1])


class StreamChecker:

	def __init__(self, words: List[str]):
		dic = {}
		for w in words:
			p = dic
			for c in w:
				if c not in p:
					p[c] = {}
				p = p[c]
			p['#'] = True
		self.dic = dic
		self.cur = []

	def query(self, letter: str) -> bool:
		(tmp, self.cur) = (self.cur, [])
		tmp.append(self.dic)
		res = False
		for d in tmp:
			if letter in d:
				self.cur.append(d[letter])
				if '#' in d[letter]:
					res = True
		return res


class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = {'chld': {}, 'end': False}
		for word in words:
			T = self.Trie
			for l in reversed(word):
				if l not in T['chld']:
					T['chld'][l] = {'chld': {}, 'end': False}
				T = T['chld'][l]
			T['end'] = True
		self.stack = ''

	def query(self, letter: str) -> bool:
		self.stack += letter
		T = self.Trie
		for i in range(len(self.stack) - 1, -1, -1):
			l = self.stack[i]
			if l not in T['chld']:
				return False
			T = T['chld'][l]
			if T['end']:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = words
		self.history = ''
		self.trie = {}
		for w in words:
			p = self.trie
			for c in w:
				if c not in p:
					p[c] = {}
				p = p[c]
			p['#'] = w
		self.p = [self.trie]

	def query(self, letter: str) -> bool:
		self.history += letter
		self.p = [p[letter] for p in self.p if letter in p]
		res = any(['#' in p for p in self.p])
		self.p += [self.trie]
		return res


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.waitlist = []
		for word in words:
			head = self.trie
			for char in word:
				if char not in head:
					head[char] = {}
				head = head[char]
			head['#'] = '#'

	def query(self, letter: str) -> bool:
		waitlist = []
		if letter in self.trie:
			waitlist.append(self.trie[letter])
		for item in self.waitlist:
			if letter in item:
				waitlist.append(item[letter])
		self.waitlist = waitlist
		return any(('#' in item for item in self.waitlist))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.dictionary = {}
		for word in words:
			current = self.dictionary
			for letter in word:
				if letter not in current:
					current[letter] = {}
				current = current[letter]
			current['!'] = {}
		self.memory = []

	def query(self, letter: str) -> bool:
		memory = self.memory + [self.dictionary]
		self.memory = []
		validity = False
		for entry in memory:
			if letter in entry:
				self.memory.append(entry[letter])
				if '!' in entry[letter]:
					validity = True
		return validity


class TrieNode:

	def __init__(self):
		self.children = collections.defaultdict(TrieNode)
		self.isWord = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.head = TrieNode()
		for word in words:
			p = self.head
			for c in word:
				p = p.children[c]
			p.isWord = True
		self.pos = [self.head]

	def query(self, letter: str) -> bool:
		tmp = [self.head]
		is_word = False
		for p in self.pos:
			if letter in p.children:
				nxt = p.children[letter]
				tmp.append(nxt)
				if nxt.isWord:
					is_word = True
		self.pos = tmp
		return is_word


class StreamChecker:

	def __init__(self, words: List[str]):
		_compress = {}
		_decompress = {}

		def compress(s):
			if s not in _compress:
				i = len(_compress)
				_compress[s] = i
				_decompress[i] = s
			return _compress[s]

		def decompress(i):
			return _decompress[i]
		compress('')
		words = set(words)
		graph = defaultdict(lambda : defaultdict(int))
		prefs = set()
		for word in words:
			for i in range(len(word) + 1):
				pref = word[:i]
				prefs.add(pref)
		for pref in prefs:
			for i in range(26):
				ch = chr(ord('a') + i)
				for j in range(len(pref) + 1):
					pref2 = pref[j:] + ch
					if pref2 in prefs and len(pref2) > len(decompress(graph[compress(pref)][ch])):
						graph[compress(pref)][ch] = compress(pref2)
		prefWithWordSuffix = set()
		for pref in prefs:
			for i in range(len(word) + 1):
				if pref[i:] in words:
					prefWithWordSuffix.add(compress(pref))
					break
		self.graph = graph
		self.state = compress('')
		self.prefWithWordSuffix = prefWithWordSuffix

	def query(self, letter: str) -> bool:
		self.state = self.graph[self.state][letter]
		return self.state in self.prefWithWordSuffix

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			node = self.trie
			for c in word:
				if c not in node:
					node[c] = {}
				node = node[c]
			node['$'] = word
		self.states = [self.trie]

	def query(self, letter: str) -> bool:
		nextStates = [self.trie]
		for state in self.states:
			if letter in state:
				nextStates.append(state[letter])
		self.states = nextStates
		return any(('$' in state for state in self.states))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = trie = {}
		for word in words:
			node = trie
			for letter in word:
				node = node.setdefault(letter, {})
			node['end'] = True
		self.s = []

	def query(self, letter: str) -> bool:
		f = 0
		temp = []
		self.s.append(self.trie)
		for d in self.s:
			if letter in d:
				t = d[letter]
				f |= 'end' in t
				temp.append(t)
		self.s = temp
		return f


MAX_WORD_LEN = 2000

class StreamChecker:

	def __init__(self, words: List[str]):
		self.ring_buffer = [None] * MAX_WORD_LEN
		self.index = 0
		self.words = set(words)
		self.possible_chars = set([char for word in self.words for char in word])

	def query(self, letter: str) -> bool:
		self.add_letter(letter)
		if letter not in self.possible_chars:
			return False
		for word in self.words:
			if word[-1] != letter:
				continue
			if self.endswith(word):
				return True
		return False

	def add_letter(self, letter):
		self.ring_buffer[self.index] = letter
		self.index = (self.index + 1) % MAX_WORD_LEN

	def endswith(self, word):
		index = (self.index - len(word)) % MAX_WORD_LEN
		for i in range(len(word)):
			if self.ring_buffer[index] != word[i]:
				return False
			index = (index + 1) % MAX_WORD_LEN
		return True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.d = {}
		for w in words:
			c = self.d
			i = 0
			while i < len(w):
				if w[i] not in c:
					c[w[i]] = {}
				c = c[w[i]]
				i += 1
			c[''] = True
		self.ps = []

	def query(self, letter: str) -> bool:
		nps = []
		for p in self.ps + [self.d]:
			if letter in p:
				nps.append(p[letter])
		self.ps = nps
		return any(('' in p for p in self.ps))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = {}
		for word in words:
			curr = self.t
			for c in word:
				if c not in curr:
					curr[c] = {}
				curr = curr[c]
			curr[''] = None
		self.cursor = []

	def query(self, letter: str) -> bool:
		self.cursor = [x[letter] for x in self.cursor if letter in x]
		if letter in self.t:
			self.cursor.append(self.t[letter])
		for c in self.cursor:
			if '' in c:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			t = self.root
			for c in word:
				if c not in t:
					t[c] = {}
				t = t[c]
			t['end'] = True
		self.temp = [self.root]

	def query(self, letter: str) -> bool:
		nextTemp = []
		flag = False
		for node in self.temp:
			if letter not in node:
				continue
			node = node[letter]
			nextTemp.append(node)
			if 'end' in node:
				flag = True
		nextTemp += [self.root]
		self.temp = nextTemp
		return flag


class Trie:

	def __init__(self):
		self.root = {}
		self.curr = self.root

	def add(self, word):
		node = self.root
		for char in word:
			if char not in node:
				node[char] = {}
			node = node[char]
		node['.'] = True

	def search(self, char):
		node = self.curr
		if char in node:
			node = node[char]
			self.curr = node
			return '.' in node
		else:
			self.curr = self.root
			return False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.stream = deque()
		self.trie = Trie()
		for w in words:
			self.trie.add(w[::-1])

	def query(self, letter: str) -> bool:
		self.stream.appendleft(letter)
		node = self.trie.root
		for char in self.stream:
			if char in node:
				node = node[char]
				if '.' in node:
					return True
			else:
				break
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for w in words:
			node = self.trie
			for char in w:
				node.setdefault(char, {})
				node = node[char]
			node.setdefault('*', {})
		self.curr = [self.trie]

	def query(self, letter: str) -> bool:
		new_curr = [self.trie]
		ret = False
		for node in self.curr:
			if letter in node:
				node = node[letter]
				new_curr.append(node)
				if '*' in node:
					ret = True
		self.curr = new_curr
		return ret


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for w in words:
			lastC = self.trie
			for c in w:
				lastC[c] = lastC.get(c, {})
				lastC = lastC[c]
			lastC['*'] = True
		self.words = []

	def query(self, letter: str) -> bool:
		newWords = []
		if letter in self.trie:
			newWords.append(self.trie[letter])
		for t in self.words:
			if letter in t:
				newWords.append(t[letter])
		self.words = newWords
		return any(['*' in t for t in self.words])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			curr = self.trie
			for char in word:
				if char not in curr:
					curr[char] = {}
				curr = curr[char]
			curr['#'] = 1
		self.tmp = [self.trie]

	def query(self, letter: str) -> bool:
		new_tmp = [self.trie]
		flag = False
		for curr in self.tmp:
			if letter in curr:
				curr = curr[letter]
				new_tmp.append(curr)
				if '#' in curr:
					flag = True
					if letter in self.trie:
						new_tmp.append(self.trie[letter])
		self.tmp = new_tmp
		return flag


class StreamChecker:

	def __init__(self, words: List[str]):
		self.queries = []
		trie = {}
		for word in words:
			word = list(reversed(word))
			here = trie
			for i in range(len(word)):
				if word[i] not in list(here.keys()):
					here[word[i]] = {}
				here = here[word[i]]
			here['is_word'] = True
		self.trie = trie

	def query(self, letter: str) -> bool:
		self.queries.append(letter)
		here = self.trie
		for i in range(1, len(self.queries) + 1):
			if self.queries[-i] not in list(here.keys()):
				return False
			here = here[self.queries[-i]]
			if 'is_word' in list(here.keys()):
				return True


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for w in words:
			start = self.trie
			for (i, l) in enumerate(w):
				if l not in start:
					start[l] = {}
				start = start[l]
			start['#'] = True
		self.past = [self.trie]

	def query(self, letter: str) -> bool:
		ans = False
		new_past = [self.trie]
		for ele in self.past:
			if letter in ele:
				if '#' in ele[letter]:
					ans = True
				new_past.append(ele[letter])
		self.past = new_past
		return ans


class StreamChecker:

	def build_trie(self, word: str) -> None:
		place = self.trie
		for ch in word:
			if ch in place:
				place = place[ch]
			else:
				place[ch] = dict()
				place = place[ch]
		place['*'] = None

	def __init__(self, words: List[str]) -> None:
		self.words = set(words)
		self.trie = dict()
		for word in words:
			self.build_trie(word)
		print(self.trie)
		self.temp = []
		self.temp2 = []

	def query(self, letter: str) -> bool:
		self.temp2 = []
		self.temp.append(self.trie)
		can = False
		for i in self.temp:
			if letter in i:
				self.temp2.append(i[letter])
				if '*' in i[letter]:
					can = True
			else:
				continue
		self.temp = self.temp2
		return can


_end = '_end'

def make_trie(words):
	root = dict()
	for word in words:
		current_dict = root
		for letter in word:
			current_dict = current_dict.setdefault(letter, {})
		current_dict[_end] = _end
	return root

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = make_trie(words)
		self.cur_tries = []

	def query(self, letter: str) -> bool:
		new_tries = []
		result = False
		for tr in self.cur_tries + [self.trie]:
			if letter in tr:
				if _end in tr[letter]:
					result = True
				if _end not in tr[letter] or len(tr[letter]) > 1:
					new_tries.append(tr[letter])
		self.cur_tries = new_tries
		return result


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			cur = self.trie
			for c in word:
				if c not in cur:
					cur[c] = {}
				cur = cur[c]
			cur['end'] = None
		self.buffer = []

	def query(self, letter: str) -> bool:
		found = False
		swap = []
		if letter in self.trie:
			swap.append(self.trie[letter])
			found = 'end' in self.trie[letter]
		for b in self.buffer:
			if letter in b:
				swap.append(b[letter])
				if 'end' in b[letter]:
					found = True
		self.buffer = swap
		return found


class Node:

	def __init__(self, v=None):
		self.children: Dict[str, Node] = {}
		self.parent: Node
		self.value = v

class Trie:

	def __init__(self):
		self.t = Node()
		self.leaves: Dict[str, List[Node]] = {}
		self.maxLen: int = 0

	def addLeave(self, w: str, n: Node) -> None:
		if w not in list(self.leaves.keys()):
			self.leaves[w] = []
		self.leaves[w].append(n)

	def insert(self, w: str) -> None:
		self.maxLen = max(self.maxLen, len(w))
		tl = self.t
		j = 0
		while j < len(w):
			i = w[j]
			n = tl.children.get(i, None)
			if n != None:
				tl = n
				j += 1
			else:
				for i in w[j:]:
					newNode = Node(i)
					newNode.parent = tl
					tl.children[i] = newNode
					tl = newNode
				break
		self.addLeave(tl.value, tl)

class StreamChecker:
	from collections import deque

	def __init__(self, words: List[str]):
		self.trie = Trie()
		[self.trie.insert(w) for w in words]
		self.q = deque(maxlen=self.trie.maxLen)

	def check_leaves(self) -> None:
		for s in self.trie.leaves:
			for i in self.trie.leaves[s]:
				j = i
				l = 0
				while j.parent.value != None:
					j = j.parent
					l += 1
				print((s, l))

	def query(self, letter: str) -> bool:
		self.q.append(letter)
		fl = self.trie.leaves.get(letter, None)
		if fl == None:
			return False
		for f in fl:
			i = len(self.q) - 2
			if f.parent.value == None:
				return True
			while i >= 0:
				l = self.q[i]
				if f.parent.value == l:
					f = f.parent
					i -= 1
				else:
					break
				if f.parent.value == None:
					return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.pointers = []
		for w in words:
			t = self.trie
			for c in w:
				if c not in t:
					t[c] = {}
				t = t[c]
			t['*'] = '*'

	def query(self, letter: str) -> bool:
		new_ptr = self.trie
		self.pointers.append(new_ptr)
		new_pointers = []
		ret_val = False
		for p in self.pointers:
			if letter in p:
				p = p[letter]
				if '*' in p:
					ret_val = True
				new_pointers.append(p)
		self.pointers = new_pointers
		return ret_val


class TrieNode:

	def __init__(self):
		self.children = {}
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = self.build(words)
		self.history = []

	def query(self, letter: str) -> bool:
		self.history.append(self.root)
		tmp = [node.children[letter] for node in self.history if letter in node.children]
		self.history = tmp
		for node in tmp:
			if node.is_word:
				return True
		return False

	def build(self, words):
		root = TrieNode()
		for word in words:
			tmp = root
			for c in word:
				if c not in tmp.children:
					tmp.children[c] = TrieNode()
				tmp = tmp.children[c]
			tmp.is_word = True
		return root


class StreamChecker:

	def __init__(self, words: List[str]):
		self.letters = ''
		self.words = words
		self.max_length = max([len(x) for x in words])

	def query(self, letter: str) -> bool:
		self.letters += letter
		self.letters = self.letters[-self.max_length:]
		for word in self.words:
			if word[-1] != letter:
				continue
			letters_word = self.letters[-len(word):]
			if word == letters_word:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			root = self.trie
			for ch in word:
				if not ch in root:
					root[ch] = {}
				root = root[ch]
			root['#'] = '#'
		self.nodes = [self.trie]

	def query(self, letter: str) -> bool:
		new_nodes = [self.trie]
		res = False
		for node in self.nodes:
			if letter in node:
				new_node = node[letter]
				if '#' in new_node:
					res = True
				new_nodes.append(new_node)
		self.nodes = new_nodes
		return res


class StreamChecker:

	def __init__(self, words: List[str]):
		self.dict_tree = {}
		for i in words:
			cur = self.dict_tree
			for j in i:
				if j not in cur:
					cur[j] = {}
				cur = cur[j]
			cur[-1] = True
		self.char_stream = []
		self.to_check = []

	def query(self, letter: str) -> bool:
		temp = []
		find = False
		for i in self.to_check:
			if letter in i:
				i = i[letter]
				temp.append(i)
				if -1 in i:
					find = True
		if letter in self.dict_tree:
			if -1 in self.dict_tree[letter]:
				find = True
			temp.append(self.dict_tree[letter])
		self.to_check = temp
		return True if find else False


from typing import Dict

class Node:

	def __init__(self, char=None):
		self.char = char
		self.is_complete_word = False
		self.children: Dict[str, 'Node'] = dict()

class Trie:

	def __init__(self):
		self.root = Node()

	def insert(self, word: str):
		current = self.root
		for char in word:
			if char not in current.children:
				current.children[char] = Node(char)
			current = current.children[char]
		current.is_complete_word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.waitlist = []
		self._max_length = 0
		self.trie = Trie()
		for word in words:
			self.trie.insert(word[::-1])
			self._max_length = max(self._max_length, len(word))

	def query(self, letter: str) -> bool:
		self.waitlist.append(letter)
		i = len(self.waitlist) - 1
		node = self.trie.root
		while i >= 0:
			if node.is_complete_word:
				return True
			letter = self.waitlist[i]
			if letter not in node.children:
				return False
			node = node.children[letter]
			i -= 1
		return node.is_complete_word


class TrieNode:

	def __init__(self, end=False):
		self.end = end
		self.lnk = {}

class StreamChecker:

	def __init__(self, words: List[str]):
		self.head = TrieNode()
		for w in words:
			x = self.head
			for (i, c) in enumerate(w):
				if c not in x.lnk:
					y = TrieNode()
					x.lnk[c] = y
				x = x.lnk[c]
			x.end = True
		self.ptrs = []

	def query(self, letter: str) -> bool:
		self.ptrs.append(self.head)
		found = False
		new_ptrs = []
		for p in self.ptrs:
			if letter in p.lnk:
				q = p.lnk[letter]
				new_ptrs.append(q)
				found |= q.end
		self.ptrs = new_ptrs
		return found


class Trie:

	def __init__(self):
		self.children = defaultdict(bool)

	def insert(self, word):
		if word == '':
			self.children['@'] = True
		else:
			child = self.children[word[0]]
			if child:
				child.insert(word[1:])
			else:
				newChild = Trie()
				newChild.insert(word[1:])
				self.children[word[0]] = newChild

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Trie()
		for word in words:
			self.root.insert(word)
		self.pointers = []

	def query(self, letter: str) -> bool:
		newPointers = []
		retFlag = False
		if letter in self.root.children:
			newPointers.append(self.root.children[letter])
			if '@' in self.root.children[letter].children:
				retFlag = True
		for p in self.pointers:
			newP = p.children[letter]
			if newP:
				newPointers.append(newP)
				if '@' in newP.children:
					retFlag = True
		self.pointers = newPointers
		return retFlag


class StreamChecker:

	def __init__(self, words: List[str]):
		self.wordict = {}
		self.word = ''
		for word in words:
			node = self.wordict
			for c in word[::-1]:
				if c not in node:
					node[c] = {}
				node = node[c]
			node[0] = 0

	def query(self, letter: str) -> bool:
		self.word = self.word + letter
		n = len(self.word)
		nodes = self.wordict
		while n:
			if self.word[n - 1] in nodes:
				if 0 in nodes[self.word[n - 1]]:
					return True
				else:
					nodes = nodes[self.word[n - 1]]
					n -= 1
			else:
				return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = dict()
		for word in words:
			node = self.trie
			for c in word[::-1]:
				if c not in node:
					node[c] = dict()
				node = node[c]
			node['.'] = '.'
		self.syms = []

	def query(self, letter: str) -> bool:
		(trie, syms) = (self.trie, self.syms)
		syms.append(letter)
		node = trie
		for c in syms[::-1]:
			if c not in node:
				return False
			node = node[c]
			if '.' in node:
				return True
		return False


TERM_CHAR = '\\0'

def build_trie(words):
	trie = {}
	for word in words:
		current = trie
		for c in word:
			if c not in current:
				current[c] = {}
			current = current[c]
		current[TERM_CHAR] = True
	return trie

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = build_trie(words)
		self.trie_currents = []

	def query(self, letter: str) -> bool:
		found = False
		next_tries = []
		for current in self.trie_currents + [self.trie]:
			if letter in current:
				next_tries.append(current[letter])
				if current[letter].get(TERM_CHAR):
					found = True
		self.trie_currents = next_tries
		return found


class TrieNode:

	def __init__(self):
		self._values = set()
		self.next = None

	def insert(self, value):
		self._values.add(value)

	def has(self, value):
		return value in self._values

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = TrieNode()
		self.q = ''
		self.chars = set()
		self.words = set()
		for word in words:
			curr = self.root
			for index in range(len(word) - 1, -1, -1):
				char = word[index]
				curr.insert(char)
				self.chars.add(char)
				if not curr.__next__:
					curr.next = TrieNode()
				curr = curr.__next__
			self.words.add(word)

	def query(self, letter: str) -> bool:
		if letter not in self.chars:
			self.q = ''
			return False
		self.q = letter + self.q
		curr = self.root
		sub_q = ''
		for char in self.q:
			if not curr.has(char):
				return False
			sub_q = char + sub_q
			if sub_q in self.words:
				return True
			curr = curr.__next__
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		trie = dict()
		for word in words:
			t = trie
			for c in word:
				if not c in t:
					t[c] = dict()
				t = t[c]
			t[None] = True
		self.trie = trie
		self.current_tries = []

	def query(self, letter: str) -> bool:
		new_tries = []
		res = False
		for trie in self.current_tries:
			if letter in trie:
				trie = trie[letter]
				new_tries.append(trie)
				res = res | trie.get(None, False)
		if letter in self.trie:
			t = self.trie.copy()
			t = t[letter]
			res = res | t.get(None, False)
			new_tries.append(t)
		self.current_tries = new_tries
		return res


class StreamChecker:

	def __init__(self, words: List[str]):
		self.ord_a = ord('a')
		stop_ord = ord('z') - self.ord_a + 1
		self.trie = [None] * stop_ord
		self.trie.append(False)
		ord_l = 0
		for word in words:
			current = self.trie
			for l in reversed(word):
				ord_l = ord(l) - self.ord_a
				if current[ord_l] is None:
					current[ord_l] = [None] * stop_ord
					current[ord_l].append(False)
				current = current[ord_l]
			current[-1] = True
		self.stream = []

	def query(self, letter: str) -> bool:
		result = False
		ord_l = ord(letter) - self.ord_a
		self.stream.append(ord_l)
		current = self.trie
		for l in self.stream[::-1]:
			current = current[l]
			if current is None:
				return False
			elif current[-1]:
				return True
		return current[-1]


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = trie = {}
		for word in words:
			node = trie
			for c in word:
				node = node.setdefault(c, {})
			node['*'] = word
		self.candidates = []

	def query(self, letter: str) -> bool:
		new = []
		exists = False
		for candidate in self.candidates + [self.trie]:
			if letter in candidate:
				new.append(candidate[letter])
				if candidate[letter].get('*'):
					exists = True
		self.candidates = new
		return exists


import queue

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.max_len = 0
		for w in words:
			if w:
				p = self.trie
				self.max_len = max(self.max_len, len(w))
				for idx in range(len(w) - 1, 0, -1):
					ch = w[idx]
					if not ch in p:
						p[ch] = {}
					p = p[ch]
				p[w[0]] = {'$': None}
		self.letters = []

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		p = self.trie
		idx = len(self.letters) - 1
		while p and idx >= max(len(self.letters) - self.max_len, 0):
			if '$' in p:
				return True
			l = self.letters[idx]
			if l in p:
				p = p[l]
			else:
				return False
			idx -= 1
		else:
			return '$' in p
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		self.state = []
		for word in words:
			curr = self.root
			for c in list(word):
				if c not in curr:
					curr[c] = {}
				curr = curr[c]
			curr['*'] = {}

	def query(self, letter: str) -> bool:
		ret = False
		new_state = []
		if letter in self.root:
			self.state.append(self.root)
		for s in self.state:
			if letter in s:
				s = s[letter]
				if '*' in s:
					ret = True
				new_state.append(s)
		self.state = new_state
		return ret


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		self.letters = []
		for w in words:
			cur = self.trie
			for l in w[::-1]:
				if not l in cur:
					cur[l] = {}
				cur = cur[l]
			cur['end'] = {}

	def query(self, letter: str) -> bool:
		self.letters.append(letter)
		cur = self.trie
		for l in self.letters[::-1]:
			if not l in cur:
				return False
			cur = cur[l]
			if 'end' in cur:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		Trie = lambda : defaultdict(Trie)
		self.root = Trie()
		self.end = True
		self.words = words
		self.prevs = []
		for (i, word) in enumerate(words):
			reduce(dict.__getitem__, word, self.root)[self.end] = i

	def query(self, letter: str) -> bool:
		result = False
		self.prevs.append(self.root)
		if self.prevs:
			new_prevs = []
			for prev in self.prevs:
				if letter in prev:
					new_prevs.append(prev[letter])
					result = result or self.end in new_prevs[-1]
			self.prevs = new_prevs
		return result


class TrieNode:

	def __init__(self):
		(self.children, self.end_node) = ({}, False)

class Trie:

	def __init__(self):
		self.root = TrieNode()

	def insert(self, word):
		root = self.root
		for symbol in word:
			root = root.children.setdefault(symbol, TrieNode())
		root.end_node = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie()
		self.stream = []
		for word in words:
			self.trie.insert(word[::-1])

	def query(self, letter: str) -> bool:
		self.stream.append(letter)
		cur = self.trie.root
		for c in self.stream[::-1]:
			if c in cur.children:
				cur = cur.children[c]
				if cur.end_node:
					return True
			else:
				break
		return False


class Trie:

	def __init__(self):
		self.trie = {}

	def add_word(self, word):
		nextLevel = self.trie
		for (i, char) in enumerate(word):
			isWord = i == len(word) - 1
			if char in nextLevel:
				if isWord:
					nextLevel[char].isWord = True
				nextLevel = nextLevel[char].children
			else:
				node = TrieNode(char, isWord)
				nextLevel[char] = node
				nextLevel = nextLevel[char].children

class TrieNode:

	def __init__(self, char, isWord=False):
		self.char = char
		self.isWord = isWord
		self.children = {}

class StreamChecker:

	def __init__(self, words: List[str]):
		self.words = set(words)
		self.trie = Trie()
		for word in words:
			self.trie.add_word(word)
		self.nextLevels = []
		self.count = 0

	def query(self, letter: str) -> bool:
		nextLevels = []
		isWord = False
		if len(self.nextLevels):
			for level in self.nextLevels:
				if letter in level:
					if level[letter].isWord:
						isWord = True
					nextLevels.append(level[letter].children)
		if letter in self.trie.trie:
			nextLevels.append(self.trie.trie[letter].children)
			if self.trie.trie[letter].isWord:
				isWord = True
		self.nextLevels = nextLevels
		return isWord


class StreamChecker:

	class Node:

		def __init__(self, isWord=False):
			self.children = [None] * 26
			self.isWord = isWord
	import queue

	def __init__(self, words: List[str]):
		self.root = self.Node()
		self.N = 0
		for word in words:
			self.N = max(self.N, len(word))
			cursor = self.root
			for c in reversed(word):
				if (next := cursor.children[ord(c) - 97]):
					cursor = next
				else:
					newNode = self.Node()
					cursor.children[ord(c) - 97] = newNode
					cursor = newNode
			cursor.isWord = True
		self.buffer = [None] * self.N
		self.bIdx = self.N

	def query(self, letter: str) -> bool:
		self.bIdx = (self.bIdx - 1) % self.N
		self.buffer[self.bIdx] = letter
		cursor = self.root
		for i in range(self.bIdx, self.N):
			if (next := cursor.children[ord(self.buffer[i]) - 97]):
				if next.isWord:
					return True
				cursor = next
			else:
				return False
		for i in range(self.bIdx):
			if self.buffer[i]:
				if (next := cursor.children[ord(self.buffer[i]) - 97]):
					if next.isWord:
						return True
					cursor = next
				else:
					return False
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			sub = self.root
			for c in word:
				sub = sub.setdefault(c, {})
			sub['0'] = None
		self.current = []

	def query(self, letter: str) -> bool:
		newQueries = []
		checker = False
		for query in self.current:
			if letter in query:
				nextQuery = query[letter]
				checker = checker or '0' in nextQuery
				newQueries.append(nextQuery)
		if letter in self.root:
			nextQuery = self.root[letter]
			checker = checker or '0' in nextQuery
			newQueries.append(nextQuery)
		self.current = newQueries
		return checker


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			node = self.root
			for c in word:
				node.setdefault(c, {})
				node = node[c]
			node[''] = True
		self.ptrs = []

	def query(self, letter: str) -> bool:
		ptrs = self.ptrs
		ptrs.append(self.root)
		(i, L) = (0, len(ptrs))
		found = False
		while i < L:
			if letter in ptrs[i]:
				ptrs[i] = ptrs[i][letter]
				if '' in ptrs[i]:
					found = True
				i += 1
			else:
				ptrs[i] = ptrs[L - 1]
				ptrs.pop()
				L -= 1
		return found


class StreamChecker:

	def __init__(self, words: List[str]):
		self.context = []
		self.map = [{}, False]
		for w in words:
			it = self.map
			for c in w:
				if c not in it[0]:
					it[0][c] = [{}, False]
				it = it[0][c]
			it[1] = True

	def query(self, letter: str) -> bool:
		new_context = []
		for m in self.context:
			if letter in m[0]:
				new_context.append(m[0][letter])
		if letter in self.map[0]:
			new_context.append(self.map[0][letter])
		self.context = new_context
		for m in self.context:
			if m[1]:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.Dict = collections.defaultdict(set)
		self.queries = ''
		for word in words:
			self.Dict[word[-1]].add(word)

	def query(self, letter: str) -> bool:
		if letter not in self.Dict:
			self.queries += letter
			return False
		else:
			temp = set([len(x) for x in self.Dict[letter]])
			self.queries += letter
			for length in temp:
				if length <= len(self.queries) and self.queries[-1:-length - 1:-1][::-1] in self.Dict[letter]:
					return True


class Trie(dict):
	word = False

	def add(self, word):
		t = self
		for c in word:
			if c not in t:
				t[c] = Trie()
			t = t[c]
		t.word = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.t = Trie()
		for w in words:
			self.t.add(w)
		self.s = []

	def query(self, c: str) -> bool:
		self.s = [t[c] for t in self.s if c in t]
		if c in self.t:
			self.s.append(self.t[c])
		return any((t.word for t in self.s))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.waitlist = []
		T = lambda : collections.defaultdict(T)
		self.trie = T()
		for word in words:
			temp_dict = self.trie
			for letter in word:
				temp_dict = temp_dict[letter]
			temp_dict['#'] = True

	def query(self, letter: str) -> bool:
		waitlist = []
		if letter in self.trie:
			waitlist.append(self.trie[letter])
		for item in self.waitlist:
			if letter in item:
				waitlist.append(item[letter])
		self.waitlist = waitlist
		return any(('#' in item for item in self.waitlist))


class StreamChecker:

	class TrieNode:

		def __init__(self):
			self.d = {}
			self.word = False

		def add(self, word):
			node = self
			for c in word:
				if c not in node.d:
					node.d[c] = StreamChecker.TrieNode()
				node = node.d[c]
			node.word = True

		def __repr__(self):
			return f'{self.d}'

	def __init__(self, words: List[str]):
		t = self.TrieNode()
		for word in words:
			t.add(word)
		self.root = t
		self.tries = []

	def query(self, letter: str) -> bool:
		self.tries = [e.d[letter] for e in self.tries if letter in e.d]
		if letter in self.root.d:
			self.tries.append(self.root.d[letter])
		print(len(self.tries))
		return any([e.word for e in self.tries])


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = dict()
		for word in words:
			d = self.trie
			for char in word:
				if char not in d:
					d[char] = dict()
				d = d[char]
			d['*'] = False
		self.recent = list()

	def query(self, letter: str) -> bool:
		self.recent = [d[letter] for d in self.recent if letter in d]
		if letter in self.trie:
			self.recent.append(self.trie[letter])
		return any(('*' in d for d in self.recent))


class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = {}
		for word in words:
			node = self.root
			for c in word:
				node = node.setdefault(c, {})
			node['is_end'] = True
		self.running = []

	def query(self, letter: str) -> bool:
		survived = []
		found = False
		if letter in self.root:
			self.running.append(self.root)
		for node in self.running:
			if letter in node:
				survived.append(node[letter])
				found = found or 'is_end' in survived[-1]
		self.running = survived
		return found


from collections import deque

class Node:

	def __init__(self):
		self.child = dict()
		self.end = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = Node()
		for i in words:
			self.insert(self.root, i[::-1])
		self.key = deque()

	def insert(self, root, key):
		for i in key:
			if root.child.get(i, None) == None:
				root.child[i] = Node()
			root = root.child[i]
		root.end = True

	def query(self, letter: str) -> bool:
		self.key.appendleft(letter)
		curr = self.root
		for i in self.key:
			if curr.child.get(i, None) == None:
				break
			curr = curr.child[i]
			if curr.end:
				return True
		return False


class Node:

	def __init__(self, val='.'):
		self.val = val
		self.children = {}
		self.is_word = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.root = self.__init_trie(words)
		self.q = []

	def query(self, letter: str) -> bool:
		self.q.append(self.root)
		new_q = []
		found = False
		for node in self.q:
			if letter in node.children:
				child_node = node.children[letter]
				new_q.append(child_node)
				if child_node.is_word:
					found = True
		self.q = new_q
		return found

	def __init_trie(self, words):
		root = Node()
		for word in words:
			cur = root
			for (i, ch) in enumerate(word):
				if ch not in cur.children:
					cur.children[ch] = Node(ch)
				cur = cur.children[ch]
			cur.is_word = True
		return root


class Trie:

	def __init__(self, char):
		self.char = char
		self.children = {}
		self.isEnd = False

class TireTree:

	def __init__(self):
		self.root = Trie('')

	def insert(self, word):
		cur = self.root
		for c in word:
			if c not in cur.children:
				cur.children[c] = Trie(c)
				cur = cur.children[c]
			else:
				cur = cur.children[c]
		cur.isEnd = True

	def search(self, word, cur):
		for c in word:
			if c not in cur.children:
				return False
			cur = cur[c]
		return cur.isEnd

class StreamChecker:

	def __init__(self, words: List[str]):
		self.tree = TireTree()
		for word in words:
			self.tree.insert(word)
		self.root = self.tree.root
		self.paths = [self.root]

	def query(self, letter: str) -> bool:
		nextpaths = [self.root]
		flag = False
		for p in self.paths:
			if letter in p.children:
				node = p.children[letter]
				nextpaths.append(node)
				if node.isEnd:
					flag = True
		self.paths = nextpaths
		return flag


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			t = self.trie
			for w in word:
				if w in t:
					t = t[w]
				else:
					t[w] = {}
					t = t[w]
			t['$'] = word
		self.currWords = [self.trie]

	def query(self, letter: str) -> bool:
		q = self.currWords
		q.append(self.trie)
		newQ = []
		ans = False
		for node in q:
			if letter in node:
				if '$' in node[letter]:
					ans = True
				newQ.append(node[letter])
		self.currWords = newQ
		return ans


class StreamChecker:

	def __init__(self, words: List[str]):
		self.d = [{}, False]
		for word in words:
			condition = self.d
			for letter in word:
				if letter not in condition[0]:
					condition[0][letter] = [{}, False]
				condition = condition[0][letter]
			condition[1] = True
		self.pool = [self.d]

	def query(self, letter: str) -> bool:
		new_pool = [self.d]
		ret = False
		for one in self.pool:
			if letter in one[0]:
				if one[0][letter][1]:
					ret = True
				new_pool.append(one[0][letter])
		self.pool = new_pool
		return ret


class StreamChecker:

	class Node:

		def __init__(self):
			self.end = False
			self.child = dict()

	def __init__(self, words: List[str]):
		self._root = self.Node()
		self.seq = []

		def insert(word=''):
			node = self._root
			for c in reversed(word):
				if c not in node.child:
					node.child[c] = self.Node()
				node = node.child[c]
			node.end = True
		for word in words:
			insert(word)

	def _search(self) -> bool:
		node = self._root
		ind = len(self.seq)
		while ind:
			node = node.child.get(self.seq[ind - 1], None)
			if not node:
				return False
			if node.end:
				return True
			ind -= 1
		return False

	def query(self, letter: str) -> bool:
		self.seq.append(letter)
		if len(self.seq) > 2000:
			self.seq.pop(0)
		return self._search()


import collections

class StreamChecker:

	def __init__(self, words: List[str]):
		self.Trie = {}
		for word in words:
			parent = self.Trie
			for char in word:
				parent = parent.setdefault(char, {})
			parent['$'] = None
		self.waitList = []

	def query(self, letter: str) -> bool:
		waitList = []
		if letter in self.Trie:
			waitList.append(self.Trie[letter])
		for currTrie in self.waitList:
			if letter in currTrie:
				waitList.append(currTrie[letter])
		self.waitList = waitList
		return any(('$' in item for item in waitList))


def _trie():
	return collections.defaultdict(_trie)

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = _trie()
		for word in words:
			trie = self.trie
			for c in word:
				if c not in trie:
					trie[c] = _trie()
				trie = trie[c]
			trie['END']
		self.activePtr = []

	def query(self, letter: str) -> bool:
		matched = False
		if letter in self.trie:
			self.activePtr.append(self.trie)
		for (i, trie) in enumerate(self.activePtr):
			if letter in trie:
				trie = trie[letter]
				if 'END' in trie:
					matched = True
				self.activePtr[i] = trie
			else:
				self.activePtr[i] = None
		self.activePtr = [t for t in self.activePtr if t is not None]
		return matched


class Trie:

	def __init__(self):
		self.root = dict()
		self.ptr = []

	def add(self, word):
		curr = self.root
		for c in word:
			if not c in curr:
				curr[c] = dict()
			curr = curr[c]
		curr['$'] = dict()

	def search(self, c):
		self.ptr.append(self.root)
		new_ptr = []
		ans = False
		for ptr in self.ptr:
			if c in ptr:
				new_ptr.append(ptr[c])
		self.ptr = new_ptr
		return any(['$' in p for p in self.ptr])

class StreamChecker:

	def __init__(self, words: List[str]):
		self.T = Trie()
		for word in words:
			self.T.add(word)

	def query(self, letter: str) -> bool:
		return self.T.search(letter)


class TrieNode:

	def __init__(self):
		self.children = {}
		self.leaf = False

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = TrieNode()
		for word in words:
			node = self.trie
			for i in range(len(word)):
				c = word[i]
				if c not in node.children:
					node.children[c] = TrieNode()
				node = node.children[c]
				if i == len(word) - 1:
					node.leaf = True
		self.leads = []

	def query(self, letter: str) -> bool:
		new_leads = []
		result = False
		for lead in self.leads:
			if letter in lead.children:
				node = lead.children[letter]
				if node.leaf:
					result = True
				new_leads.append(node)
		if letter in self.trie.children:
			if self.trie.children[letter].leaf:
				result = True
			new_leads.append(self.trie.children[letter])
		self.leads = new_leads
		return result


class Node:

	def __init__(self, val='', endWord=False):
		self.val = val
		self.endWord = endWord
		self.children = dict()

class Trie:

	def __init__(self, words):
		self.root = Node()
		for word in words:
			self.insertWord(word)

	def insertWord(self, word):
		currentNode = self.root
		for letter in word:
			if not letter in currentNode.children:
				currentNode.children[letter] = Node(letter)
			currentNode = currentNode.children[letter]
		currentNode.endWord = True

class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = Trie(words)
		self.trieRoot = self.trie.root
		self.currentNodes = []

	def query(self, letter: str) -> bool:
		newNodes = []
		if letter in self.trieRoot.children:
			newNodes.append(self.trieRoot.children[letter])
		for node in self.currentNodes:
			if letter in node.children:
				newNodes.append(node.children[letter])
		self.currentNodes = newNodes
		for node in self.currentNodes:
			if node.endWord:
				return True
		return False


class StreamChecker:

	def __init__(self, words: List[str]):
		self.trie = {}
		for word in words:
			self.trie_insert(word)
		self.searches = []

	def trie_insert(self, word):
		curr = self.trie
		for letter in word:
			if letter not in curr:
				curr[letter] = {}
			curr = curr[letter]
		curr['*'] = True

	def query(self, letter: str) -> bool:
		self.searches.append(self.trie)
		self.searches = [node.get(letter) for node in self.searches if letter in node]
		return any(('*' in node for node in self.searches))