Solution for set of stacks