for 9.8, add function with memory/cache
using map with pair as key