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.

narahc321 | 170 |

agness | 163 |

lonecoder | 153 |

2016uec1342 | 142 |

HuHu | 140 |

lonecoder | 1088 |

nikhil_sojan | 1082 |

KshatriyaYash | 1066 |

mazumderrohit8 | 928 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4504 |

Quandray | 4444 |

Login to report an issue on this page.