There are n users registered on a website CuteKittens.com. Each of them has a unique password represented by pass[1], pass[2], ..., pass[N]. As this a very lovely site, many people want to access those awesomely cute pics of the kittens. But the adamant admin does not want the site to be available to the general public, so only those people who have passwords can access it.

Yu, being an awesome hacker finds a loophole in the password verification system. A string which is a concatenation of one or more passwords, in any order, is also accepted by the password verification system. Any password can appear $\mbox{0}$ or more times in that string. Given access to each of the $n$ passwords, and also have a string $log in Atempt$, determine whether this string be accepted by the password verification system of the website.  If all of the $log in Atempt$ string can be created by concatenating password strings, it is accepted.  In this case, return the passwords in the order they must be concatenated, each separated by a single space on one line.  If the password attempt will not be accepted, return 'WRONG PWASSWORD'.

Examples 

$passwords=[\text{'abra'},\:\:\text{'ka'},\:\:\text{'dabra'}]$ 

$log inAtempt=\textbf{'abrakadabra'}$   

Concatenate the passwords in index order $0,1,2$ to match 'abrakadabra'. Return 'abra ka dabra'.  

$passwords=[\text{'abra'},\:\:\text{'ka'},\:\:\text{'dabra'}]$ 

$log in Atempt=\text{'kaabra'}$   

Concatenate the passwords in index order $\mathbf{1},\mathbf{0}$ to match 'kaabra'. Return 'ka abra'.  

$passwords=\textbf{['ab'},\:\:\textbf{'ba']}$ 

$log in Atempt=\textbf{'aba'}$   

Concatenate the passwords in index order $\mathbf{0,1}$ to match 'abba', $\mathbf{1},\mathbf{0}$ to match 'baab', $\boldsymbol{0},\boldsymbol{0}$ to match 'abab' or $\boldsymbol{1,1}$ to match $baba'.  No combination of 1 or more passwords can be concatenated to match 'aba'.  Return 'WRONG PASSWORD'.   

Function Description

Complete the passwordCracker function in the editor below.   

passwordCracker has the following parameters: 

- string passwords[n]: a list of password strings 

- string loginAttempt: the string to attempt to create  

Returns 

- string: Return the passwords as a single string in the order required for the password to be accepted, each separated by a space. If it is not possible to form the string, return the string WRONG PASSWORD.  

Input Format

The first line contains an integer t, the total number of test cases.  

Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of three lines is as follows: 

- The first line of each test case contains n, the number of users with passwords. 

- The second line contains n space-separated strings, passwords[i], that represent  the passwords of each user. 

- The third line contains a string, loginAttempt, which Yu must test for acceptance.  

Constraints

$1\leq t\leq10$  
$1\leq n\leq10$  
$passwords[i]\neq passwords[j],1\leq i<j\leq N$  
$1\leq|passwords[i]|\leq10$, where $i\in[1,n]$  
$1<|loginAtempt|\leq2000$ 
loginAttempt and passwords[i] contain only lowercase latin characters ('a'-'z').

Sample Input 0
3
6
because can do must we what
wedowhatwemustbecausewecan
2
hello planet
helloworld
3
ab abcd cd
abcd

Sample Output 0
we do what we must because we can
WRONG PASSWORD
ab cd

Explanation 0

Sample Case #00: "wedowhatwemustbecausewecan" is the concatenation of passwords {"we", "do", "what", "we", "must", "because", "we", "can"}. That is 

loginAttempt = pass[5] + pass[3] + pass[6] + pass[5] +  pass[4] + pass[1] + pass[5] + pass[2]

Note that any password can repeat any number of times.  

Sample Case #01: We can't create string "helloworld" using the strings {"hello", "planet"}.  

Sample Case #02: There are two ways to create loginAttempt ("abcd"). Both pass[2] = "abcd" and pass[1] + pass[3] = "ab cd" are valid answers.

Sample Input 1
3
4
ozkxyhkcst xvglh hpdnb zfzahm
zfzahm
4
gurwgrb maqz holpkhqx aowypvopu
gurwgrb
10
a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa aaaaaaaaa aaaaaaaaaa
aaaaaaaaaab

Sample Output 1
zfzahm
gurwgrb
WRONG PASSWORD

