Function G for part 2