def f(d,w):
a=[[w]]
while a:b=a;a=[x+[y]for x in a for y in set(d)-set(x)if x[-1][-1]==y[0]]
return b
print f(['hello', 'turtle', 'eat', 'cat', 'people'], 'artistic')
print f(['lemonade', 'meatball', 'egg', 'grape'], 'ham' )
print f(['cat', 'cute', 'ewok'], 'attic')
print f(['cat', 'cute', 'ewok', 'kilo', 'to', 'otter'], 'attic')
print f(['cat', 'today', 'yoda', 'attic'], 'ferret')
print f(['cancel', 'loitering', 'gnocchi', 'improv', 'vivic', 'child', 'despair', 'rat', 'tragic', 'chimney', 'rex', 'xylophone'], 'attic')
print f(['cat', 'today', 'yoda', 'artistic', 'cute', 'ewok', 'kilo', 'to', 'otter'], 'attic')
ZGVmIGYoZCx3KToKIGE9W1t3XV0KIHdoaWxlIGE6Yj1hO2E9W3grW3ldZm9yIHggaW4gYSBmb3IgeSBpbiBzZXQoZCktc2V0KHgpaWYgeFstMV1bLTFdPT15WzBdXQogcmV0dXJuIGIKCgpwcmludCBmKFsnaGVsbG8nLCAndHVydGxlJywgJ2VhdCcsICdjYXQnLCAncGVvcGxlJ10sICdhcnRpc3RpYycpCnByaW50IGYoWydsZW1vbmFkZScsICdtZWF0YmFsbCcsICdlZ2cnLCAnZ3JhcGUnXSwgJ2hhbScgKQpwcmludCBmKFsnY2F0JywgJ2N1dGUnLCAnZXdvayddLCAnYXR0aWMnKQpwcmludCBmKFsnY2F0JywgJ2N1dGUnLCAnZXdvaycsICdraWxvJywgJ3RvJywgJ290dGVyJ10sICdhdHRpYycpCnByaW50IGYoWydjYXQnLCAndG9kYXknLCAneW9kYScsICdhdHRpYyddLCAnZmVycmV0JykKcHJpbnQgZihbJ2NhbmNlbCcsICdsb2l0ZXJpbmcnLCAnZ25vY2NoaScsICdpbXByb3YnLCAndml2aWMnLCAnY2hpbGQnLCAnZGVzcGFpcicsICdyYXQnLCAndHJhZ2ljJywgJ2NoaW1uZXknLCAncmV4JywgJ3h5bG9waG9uZSddLCAnYXR0aWMnKQpwcmludCBmKFsnY2F0JywgJ3RvZGF5JywgJ3lvZGEnLCAnYXJ0aXN0aWMnLCAnY3V0ZScsICdld29rJywgJ2tpbG8nLCAndG8nLCAnb3R0ZXInXSwgJ2F0dGljJyk=
[['artistic', 'cat', 'turtle', 'eat']]
[['ham', 'meatball', 'lemonade', 'egg', 'grape']]
[['attic', 'cute', 'ewok']]
[['attic', 'cute', 'ewok', 'kilo', 'otter']]
[['ferret', 'today', 'yoda', 'attic', 'cat']]
[['attic', 'child', 'despair', 'rat', 'tragic', 'cancel', 'loitering', 'gnocchi', 'improv', 'vivic', 'chimney'], ['attic', 'cancel', 'loitering', 'gnocchi', 'improv', 'vivic', 'child', 'despair', 'rat', 'tragic', 'chimney']]
[['attic', 'cat', 'today', 'yoda', 'artistic', 'cute', 'ewok', 'kilo', 'otter']]