Source: hackerrank | Difficulty: MEDIUM
Tags: Complete search


Solutions:
class Trie:
	_end = '_end_'

	def __init__(self):
		self.root = dict()
		self.answer = []

	def insert(self, word):
		current_dict = self.root
		for letter in word:
			if letter not in current_dict:
				current_dict[letter] = dict()
			current_dict = current_dict[letter]
		current_dict[self._end] = self._end

	def find(self, word):
		current_dict = self.root
		ans = ''
		for letter in word:
			if letter not in current_dict:
				return
			ans += letter
			current_dict = current_dict[letter]
			if self._end in current_dict:
				yield ans

	def solve(self, loginattempt):
		store = [None] * len(loginattempt)
		for i in reversed(range(len(loginattempt))):
			for word in self.find(loginattempt[i:]):
				j = i + len(word)
				if j < len(loginattempt) and store[j]:
					store[i] = word + ' ' + store[j]
					break
				elif j == len(loginattempt):
					store[i] = word
					break
		if store[0]:
			return store[0]
		else:
			return 'WRONG PASSWORD'
for t in range(int(input())):
	n = int(input())
	trie = Trie()
	for word in input().strip().split():
		trie.insert(word)
	loginattempt = input().strip()
	print(trie.solve(loginattempt))


import sys
sys.setrecursionlimit(5000)

def hack(att):
	if att == '':
		return ''
	legit = [l for l in pwds if l == att[:len(l)]]
	if not legit:
		return False
	for l in legit:
		if att[len(l):] in seen:
			continue
		n = hack(att[len(l):])
		if n != False:
			return l + ' ' + n
		else:
			seen.add(att[len(l):])
	return False
for _ in range(int(input())):
	seen = set()
	n = int(input())
	pwds = input().split()
	att = input()
	h = hack(att)
	print(h if h else 'WRONG PASSWORD')


import sys
cache = {}

def isValid(s, strList):
	if (s, tuple(strList)) in cache:
		return cache[s, tuple(strList)]
	for substr in strList:
		if s == substr:
			cache[s, tuple(strList)] = (substr, True)
			return cache[s, tuple(strList)]
		elif len(s) >= len(substr) and s[0:len(substr)] == substr:
			output = isValid(s[len(substr):], strList)
			if output[1]:
				cache[s, tuple(strList)] = (substr + ' ' + output[0], True)
				return cache[s, tuple(strList)]
	cache[s, tuple(strList)] = (s, False)
	return cache[s, tuple(strList)]
sys.setrecursionlimit(2500)
n = int(input().strip())
for testCase in range(0, n):
	numPwrds = int(input().strip())
	pwrds = input().strip().split(' ')
	inp = input().strip()
	output = isValid(inp, pwrds)
	if output[1]:
		print(output[0])
	else:
		print('WRONG PASSWORD')


import sys
sys.setrecursionlimit(4096)

def find(pwords, login, valid, string, memo):
	if string == login:
		return
	for word in pwords:
		current = string + word
		if current in memo:
			continue
		if not login.startswith(current):
			memo[current] = False
			continue
		memo[current] = valid + [word]
		find(pwords, login, memo[current], current, memo)
T = int(input())
for _ in range(T):
	(N, pwords, login) = (input(), input().split(), input())
	memo = {}
	find(pwords, login, [], '', memo)
	result = memo.get(login) or ('WRONG', 'PASSWORD')
	print(' '.join(result))


import sys

def crack_reversed(passwords, attempt, st=0):
	if st in crack_reversed.memo:
		return crack_reversed.memo[st]
	for passwd in passwords:
		if st + len(passwd) > len(attempt):
			continue
		elif passwd != attempt[st:st + len(passwd)]:
			continue
		if st + len(passwd) == len(attempt):
			crack_reversed.memo[st] = (True, [passwd])
			return (True, [passwd])
		(res, words) = crack_reversed(passwords, attempt, st + len(passwd))
		if res:
			crack_reversed.memo[st] = (True, words + [passwd])
			return (True, words + [passwd])
	crack_reversed.memo[st] = (False, tuple())
	return (False, [])

def crack(passwords, attempt, st=0):
	crack_reversed.memo = {}
	(res, words) = crack_reversed(passwords, attempt, st)
	return (res, list(reversed(words)))
