import sys
import heapq
def solve():
"""
Solves a single test case for the maximum profit problem.
"""
try:
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
except (IOError, ValueError):
return
# A min-priority queue to store the costs of items bought but not yet sold
held_items_costs = []
total_profit = 0
for i in range(n):
# On day i, the option to buy at price a[i] becomes available.
# An item bought today can be sold today.
heapq.heappush(held_items_costs, a[i])
# See if we can make profitable sales with today's selling price b[i].
# We can sell multiple items.
while held_items_costs and b[i] > held_items_costs[0]:
# It's profitable to sell the cheapest item we hold.
cheapest_cost = heapq.heappop(held_items_costs)
total_profit += b[i] - cheapest_cost
print(total_profit)
def main():
"""
Main function to handle multiple test cases.
"""
try:
num_test_cases = int(sys.stdin.readline())
for _ in range(num_test_cases):
solve()
except (IOError, ValueError):
pass
if __name__ == "__main__":
main()