memoized build_query_r stack