sys.setrecursionlimit(2048)
tests_cnt = int(input())
for _ in range(tests_cnt):
	_ = int(input())
	passwords = input().split()
	attempt = input()
	(res, words) = crack(passwords, attempt)
	if res:
		print(' '.join(words))
	else:
		print('WRONG PASSWORD')


import sys
sys.setrecursionlimit(2017)
t = int(input())
mem = {}

def cracker(psws, psw):
	v = None
	if psw in mem:
		return mem[psw]
	if psw == '':
		mem[psw] = []
		return mem[psw]
	for p in psws:
		if psw.startswith(p):
			v = cracker(psws, psw[len(p):])
			if v != None:
				v.insert(0, p)
				mem[psw] = v
				return mem[psw]
	mem[psw] = None
	return mem[psw]
for _ in range(t):
	input()
	psws = input().strip().split(' ')
	superpsw2 = input().strip()
	mem = {}
	print(' '.join(cracker(psws, superpsw2) or ['WRONG PASSWORD']))


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


n = int(input().strip())
for i in range(n):
	a = input()
	psw = input().strip().split(' ')
	att = input().strip()
	arr = [[] for i in att + max(((len(i), i) for i in psw))[1]]
	arr[0] = 1
	for i in range(len(att)):
		if arr[i]:
			for j in psw:
				if att[i:].startswith(j):
					arr[i + len(j)] = [i, j]
	if not arr[len(att)]:
		print('WRONG PASSWORD')
	else:
		i = len(att)
		chk = []
		while i != 0:
			chk.append(arr[i][1])
			i = arr[i][0]
		print(' '.join(chk[::-1]))


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(20000)

def match(a, pwords, l, visited, idx):
	for p in pwords:
		if p == a[idx:idx + len(p)]:
			l.append(p)
			if len(p) == len(a) - idx:
				return True
			new_len = len(a) - idx - len(p)
			if new_len in visited:
				l.pop()
				continue
			if match(a, pwords, l, visited, idx + len(p)):
				return True
			visited.add(new_len)
			l.pop()
	return False
ntest = int(input())
for i in range(ntest):
	input()
	passwords = [p for p in input().split(' ')]
	attempt = input()
	l = []
	v = set()
	if match(attempt, passwords, l, v, 0):
		print(' '.join(l))
	else:
		print('WRONG PASSWORD')


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
for _ in range(int(input())):
	input()
	l = input().split()
	w = input()
	d = {}
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	mem = set()
	if not set(''.join(l)) >= set(w):
		print('WRONG PASSWORD')
		continue
	if set(w) <= set(l):
		print(' '.join(list(w)))
		continue
	ans = find_password(d, w, '', mem)
	print(ans if ans else 'WRONG PASSWORD')


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


from sys import setrecursionlimit
setrecursionlimit(10000)

def check_password_recursive_cache(words, password, solutions):
	if solutions.get(password) == None:
		solutions[password] = check_password_recursive(words, password, solutions)
	return solutions.get(password)

def check_password_recursive(words, password, solutions):
	current = ''
	for i in range(len(password)):
		current = password[0:i + 1]
		if words.get(current) != None:
			if i + 1 == len(password):
				return password
			else:
				resp = check_password_recursive_cache(words, password[i + 1:], solutions)
				if resp != 'WRONG PASSWORD':
					return current + ' ' + resp
	return 'WRONG PASSWORD'
times = int(input())
for i in range(times):
	vocab_size = int(input())
	words = input().split(' ')
	password = input()
	d = {}
	for w in words:
		d[w] = 1
	print(check_password_recursive_cache(d, password, {}))


class node:

	def __init__(self, val):
		self.val = val
		self.children = [None for i in range(ord('z') - ord('a') + 1)]
		self.endsHere = False

