#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        string a ,b;
        cin>>a>>b;
        ll one=0;
        ll zero=0;
        for(ll i=0;i<n;i++){
            if(a[i]=='1'){
                one+=1;
            }
            else{
                zero+=1;
            }
        }
        int  man=0;
        int flag=0;
        for(ll i=n-1;i>=0;i--){
            if(a[i]!=b[i]&&man==0){
                if(one!=zero){
                    flag=1;
                    break;
                }
                man=1;
                  if(a[i]=='1'){
                    one-=1;
                }
                if(a[i]=='0'){
                    zero-=1;
                }
                continue;
            }
            if(a[i]==b[i]&&man==1){
                if(one==zero){
                    man=0;
                }
                if(one!=zero){
                    flag=1;
                    break;
                }
                if(a[i]=='1'){
                    one-=1;
                }
                if(a[i]=='0'){
                    zero-=1;
                }
                continue;
            }
            if(a[i]=='1'){
                one-=1;
            }
            if(a[i]=='0'){
                zero-=1;
            }
        }
        if(flag==0){
            cout<<"YES"<<"\n";
        }
        if(flag==1){
            cout<<"NO"<<"\n";
        }
    }
}