Given two arrays a[] and b[] of same size.Your task is to find minimum sum of two elements such that they belong to different arrays and are not at same index in their arrays.
Example 1:
Input : 
a[] = {5, 4, 13, 2, 1}
b[] = {2, 3, 4, 6, 5}
Output : 
3
Explanation :
We take 1 from a[] and 2 from b[]
Sum is 1 + 2 = 3.
 
Example 2:
Input : 
a[] = {5, 4, 13, 1}
b[] = {3, 2, 6, 1}
Output : 
3
Explanation :
We take 1 from a[] and 2 from b[].
Note that we can't take 1 from b[]
as the elements can not be at same
index. 
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function minSum() which takes the array A[], B[] and its size N as inputs and returns the minimum sum.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1<=N<=10^{5}
1<=a[i]<=10^{5}
1<=b[i]<=10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Solution:
    def minSum(self,  a, b, n):
        # code here
    


Solutions:
class Solution:

	def minSum(self, a, b, n):
		a1 = min(a)
		b1 = min(b)
		if a.index(a1) != b.index(b1):
			return a1 + b1
		else:
			b.sort()
			a.sort()
			a2 = a[1]
			b2 = b[1]
			return min(a1 + b2, a2 + b1)


class Solution:

	def minSum(self, a, b, n):
		c = sorted(a)
		d = sorted(b)
		if a.index(c[0]) != b.index(d[0]):
			return c[0] + d[0]
		else:
			return min(c[0] + d[1], d[0] + c[1])


class Solution:

	def minSum(self, a, b, n):
		m1 = min(a)
		m2 = min(b)
		if a.index(m1) != b.index(m2):
			return m1 + m2
		else:
			a.remove(m1)
			b.remove(m2)
			l1 = min(a)
			l2 = min(b)
			ln1 = l1 + m2
			ln2 = l2 + m1
			return min(ln1, ln2)


class Solution:

	def minSum(self, a, b, n):
		min_sum = 99999
		a_min = a[0]
		a_idx = 0
		for i in range(1, n, 1):
			if a_min > a[i]:
				a_min = a[i]
				a_idx = i
		b_min = b[0]
		b_idx = 0
		for i in range(1, n, 1):
			if b_min > b[i]:
				b_min = b[i]
				b_idx = i
		for j in range(n):
			if j != a_idx:
				sum_ = a[a_idx] + b[j]
				min_sum = min(min_sum, sum_)
		for j in range(n):
			if j != b_idx:
				sum_ = a[j] + b[b_idx]
				min_sum = min(min_sum, sum_)
		return min_sum


class Solution:

	def minSum(self, a, b, n):
		p = sorted(a)
		q = sorted(b)
		p_index = 0
		q_index = 0
		while p_index < n or q_index < n:
			first = p[p_index]
			first_index = a.index(first)
			second = q[q_index]
			second_index = b.index(second)
			if first_index == second_index:
				if p[p_index + 1] + q[q_index] < p[p_index] + q[q_index + 1]:
					a[first_index] = float('-inf')
					p_index += 1
				else:
					b[second_index] = float('-inf')
					q_index += 1
			else:
				return p[p_index] + q[q_index]
		return -1


class Solution:

	def minSum(self, a, b, n):
		t = a.copy()
		y = b.copy()
		c = min(a)
		d = a.index(c)
		b.pop(d)
		e = min(b)
		f = c + e
		p = min(y)
		q = y.index(p)
		t.pop(q)
		r = min(t)
		s = p + r
		if f < s:
			return f
		else:
			return s


class Solution:

	def minSum(self, a, b, n):
		min_a = min(a)
		min_b = min(b)
		if a.index(min_a) != b.index(min_b):
			return min_a + min_b
		else:
			y = min_b
			x = min_a
			b.remove(min_b)
			min_b = min(b)
			a.remove(min_a)
			min_a = min(a)
			return min(x + min_b, min_a + y)


import sys