class trie:

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

	def add(self, s):
		n = len(s)
		temp = self.root
		for (i, c) in enumerate(s):
			pos = ord(c) - ord('a')
			if not temp.children[pos]:
				temp.children[pos] = node(c)
			temp = temp.children[pos]
			if i == n - 1:
				temp.endsHere = True

	def solve(self, pos, arr, s):
		n = len(s)
		temp = self.root
		for i in range(pos + 1, n):
			j = ord(s[i]) - ord('a')
			if temp.children[j]:
				temp = temp.children[j]
			else:
				break
			if temp.endsHere:
				arr[i] = pos

	def stringify(self, s, arr):
		if arr[-1] == -2:
			return 'WRONG PASSWORD'
		else:
			i = arr[-1]
			pos = n - 1
			answer = []
			while pos >= 0:
				if pos == i:
					answer.append(' ')
					i = arr[i]
				answer.append(s[pos])
				pos -= 1
			return ''.join(answer[::-1])
t = int(input())
for _ in range(t):
	tr = trie()
	n = int(input())
	passwords = input().split()
	for password in passwords:
		tr.add(password)
	s = list(input().strip())
	n = len(s)
	arr = [-2 for i in range(n)]
	for i in range(-1, n):
		if i < 0 or arr[i] != -2:
			tr.solve(i, arr, s)
	print(tr.stringify(s, arr))


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2020)

def check(la, pws):
	bad = []
	wrong = 'WRONG PASSWORD'

	def check1(k):
		for p in pws:
			lenp = len(p)
			kent = k + lenp
			if kent in bad:
				continue
			if p != la[k:kent]:
				continue
			if kent == len(la):
				return p
			r = check1(kent)
			if r != wrong:
				return p + ' ' + r
			bad.append(kent)
		return wrong
	c = check1(0)
	return c
t = int(input())
for oe in range(t):
	n = int(input())
	passw = input().split(' ')
	loginAttempt = input()
	print(check(loginAttempt, passw))


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(5000)

def crack(password: str, answer: list):
	if password in meme:
		return meme[password]
	if not password:
		return answer
	for word in words:
		l = len(word)
		if password[:l] == word:
			ans = crack(password[l:], answer + [word])
			if ans:
				meme[password] = ans
				return ans
	meme[password] = []
	return []
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	words = input().strip().split()
	password = input().strip()
	meme = {}
	answer = crack(password, [])
	if not answer:
		print('WRONG PASSWORD')
	else:
		print(' '.join(answer))


from sys import setrecursionlimit
setrecursionlimit(2200)

def read():
	return list(map(int, input().split()))
data = []
mem = []
p = ''
s = set()

def isV(ind, p):
	if mem[ind] != -1:
		return mem[ind]
	if ind == len(p):
		mem[ind] = True
		return True
	lp = ''
	for i in range(ind, len(p)):
		lp += p[i]
		if lp in s and isV(i + 1, p):
			data.insert(0, lp)
			mem[ind] = True
			return True
	mem[ind] = False
	return False
t = read()[0]
for _ in range(t):
	n = read()
	s = set(input().split())
	p = input()
	data = []
	mem = [-1] * (len(p) + 1)
	st = isV(0, p)
	if st:
		print(' '.join(data))
	else:
		print('WRONG PASSWORD')


words = {}
attempt = ''
l = 0
dp = {}

def findAns(n):
	if n in dp:
		return dp[n]
	for i in range(n + 1, l + 1):
		x = attempt[n:i]
		if x in words:
			ans = findAns(i)
			if ans == None:
				continue
			else:
				ans = [x] + ans
				dp[n] = ans
				return ans
	dp[n] = None
	return None
for _ in range(int(input())):
	input()
	words = input().split()
	attempt = input()
	l = len(attempt)
	dp = {l: []}
	for i in range(l - 1, 0, -1):
		findAns(i)
	ans = findAns(0)
	if ans:
		print(' '.join(ans))
	else:
		print('WRONG PASSWORD')


import sys
sys.setrecursionlimit(2000)

def solve(pwds, login, els=[], mem=None):
	if login == '':
		return []
	if login[0] not in pwds.keys():
		return None
	if login in mem:
		return None
	for m in pwds[login[0]]:
		if login.startswith(m):
			elr = solve(pwds, login[len(m):], els=els, mem=mem)
			if elr != None:
				elr.append(m)
				return elr
	mem.add(login)
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	mem = set()
	ans = solve(d, w, mem=mem)
	if ans == None:
		print('WRONG PASSWORD')
	else:
		ans.reverse()
		print(' '.join(ans))


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


