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.

cs_abhi | 102 |

PiyushPandey4 | 100 |

codefundo | 95 |

kya_bolti_public | 93 |

masterishaan19 | 74 |

PiyushPandey4 | 916 |

john_wick | 765 |

ASWATHAMA | 565 |

UsfShilpa | 556 |

akhyasharma01 | 547 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.