class Solution:

	def minSum(self, a, b, n):
		minA = a[0]
		indexA = 0
		for i in range(1, n):
			if a[i] < minA:
				minA = a[i]
				indexA = i
		minB = b[0]
		indexB = 0
		for i in range(1, n):
			if b[i] < minB:
				minB = b[i]
				indexB = i
		if indexA != indexB:
			return minA + minB
		minA2 = sys.maxsize
		indexA2 = 0
		for i in range(n):
			if i != indexA and a[i] < minA2:
				minA2 = a[i]
				indexA2 = i
		minB2 = sys.maxsize
		indexB2 = 0
		for i in range(n):
			if i != indexB and b[i] < minB2:
				minB2 = b[i]
				indexB2 = i
		return min(minB + minA2, minA + minB2)


class Solution:

	def minSum(self, a, b, n):
		m1 = min(a)
		m2 = min(b)
		if a.index(m1) != b.index(m2):
			return m1 + m2
		else:
			a.remove(m1)
			b.remove(m2)
			m11 = min(a)
			m22 = min(b)
			m1 = m1 + m22
			m2 = m2 + m11
			return min(m1, m2)


class Solution:

	def minSum(self, a, b, n):
		minn1 = min(a)
		minn2 = min(b)
		if a.index(minn1) != b.index(minn2):
			return minn1 + minn2
		else:
			a.remove(minn1)
			b.remove(minn2)
			new_min1 = min(a)
			new_min2 = min(b)
			minn1 = minn1 + new_min2
			minn2 = minn2 + new_min1
			return min(minn1, minn2)


class Solution:

	def minSum(self, a, b, n):
		res = 0
		m1 = min(a)
		m2 = min(b)
		if a.index(m1) != b.index(m2):
			return m1 + m2
		else:
			a.remove(m1)
			b.remove(m2)
			m11 = min(a)
			m21 = min(b)
			m1 = m1 + m21
			m2 = m2 + m11
			return min(m1, m2)


class Solution:

	def minSum(self, a, b, n):
		m1 = min(a)
		m2 = min(b)
		i1 = a.index(m1)
		i2 = b.index(m2)
		if i1 != i2:
			return m1 + m2
		else:
			a.remove(m1)
			b.remove(m2)
			m3 = min(a)
			m4 = min(b)
			return min(m1 + m4, m2 + m3)


class Solution:

	def minSum(self, a, b, n):
		a1 = min(a)
		b1 = min(b)
		if a.index(a1) != b.index(b1):
			return a1 + b1
		else:
			a.remove(a1)
			b.remove(b1)
			a11 = min(a)
			b11 = min(b)
			a1 = a1 + b11
			b1 = b1 + a11
			return min(a1, b1)


class Solution:

	def minSum(self, a, b, n):
		a_min = min(a)
		b_min = min(b)
		if a.index(a_min) != b.index(b_min):
			sum = a_min + b_min
			return sum
		else:
			a.remove(a_min)
			b.remove(b_min)
			a_min2 = min(a)
			b_min2 = min(b)
			m1 = a_min + b_min2
			m2 = a_min2 + b_min
			return min(m1, m2)


class Solution:

	def minSum(self, a, b, n):
		m1 = min(a)
		m2 = min(b)
		if a.index(m1) != b.index(m2):
			sum = m1 + m2
			return sum
		else:
			if len(a) != 0:
				mini = a[0] + b[1]
			for i in range(len(a)):
				for j in range(len(b)):
					if i != j:
						sum = a[i] + b[j]
						if sum < mini:
							mini = sum
			return mini


class Solution:

	def minSum(self, a, b, n):
		k = a.index(min(a))
		j = b.index(min(b))
		if k != j:
			return min(a) + min(b)
		else:
			a.sort()
			b.sort()
			return min(a[0] + b[1], a[1] + b[0])


class Solution:

	def minSum(self, a: list, b: list, n: int) -> int:
		x1 = self.findMinIndex(a)
		y1 = self.findMinIndex(b)
		if x1 != y1:
			return a[x1] + b[y1]
		else:
			x2 = self.findMinIndex(a, x1)
			y2 = self.findMinIndex(b, y1)
			result1 = a[x1] + b[y2]
			result2 = a[x2] + b[y1]
			return result1 if result1 < result2 else result2

	@staticmethod
	def findMinIndex(arr: list, first_min=None) -> int:
		(min_index, min_value) = (None, float('inf'))
		for (index, value) in enumerate(arr):
			if index == first_min:
				continue
			elif value < min_value:
				(min_index, min_value) = (index, value)
		return min_index