def rec(c, i, w, a):
	j = i + 1
	r = []
	if i >= len(w):
		return ['']
	while i < len(w) and j <= len(w):
		if w[i:j] in a and c[j] != 'WRONG PASSWORD':
			return w[i:j] + ' ' + c[j]
		else:
			j += 1
	return 'WRONG PASSWORD'
t = int(input())
for _ in range(t):
	u = int(input())
	a = input().split()
	w = input()
	c = [''] * len(w) + ['']
	for i in range(len(w) - 1, -1, -1):
		c[i] = rec(c, i, w, a)
	print(c[0])


import sys
sys.setrecursionlimit(2500)

def repentSins(noOfWords, setOfWords, loginAttempt, lookup):
	if lookup.get(loginAttempt, -1) == -1:
		if len(loginAttempt) == 0:
			lookup[loginAttempt] = [-2]
			return lookup[loginAttempt]
		for i in setOfWords:
			if loginAttempt.find(i, 0, len(i)) != -1:
				lookup[loginAttempt] = [i] + repentSins(noOfWords, setOfWords, loginAttempt[len(i):], lookup)
				if lookup[loginAttempt][::-1][0] == -2:
					return lookup[loginAttempt]
		lookup[loginAttempt] = [-1]
		return lookup[loginAttempt]
	return lookup[loginAttempt]
temp = 0
n = int(input())
for i in range(n):
	noOfWords = int(input())
	setOfWords = str(input())
	setOfWords = setOfWords.split()
	loginAttempt = str(input())
	lookup = {}
	temp = repentSins(noOfWords, setOfWords, loginAttempt, lookup)
	if temp[::-1][0] == -2:
		for j in range(len(temp) - 1):
			print(temp[j], end=' ')
		print()
	else:
		print('WRONG PASSWORD')


import collections

def solve_no_recurse(attempt, words):
	seen = set()
	stack = collections.deque()
	stack.append((0, []))
	while stack:
		(start, result) = stack.pop()
		if start == len(attempt):
			return result
		for word in words:
			sum_ = start + len(word)
			if sum_ in seen:
				continue
			if attempt.startswith(word, start):
				seen.add(sum_)
				stack.append((sum_, result + [word]))
	return None

def minimize_word_list(words):
	word_set = set(words)
	for word in words:
		result = solve_no_recurse(attempt=word, words=word_set - {word})
		if result:
			word_set.discard(word)
	return word_set

def main():
	for _ in range(int(input())):
		input()
		words = input().split()
		words = sorted(minimize_word_list(words), key=len, reverse=True)
		attempt = input()
		result = solve_no_recurse(words=words, attempt=attempt)
		if not result:
			print('WRONG PASSWORD')
		else:
			print(*result)
main()


import sys
sys.setrecursionlimit(4000)
debug = False

def print_(*args):
	if debug:
		print(*args)

def password_verification(passwords, login, indent, memo):
	cached_answer = memo.get(login, False)
	if cached_answer is not False:
		return cached_answer
	spaces = ' ' * indent
	login_len = len(login)
	for i in range(1, login_len + 1):
		phrase = login[:i]
		print_(spaces, 'testing', phrase)
		if phrase in passwords:
			sub_login = login[i:]
			print_(spaces, 'sub_login', sub_login)
			if sub_login == '':
				return phrase
			else:
				answer = password_verification(passwords, sub_login, indent + 1, memo)
			print_(spaces, 'sub answer', answer)
			if answer is not None:
				answer = '{} {}'.format(phrase, answer)
				memo[login] = answer
				print_(spaces, 'answer', answer)
				return answer
	print_(spaces, 'nada', login)
	memo[login] = None
	return None
num_cases = int(input().strip())
for i in range(0, num_cases):
	num_passwords = int(input().strip())
	passwords = input().strip().split(' ')
	login_attempt = input().strip()
	result = password_verification(passwords, login_attempt, 0, {})
	if result is None:
		print('WRONG PASSWORD')
	else:
		print(result)


import sys
sys.setrecursionlimit(5000)

def solution(attempt, words):
	if attempt in results:
		return results[attempt]
	if attempt == '':
		return []
	for word in words:
		if attempt.startswith(word):
			n = len(word)
			remainder = attempt[n:]
			result = solution(remainder, words)
			if result != ['WRONG PASSWORD']:
				results[attempt] = [word] + result
				return [word] + result
	results[attempt] = ['WRONG PASSWORD']
	return ['WRONG PASSWORD']

