Help Upward Path Starters 220 (Rated)

My submission - CodeChef: Practical coding for everyone
Problem Link - UPPATH

My approach -

  1. Move left on the upper row r1 till you can
  2. At some point you will have to move to the 2nd row when max of current column you are on is less than minimum of next column, at which point you move to r2
  3. from r2 we just see if we can move all the way to right, while ensuring we are choosing the least possible minimum value to move to
# cook your dish here
t = int(input())

for _ in range(t) : 
    n = int(input())
    r1 = [int(i) for i in input().split(' ')]
    r2 = [int(i) for i in input().split(' ')]
    
    # till in 1st row
    i = 0
    while i < n-1:
        minVal = min(r1[i], r2[i])
        maxVal = max(r1[i], r2[i])
        nextmin = max(r1[i+1], r2[i+1])
        nextmax = max(r1[i+1], r2[i+1])
        if maxVal <= nextmin:
            break
        else : 
            i += 1
    
    # if move down on last col
    if i == n-1:
        if r1[n-1] <= r2[n-1]:
            print("Yes")
        else :
            print("No")
        continue
    
    # if moved down now on bottom 
    curr = max(r1[i], r2[i])
    i += 1
    
    unR = False
    
    while i < n:
        minVal = min(r1[i], r2[i])
        maxVal = max(r1[i], r2[i])
        if curr > maxVal:
            unR = True
            break
        elif curr > minVal:
            curr = maxVal
        else :
            curr = minVal
        i += 1
        
        
    if unR :
        print("No")
    else :
        print("Yes")

This code is failing on some hidden test cases, please help if this logic works or not