class Solution:

	def minSum(self, a, b, n):
		min1 = min(a)
		min2 = min(b)
		if a.index(min1) != b.index(min2):
			return min1 + min2
		else:
			a.remove(min1)
			b.remove(min2)
			m11 = min(a)
			m22 = min(b)
			min1 = min1 + m22
			min2 = min2 + m11
			return min(min1, min2)


class Solution:

	def minSum(self, a, b, n):
		a1 = a.copy()
		b1 = b.copy()
		x = min(a)
		y = min(b)
		z = []
		if a.index(x) != b.index(y):
			return x + y
		elif x <= y:
			while True:
				y = min(b)
				b[b.index(y)] = 99999
				y = min(b)
				if a.index(x) != b.index(y):
					z.append(x + y)
					break
		elif x > y:
			while True:
				x = min(a)
				a[a.index(x)] = 99999
				x = min(a)
				if a.index(x) != b.index(y):
					z.append(x + y)
					break
		a = a1
		b = b1
		x = min(a1)
		y = min(b1)
		if y >= x:
			while True:
				x = min(a)
				a[a.index(x)] = 99999
				x = min(a)
				if a.index(x) != b.index(y):
					z.append(x + y)
					break
		return min(z)


class Solution:

	def minSum(self, a, b, n):
		x = min(a)
		y = min(b)
		if a.index(x) != b.index(y):
			return x + y
		else:
			a.remove(x)
			b.remove(y)
			e = min(a)
			f = min(b)
			x = x + f
			y = y + e
			return min(x, y)


class Solution:

	def minSum(self, a, b, n):
		(x, y, x1, y1, s1, s2) = (0, 0, 0, 0, 0, 0)
		x = min(a)
		y = min(b)
		if a.index(x) == b.index(y):
			a.remove(x)
			b.remove(y)
			x1 = min(a)
			y1 = min(b)
			s1 = x + y1
			s2 = x1 + y
			return min(s1, s2)
		else:
			return x + y


class Solution:

	def minSum(self, a, b, n):
		l = min(a)
		h = min(b)
		if a.index(l) != b.index(h):
			return l + h
		else:
			a.remove(l)
			b.remove(h)
			l1 = min(a)
			h1 = min(b)
			l = l + h1
			h = h + l1
			return min(l, h)


class Solution:

	def minSum(self, a, b, n):
		di1 = {}
		di2 = {}
		for i in range(n):
			di1[a[i]] = i
			di2[b[i]] = i
		di1 = sorted(di1.items(), key=lambda x: x[0])
		di2 = sorted(di2.items(), key=lambda x: x[0])
		i = 0
		j = 0
		if di1[0][1] == di2[0][1]:
			min1 = di1[1][0]
			min2 = di2[1][0]
			return min(di1[0][0] + min2, min1 + di2[0][0])
		return di1[0][0] + di2[0][0]


class Solution:

	def minSum(self, a, b, n):
		if a.index(min(a)) != b.index(min(b)):
			return min(a) + min(b)
		else:
			a.sort()
			b.sort()
			return min(min(a) + b[1], min(b) + a[1])


class Solution:

	def minSum(self, a, b, n):
		minA = a[0]
		indexA = 0
		n = len(a)
		for i in range(n):
			if minA > a[i]:
				minA = a[i]
				indexA = i
		minB = b[0]
		indexB = 0
		m = len(b)
		for j in range(m):
			if minB > b[j]:
				minB = b[j]
				indexB = j
		minA1 = 10 ** 7
		indexA1 = 0
		for i in range(n):
			if i != indexA and minA1 > a[i]:
				minA1 = a[i]
				indexA1 = i
		sumA = minA1 + minB
		minB1 = 10 ** 7
		indexB1 = 0
		for j in range(m):
			if j != indexB and minB1 > b[j]:
				minB1 = b[j]
				indexB1 = j
		sumB = minB1 + minA
		if indexA != indexB:
			minSum = minA + minB
			return minSum
		elif sumA < sumB:
			return sumA
		else:
			return sumB