def dpsolution(attempt, words):
	n = len(attempt)
	OPT = []
	for i in range(n):
		print(OPT)
		if attempt[:i + 1] in words:
			OPT.append([attempt[i + 1]])
		else:
			for j in range(0, i):
				if OPT[j] and attempt[j + 1:i + 1] in words:
					OPT.append(OPT[j] + [attempt[j + 1:i + 1]])
					success = True
		if len(OPT) != i + 1:
			OPT.append([])
	return OPT[-1]
T = int(input())
for _ in range(T):
	n = int(input())
	words = input().split()
	attempt = input()
	results = {}
	print(*solution(attempt, words))


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2500)

def stringTree(passwords, inputted, memo):
	if memo.get(inputted) == None:
		for i in passwords:
			if inputted[:len(i)] == i:
				if len(inputted[len(i):]) == 0:
					return [i] + [-2]
				memo[inputted] = [i] + stringTree(passwords, inputted[len(i):], memo)
				if memo[inputted][-1] == -2:
					return memo[inputted]
		else:
			return [-3]
	else:
		return memo[inputted]

def mainStringTree(passwords, inputted):
	memo = {}
	temp = stringTree(passwords, inputted, memo)
	if temp[-1] == -3:
		return 'WRONG PASSWORD'
	elif temp[-1] == -2:
		output = ''
		for i in temp[:len(temp) - 1]:
			output += str(i)
			output += ' '
		return output[:len(output) - 1]

def passwordLite():
	cases = int(input())
	for i in range(cases):
		numOfWords = int(input())
		passwords = list(input().split(' '))
		inputted = input()
		print(mainStringTree(passwords, inputted))
	return
passwordLite()


def lenght(arr):
	res = 0
	for string in arr:
		res += len(string)
	return res

def isValid(attempt, passwd):
	maxLen = max([len(pas) for pas in passwd])
	solution = [[] for i in range(len(attempt))]
	for i in range(len(attempt)):
		for j in range(i, i - maxLen, -1):
			if j < 0:
				break
			if attempt[j:i + 1] in passwd:
				if j != 0:
					if solution[j - 1] == []:
						continue
					solution[i] = solution[j - 1]
				solution[i] = solution[i] + [attempt[j:i + 1]]
				break
	if lenght(solution[-1]) != len(attempt):
		return ['WRONG', 'PASSWORD']
	return solution[-1]
nTests = int(input())
for i in range(nTests):
	int(input())
	passwd = input().split(' ')
	attempt = input()
	print(' '.join(isValid(attempt, passwd)))


import sys
sys.setrecursionlimit(4000)

def checkpassword(target, build, combi, level):
	if target == build:
		return combi
	answer = 'WRONG PASSWORD'
	for password in passwords:
		if build + '' + password in target[:len(build + '' + password)]:
			if build not in wrong:
				answer = checkpassword(target, build + '' + password, combi + password + ' ', level + 1)
			if answer != 'WRONG PASSWORD':
				return answer
	wrong.append(build)
	return answer
testcases = int(input())
passwords = []
testinput = []
for case in range(testcases):
	input()
	passwords = []
	passwords = input().split()
	testinput = input().strip()
	wrong = []
	sorted(passwords, key=len)
	print(checkpassword(testinput, '', '', 0))


tests = []
n_t = int(input().strip())
import sys
sys.setrecursionlimit(2000)
while n_t > 0:
	n_pass = int(input().strip())
	passwords = input().strip().split()
	target = input().strip()
	tests.append((passwords, target))
	n_t -= 1
visited = {}

def solver(passwords, target):
	target_set = set(target)
	word_set = set(passwords)
	l_set = set(''.join(passwords))
	if target_set > l_set:
		res = 'WRONG PASSWORD'
	elif target_set <= word_set:
		res = ' '.join(list(target))
	else:
		ans = ''
		mem = set()
		dic = {}
		for w in word_set:
			if w[0] not in dic:
				dic[w[0]] = [w]
			else:
				dic[w[0]].append(w)
		res = helper(dic, target, ans, mem)
		res = 'WRONG PASSWORD' if res == '' else res
	return res

