← Home
package main

import (
	"bufio"
	"os"
	"strconv"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buf := make([]byte, 1024*1024)
	scanner.Buffer(buf, 1024*1024)

	if !scanner.Scan() {
		return
	}
	t, _ := strconv.Atoi(scanner.Text())

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	for i := 0; i < t; i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		s := scanner.Text()
		scanner.Scan()
		r := scanner.Text()

		c0, c1 := 0, 0
		for j := 0; j < n; j++ {
			if s[j] == '0' {
				c0++
			} else {
				c1++
			}
		}

		possible := true
		for j := 0; j < n-1; j++ {
			if c0 == 0 || c1 == 0 {
				possible = false
				break
			}
			if r[j] == '0' {
				c1--
			} else {
				c0--
			}
		}

		if possible {
			out.WriteString("YES\n")
		} else {
			out.WriteString("NO\n")
		}
	}
}