class Solution:

	def minSum(self, a, b, n):
		min_a = a[0]
		min_b = b[0]
		index_a = 0
		index_b = 0
		for i in range(1, n):
			if a[i] < min_a:
				min_a = a[i]
				index_a = i
		for i in range(1, n):
			if b[i] < min_b:
				min_b = b[i]
				index_b = i
		if index_a != index_b:
			return min_a + min_b
		min_a2 = 112345678
		for i in range(n):
			if i != index_a and a[i] < min_a2:
				min_a2 = a[i]
				index_a2 = i
		min_b2 = 112345678
		for i in range(n):
			if i != index_b and b[i] < min_b2:
				min_b2 = b[i]
				index_a2 = i
		return min(min_b + min_a2, min_a + min_b2)


class Solution:

	def minSum(self, a, b, n):
		ma = min(a)
		mai = a.index(ma)
		mb = min(b)
		mbi = b.index(mb)
		if mbi != mai:
			return ma + mb
		else:
			a.sort()
			b.sort()
			return min(a[0] + b[1], b[0] + a[1])


class Solution:

	def mini(self, a, n):
		m = 999999999999
		b = []
		for i in range(n):
			if a[i] < m:
				m = a[i]
				g = i
		b.append([m, g])
		m = 999999999999
		for i in range(n):
			if a[i] < m and a[i] >= b[0][0] and (i != b[0][1]):
				m = a[i]
				g = i
		b.append([m, g])
		return b

	def minSum(self, a, b, n):
		e = self.mini(a, n)
		f = self.mini(b, n)
		if e[0][1] != f[0][1]:
			return e[0][0] + f[0][0]
		return min(e[0][0] + f[1][0], f[0][0] + e[1][0])

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		b = [int(x) for x in input().strip().split()]
		ob = Solution()
		print(ob.minSum(a, b, n))
		T -= 1
if __name__ == '__main__':
	main()


class Solution:

	def minSum(self, a, b, n):
		p = min(a)
		q = min(b)
		mini = p + q
		if a.index(p) == b.index(q):
			a.remove(p)
			b.remove(q)
			r = min(a)
			s = min(b)
			if p + s < q + r:
				mini = p + s
			else:
				mini = q + r
		return mini

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		b = [int(x) for x in input().strip().split()]
		ob = Solution()
		print(ob.minSum(a, b, n))
		T -= 1
if __name__ == '__main__':
	main()


class Solution:

	def minSum(self, a, b, n):
		newA = []
		newB = []
		for i in a:
			newA.append(i)
		for i in b:
			newB.append(i)
		x = min(a)
		z = a.index(x)
		b.pop(z)
		y = min(b)
		p = min(newB)
		r = newB.index(p)
		newA.pop(r)
		q = min(newA)
		return min(x + y, p + q)

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		b = [int(x) for x in input().strip().split()]
		ob = Solution()
		print(ob.minSum(a, b, n))
		T -= 1
if __name__ == '__main__':
	main()


class Solution:

	def findInd(self, ele, arr):
		for i in range(len(arr)):
			if arr[i] == ele:
				return i

	def minSum(self, a, b, n):
		(m1, m2) = (min(a), min(b))
		if self.findInd(m1, a) != self.findInd(m2, b):
			return m1 + m2
		else:
			a.remove(m1)
			b.remove(m2)
			(m11, m22) = (min(a), min(b))
			return min(m1 + m22, m2 + m11)

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		b = [int(x) for x in input().strip().split()]
		ob = Solution()
		print(ob.minSum(a, b, n))
		T -= 1
if __name__ == '__main__':
	main()


class Solution:

	def minSum(self, a, b, n):
		m1 = min(a)
		m2 = min(b)
		if a.index(m1) != b.index(m2):
			return m1 + m2
		else:
			a.remove(m1)
			b.remove(m2)
			m11 = min(a)
			m22 = min(b)
			m1 = m1 + m22
			m2 = m2 + m11
			return min(m1, m2)

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		b = [int(x) for x in input().strip().split()]
		ob = Solution()
		print(ob.minSum(a, b, n))
		T -= 1
if __name__ == '__main__':
	main()


