Given two sorted arrays such the arrays may have some common elements.

Find the sum of the maximum sum path to reach from beginning of any array to end of any of the two arrays. We can start from any array and switch from one array to another array only at common elements.

**Input:**

The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains two integers M and N denoting the size of two arrays.

Then two lines follow, first containing M space-separated integers A1, A2, ..., AM denoting the elements of the array and

second containing N space-separated integers A1, A2, ..., AN denoting the elements of the array.

**Output:**

Print the maximum sum.

**Constraints:**

1 <= T <= 100

1 <= N <= 100

0 <=A[i]<=100

**Example:**

**Input:**

2

8 8

2 3 7 10 12 15 30 34

1 5 7 8 10 15 16 19

3 4

1 2 3

1 1 2 4

**Output:**

122

8

**Explanation:**

For sample test case 2, the path will be 1 1 2 4 as we can switch between arrays at common elements .In this case we'll start from first array and then move

to second array to get the maximum sum path.

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

VISHESHA SADU | 27 |

ghanshyam648 | 25 |

Biswajit3 | 21 |

karthik_kasaragadda | 20 |

tanya_sharma | 18 |

KshatriyaYash | 1066 |

nikhil_sojan | 978 |

lonecoder | 935 |

mazumderrohit8 | 864 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4468 |

Quandray | 4444 |

Login to report an issue on this page.