def helper(dic, target, ans, mem):
	if target == '':
		return ans
	if target in mem:
		return ''
	if target[0] in dic:
		for w in dic[target[0]]:
			if w == target[:len(w)]:
				curr_ans = helper(dic, target[len(w):], ans + w + ' ', mem)
				if curr_ans != '':
					return curr_ans
	mem.add(target)
	return ''
for (passwords, target) in tests:
	print(solver(passwords, target))


def cracker(pwd, words):
	guide = [None] * (len(pwd) + 1)
	guide[0] = -1
	for i in range(min(len(words[0]) - 1, 0), len(pwd)):
		j = 0
		done = False
		while j < len(words) and (not done) and (len(words[j]) - 1 <= i):
			if words[j] == pwd[i - len(words[j]) + 1:i + 1] and guide[i - len(words[j]) + 1] != None:
				guide[i + 1] = j
				done = True
			j += 1
	if guide[len(pwd)] == None:
		print('WRONG PASSWORD')
	else:
		w = words[guide[-1]]
		passes = w
		j = len(pwd) - len(w)
		while j > 0:
			w = words[guide[j]]
			passes = w + ' ' + passes
			j -= len(w)
		print(passes)
for z in range(int(input())):
	input()
	p = input().split()
	p = sorted(p, key=lambda x: len(x))
	code = input()
	cracker(code, p)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


for _ in range(int(input())):
	n = int(input())
	pw = input().strip().split()
	s = input().strip()
	ans = [[] for _ in range(len(s))]
	for i in range(len(s) - 1, -1, -1):
		for w in pw:
			if i + len(w) < len(s) and ans[i + len(w)] and (s[i:i + len(w)] == w):
				ans[i] = [w] + ans[i + len(w)]
				break
			elif i + len(w) == len(s) and s[i:] == w:
				ans[i] = [w]
				break
	print(' '.join(ans[0]) or 'WRONG PASSWORD')


import sys
sys.setrecursionlimit(2000)

def find_password(d, w, cur, mem):
	if w == '':
		return cur
	if w in mem:
		return ''
	if w[0] in d:
		for i in d[w[0]]:
			if w[:len(i)] == i:
				cur_ans = find_password(d, w[len(i):], cur + i + ' ', mem)
				if cur_ans != '':
					return cur_ans
	mem.add(w)
	return ''
t = int(input())
for _ in range(t):
	n = int(input())
	l = input().split()
	w = input()
	password_set = set(''.join(l))
	word_set = set(w)
	list_set = set(l)
	if not password_set >= word_set:
		print('WRONG PASSWORD')
		continue
	if word_set <= list_set:
		print(' '.join(list(w)))
		continue
	d = {}
	mem = set()
	for i in l:
		if i[0] in d:
			d[i[0]].append(i)
		else:
			d[i[0]] = [i]
	ans = find_password(d, w, '', mem)
	print('WRONG PASSWORD' if ans == '' else ans)


from sys import setrecursionlimit
setrecursionlimit(4000)

def parse(users, pass_arr, login_attempt):

	def parse_(login_attempt, cur, cache):
		if len(login_attempt) == 0:
			return cur
		if login_attempt in cache:
			return ''
		for i in range(users):
			if login_attempt.startswith(pass_arr[i]):
				cur_ans = parse_(login_attempt[len(pass_arr[i]):], cur + ' ' + pass_arr[i], cache)
				if cur_ans != '':
					return cur_ans
		cache.add(login_attempt)
		return ''
	return parse_(login_attempt, '', set([]))
testcases = int(input())
for i in range(testcases):
	users = int(input())
	pass_arr = input().split()
	login_attempt = input()
	ans = parse(users, pass_arr, login_attempt)
	print('WRONG PASSWORD' if ans == '' else ans)


import functools
import sys
sys.setrecursionlimit(20000)
t = int(input())
for _ in range(t):
	_ = int(input())
	pws = sorted(input().split(), key=len)
	s = input()

	@functools.lru_cache(len(s))
	def test(i):
		if i == len(s):
			return []
		for p in pws:
			if s[i:i + len(p)] == p:
				res = test(i + len(p))
				if res is not None:
					return [p] + res
	result = test(0)
	print(' '.join(result) if result else 'WRONG PASSWORD')