class Solution:

	def minSum(self, a, b, n):
		first = float('inf')
		second = float('inf')
		x = float('inf')
		y = float('inf')
		ind1 = 0
		ind2 = 0
		for i in range(n):
			if a[i] < first:
				second = first
				first = a[i]
				ind1 = i
			elif a[i] < second:
				second = a[i]
			if b[i] < x:
				y = x
				x = b[i]
				ind2 = i
			elif b[i] < y:
				y = b[i]
		if ind1 != ind2:
			return first + x
		else:
			return min(first + y, second + x)


class Solution:

	def minSum(self, a, b, n):
		min1 = a[0]
		ind = 0
		for i in range(n):
			if a[i] < min1:
				min1 = a[i]
				ind = i
		min2 = b[0]
		ind2 = 0
		for j in range(n):
			if b[j] < min2:
				min2 = b[j]
				ind2 = j
		a.sort()
		b.sort()
		if ind != ind2:
			return min1 + min2
		else:
			return min(min1 + b[1], a[1] + min2)


class Solution:

	def minSum(self, a, b, n):
		minA = min(a)
		minB = min(b)
		if a.index(minA) != b.index(minB):
			return minA + minB
		else:
			i1 = a.index(minA)
			i2 = b.index(minB)
			min_of_a_before = a.pop(i1)
			min_of_b_before = b.pop(i2)
			minA2 = min(a)
			minB2 = min(b)
			return min(minA + minB2, minB + minA2)


class Solution:

	def minSum(self, a, b, n):
		mina = a[0]
		ida = 0
		for i in range(1, len(a)):
			if a[i] < mina:
				mina = a[i]
				ida = i
		minb = b[0]
		idb = 0
		for j in range(1, len(b)):
			if b[j] < minb:
				minb = b[j]
				idb = j
		if ida != idb:
			return mina + minb
		mina2 = float('inf')
		ida2 = 0
		for i in range(len(a)):
			if i != ida and a[i] < mina2:
				mina2 = a[i]
				ida2 = i
		minb2 = float('inf')
		idb2 = 0
		for j in range(len(b)):
			if j != idb and b[j] < minb2:
				minb2 = b[j]
				idb2 = j
		return min(mina + minb2, mina2 + minb)


class Solution:

	def minSum(self, a, b, n):
		minA = a[0]
		indexA = 0
		for i in range(1, n):
			if a[i] < minA:
				minA = a[i]
				indexA = i
		minB = b[0]
		indexB = 0
		for i in range(1, n):
			if b[i] < minB:
				minB = b[i]
				indexB = i
		if indexA != indexB:
			return minA + minB
		minA2 = 10 ** 9
		indexA2 = 0
		for i in range(n):
			if i != indexA and a[i] < minA2:
				minA2 = a[i]
				indexA2 = i
		minB2 = 10 ** 9
		indexB2 = 0
		for i in range(n):
			if i != indexB and b[i] < minB2:
				minB2 = b[i]
				indexB2 = i
		return min(minB + minA2, minA + minB2)


class Solution:

	def minSum(self, a, b, n):
		ma = 10 ** 5
		mb = 10 ** 5
		nma = 0
		nmb = 0
		ai = 0
		bi = 0
		for i in range(0, n):
			if a[i] < ma:
				ma = a[i]
				ai = i
		for i in range(0, n):
			if b[i] < mb:
				mb = b[i]
				bi = i
		if ai == bi:
			a.pop(ai)
			b.pop(bi)
			nma = min(a)
			nmb = min(b)
			x = min(nma + mb, nmb + ma)
			return x
		res = ma + mb
		return res


class Solution:

	def minSum(self, a, b, n):
		new_a = a.copy()
		new_a.sort()
		a_least = new_a[0]
		a_secondleast = new_a[1]
		new_b = b.copy()
		new_b.sort()
		b_least = new_b[0]
		b_secondleast = new_b[1]
		a_index = a.index(a_least)
		b_index = b.index(b_least)
		if a_index != b_index:
			min_sum = a_least + b_least
			return min_sum
		else:
			sum1 = a_least + b_secondleast
			sum2 = a_secondleast + b_least
			if sum1 < sum2:
				return sum1
			return sum2