------
## 最终结论
```python
def rearrange_array(t, test_cases):
    results = []
    
    for case in test_cases:
        n, a, l = case
        unlocked_values = [a[i] for i in range(n) if l[i] == 0]
        unlocked_values.sort(reverse=True)  # Sort in descending order to maximize prefix sums
        
        result = []
        unlock_index = 0
        for i in range(n):
            if l[i] == 1:
                result.append(a[i])
            else:
                result.append(unlocked_values[unlock_index])
                unlock_index += 1
        
        results.append(result)
    
    return results

# Reading input and outputting the result
t = int(input().strip())
test_cases = []

for _ in range(t):
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    l = list(map(int, input().strip().split()))
    test_cases.append((n, a, l))

results = rearrange_array(t, test_cases)

for res in results:
    print(' '.join(map(str, res)))
```