You are given two arrays A and B each containing N numbers. You need to choose exactly one number from A and exactly one number from B such that the index of the two chosen numbers is not same and the sum of the 2 chosen values is minimum. Formally, if you chose ith element from A whose value is x and jth element from B whose value is y, you need to minimize the value of (x+y) such that i is not equal to j.

Your objective is to find this minimum value.

**Input:**

The first line of input contains an integer denoting the test cases,t. The first line of each test case contains an integer N denoting the size of two arrays. Then each of the next two lines contains N space separated integers denoting values of the array A[] and B[] respectively.

**Output:**

Print the minimum sum which can be obtained under the conditions mentioned in the problem statement.If not possible print "-1" without quotes.

**Constraints:**

1<=T<=30

1<= N <=100000

1< =Array elements < =100000

**Example:**

**Input:**

1

5

5 4 3 2 1

1 2 3 4 5

**Output:**

2

**Explanation:**

Minimum sum will be obtained by choosing number at the last index of first array i.e. 5th element of the first array(1) and first index of the second array ie first element of second array (1).

Sum=1+1=2 as their indexes are different but sum is minimum.

Author: justrelax

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.

ShubhamJain18 | 205 |

makeitdonesolo | 140 |

lost_found | 124 |

maggiiiii | 118 |

SahatPrasad1 | 108 |

Bishnu Dev Panda | 777 |

okayboss | 551 |

ShubhamJain18 | 516 |

yk12 | 514 |

makeitdonesolo | 480 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.