f=lambda s,a:[a[i]for x in[map(int,c.split('TO'))for c in s.split('AND')]for i in range(x[0]-1,x[-1]-2*(x[-1]<x[0]),1-2*(x[-1]<x[0]))if 0<=i<len(a)]

testA = ['H','e','l','l','o',' ','W','o','r','l','d']
for testS in ("1 TO 3", "5", "-10 TO 10", "0 AND 2 AND 4", "8 TO 3", "-300 AND 300", "1 TO 3 AND 3 TO 1", "-20 TO 0 AND 1 AND 4"):
	print testS
	print f(testS, testA)
	print ''
