Read problems statements in Mandarin Chinese and Russian as well. 

Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.

------ Input ------ 

The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.

------ Output ------ 

For each test case, output the stem in a new line.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ n ≤ 10$
$1 ≤ |w[i]| ≤ 20$

----- Sample Input 1 ------ 
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------ 
grace
----- explanation 1 ------ 
The stem is grace.

Difficulty: MEDIUM_HARD

Solutions:
import sys
T = int(sys.stdin.readline())
for t in range(T):
	n = int(sys.stdin.readline())
	w = sys.stdin.readline().strip()
	words = w.split()
	shortest_word = min(words, key=len)
	longest_substr_len = 0
	longest_substr = ''
	for i in range(0, len(shortest_word)):
		for j in reversed(range(i + 1, len(shortest_word) + 1)):
			sub_str = shortest_word[i:j]
			found = True
			for word in words:
				if sub_str not in word:
					found = False
					break
			if found:
				if len(sub_str) > longest_substr_len:
					longest_substr_len = len(sub_str)
					longest_substr = sub_str
				elif len(sub_str) == longest_substr_len:
					if sub_str < longest_substr:
						longest_substr_len = len(sub_str)
						longest_substr = sub_str
	print(longest_substr)


t = int(input())
for tt in range(t):
	n = int(input())
	s = input().split()
	r = ''
	for i in range(len(s[0])):
		for j in range(i + 1, len(s[0]) + 1):
			t = s[0][i:j]
			for p in s:
				if p.count(t) == 0:
					t = ''
			if (len(t), r) > (len(r), t):
				r = t
	print(r)


for _ in range(int(input())):
	n = int(input())
	z = input().strip().split()
	w = list(z[:n])
	wlen = [len(aw) for aw in w]
	mlen = min(wlen)
	ref = w[wlen.index(mlen)]
	subs = []
	for pos in range(mlen):
		if all((ref[pos] in aw for aw in w)):
			subs.append(pos)
	if len(subs) == 0:
		print()
		continue
	tlen = 2
	while True:
		nsubs = []
		for pos in subs:
			if pos + 1 in subs:
				if all((ref[pos:pos + tlen] in aw for aw in w)):
					nsubs.append(pos)
		if len(nsubs) == 0:
			break
		subs = nsubs
		tlen += 1
	tlen -= 1
	ans = [ref[pos:pos + tlen] for pos in subs]
	ans.sort()
	print(ans[0])


test = int(input())
for i in range(test):
	n = int(input())
	string = input().split(' ')
	string.sort(key=len)
	subst = ''
	for j in range(len(string[0])):
		for k in range(j + 1, len(string[0]) + 1):
			word = string[0][j:k]
			value = True
			for l in string[1:]:
				if word not in l:
					value = False
					break
			if value:
				if len(word) > len(subst):
					subst = word
				elif len(word) == len(subst) and subst > word:
					subst = word
	print(subst)


import math
T = int(input())
for t in range(T):
	n = int(input())
	w = [x for x in input().split()]
	a = max(w, key=len)
	l = len(a)
	candidate = []
	while l > 0:
		i = 0
		while i + l <= len(a):
			if all((a[i:i + l] in s for s in w)):
				candidate.append(a[i:i + l])
				i += 1
			else:
				i += 1
		if not len(candidate) == 0:
			break
		l -= 1
	candidate.sort()
	print(candidate[0])


def rec(L, index, common):
	ans = []
	for i in range(0, len(common)):
		if L[index].find(common[i], 0, len(L[index])) > -1:
			ans.append(common[i])
	return ans
t = int(input())
for deju in range(t):
	L = []
	n = int(input())
	L = input().split()
	if n == 1:
		print(L[0])
		continue
	common = []
	for i in range(0, len(L[0])):
		for j in range(i, len(L[0])):
			if L[1].find(L[0][i:j + 1], 0, len(L[1])) > -1:
				common.append(L[0][i:j + 1])
	for i in range(2, n):
		common = rec(L, i, common)
	maxi = 0
	ans = []
	for i in range(len(common)):
		maxi = max(maxi, len(common[i]))
	for i in range(len(common)):
		if len(common[i]) == maxi:
			ans.append(common[i])
	ans.sort()
	print(ans[0])


import sys
T = sys.stdin.readline()
for i in range(int(T)):
	root = ''
	n = sys.stdin.readline()
	words = sys.stdin.readline().split()
	for j in range(len(words[0])):
		for k in range(len(words[0])):
			pot_root = words[0][j:k + 1]
			r = True
			for item in words:
				if pot_root not in item:
					r = False
			if r == True and len(pot_root) >= len(root):
				if len(root) == len(pot_root):
					if pot_root < root:
						root = pot_root
				else:
					root = pot_root
	print(root)


T = int(input())
for t in range(T):
	n = int(input())
	words = input().split()
	L = len(words[0])
	ans = ''
	for i in range(L):
		for j in range(L):
			s = words[0][j:i + 1]
			for word in words:
				if s not in word:
					break
			else:
				if len(s) > len(ans) or (len(s) == len(ans) and s < ans):
					ans = s
	print(ans)


import sys
import queue
t = int(input())
while t:
	n = int(input())
	line = input().split()
	w = [x for x in line]
	n1 = len(w[0])
	ans = ''
	for size in range(1, n1 + 1):
		for i in range(0, n1 - size + 1):
			flag = True
			for k in range(n):
				if w[0][i:i + size] not in w[k]:
					flag = False
					break
			if flag:
				if ans == '' or size > len(ans):
					ans = w[0][i:i + size]
				elif len(ans) == size:
					if w[0][i:i + size] < ans:
						ans = w[0][i:i + size]
	sys.stdout.write(ans + '\n')
	t -= 1


import sys
T = int(sys.stdin.readline())
for i in range(T):
	n = int(sys.stdin.readline())
	s = sys.stdin.readline().strip()
	a = s.split()
	min_string = min(a, key=len)
	length = 0
	sub_str = ''
	length_sub_str = 0
	longest_sub_str = ''
	for i in range(0, len(min_string)):
		for j in range(i + 1, len(min_string) + 1):
			sub_str = min_string[i:j]
			flag = False
			for ele in a:
				if sub_str in ele:
					flag = True
				else:
					flag = False
					break
			if flag == True:
				if length_sub_str < len(sub_str):
					length_sub_str = len(sub_str)
					longest_sub_str = sub_str
				elif length_sub_str == len(sub_str):
					if longest_sub_str > sub_str:
						longest_sub_str = sub_str
	print(longest_sub_str)