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.

bruceewayne | 192 |

manvirag982 | 178 |

theAwesome | 168 |

shivam123ahuja | 167 |

Core_Ka_Bachha | 160 |

mr_kksparrow | 433 |

manvirag982 | 394 |

TheHandle | 282 |

rajupraaa1234 | 276 |

Exception_404 | 268 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.