restructure query