// Seperate the string to groups, for each group,
// there will be a chance to replace a S to G right after that group
package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, line, groups := 0, "", [][2]int{}
	fmt.Fscanf(reader, "%d\n%s\n", &n, &line)
	l, r := -1, -1
	// Break the input into G groups, each group stands by [lIndex, RIndex]
	for i := 0; i < len(line); i++ {
		if line[i:i+1] == "G" {
			if l != -1 {
				r++
			} else {
				l, r = i, i
			}
			if i == (len(line) - 1) {
				groups = append(groups, [2]int{l, r})
			}
			continue
		}
		if l != -1 {
			groups = append(groups, [2]int{l, r})
		}
		l, r = -1, -1
	}

	max := 0
	for i := 0; i < len(groups); i++ {
		res := groups[i][1] - groups[i][0] + 1
		if i == (len(groups) - 1) {
			if i > 0 {
				res++
			}
		} else {
			res++
			if (groups[i+1][0] - groups[i][1]) == 2 {
				res += groups[i+1][1] - groups[i+1][0] + 1
				if (i == (len(groups) - 2)) && (i == 0) {
					res--
				}
			}
		}

		if max < res {
			max = res
		}
	}

	fmt.Println(max)
}

package main

import (
  "fmt"
)

func max(x, y int) int {
  if x > y {
    return x
  }
  return y
}

func main() {
  var n int;
  var s string;
  fmt.Scanf("%d\n%s", &n, &s);

  lhs := make([]int, n)
  rhs := make([]int, n)

  var c, tot, sol int = 0, 0, 0

  for i, ch := range(s) {
    lhs[i] = c
    if ch == 'G' {
      c += 1
      tot += 1
    } else {
      c = 0
    }
    sol = max(sol, c)
  }

  c = 0
  for i := len(s)-1; i >= 0; i -=1 {
    rhs[i] = c
    if s[i] == 'G' {
      c += 1
    } else {
      c = 0
    }
    sol = max(sol, c)
  }

  for i, ch := range(s) {
    if ch == 'S' {
      tmp := lhs[i] + rhs[i]
      if (tmp < tot) {
        sol = max(sol, tmp + 1)
      } else {
        sol = max(sol, tmp)
      }
    }
  }

  fmt.Println(sol)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var str string
	fmt.Scan(&n, &str)
	cnt := 0
	cntG := 0
	arrf := make([]int, 0)
	for i := 0; i < n; i++ {
		if str[i] == 'G' {
			cnt++
			cntG++
			arrf = append(arrf, cnt)
		} else {
			arrf = append(arrf, cnt)
			cnt = 0
		}
	}
	cnt = 0
	arrb := make([]int, 0)
	for i := n - 1; i >= 0; i-- {
		if str[i] == 'G' {
			cnt++
			arrb = append(arrb, cnt)
		} else {
			arrb = append(arrb, cnt)
			cnt = 0
		}
	}
	//fmt.Println(arrf)
	//fmt.Println(arrb)
	ans := 0
	for i := 0; i < n; i++ {
		if ans < arrb[n - i - 1] {
			ans = arrb[n - i - 1]
		}
		if ans < arrf[i] {
			ans = arrf[i]
		}
		if str[i] == 'S' {
			curSum := arrb[n - i - 1] + arrf[i] + 1
			if curSum <= cntG && ans < curSum {
				ans = curSum
			}
			if curSum == cntG + 1 && ans < curSum {
				ans = curSum - 1
			}
		}
	}
	fmt.Println(ans)
}

package main

import "fmt"

func maxInt(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func calc(x, y, total int) int {
	if x+y == total {
		return total
	}
	return x + y + 1
}

func gao(segments []int, totalGolds int) int {
	if len(segments) <= 1 {
		return totalGolds
	}
	ans := 0
	for i := 1; i < len(segments); i++ {
		ans = maxInt(ans, calc(segments[i-1], segments[i], totalGolds))
	}
	return ans
}

func main() {
	var (
		n        int
		trophies []byte
	)
	_, _ = fmt.Scanln(&n)
	_, _ = fmt.Scanln(&trophies)
	nGolds, totalGolds := 0, 0
	var segments []int
	for _, ch := range trophies {
		if ch == 'G' {
			nGolds++
			totalGolds++
			continue
		}
		segments = append(segments, nGolds)
		nGolds = 0
	}
	if nGolds > 0 {
		segments = append(segments, nGolds)
	}
	fmt.Printf("%v\n", gao(segments, totalGolds))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func main() {
	defer writer.Flush()
	var n, mx, tot int
	scanf("%d\n", &n)
	lft := make([]int, n)
	rt := make([]int, n)
	var s string
	scanf("%s\n", &s)
	for i := 0; i < n; i++ {
		if i == 0 {
			if s[i] == 'G' {
				lft[i] = 1
				tot++
				mx = max(mx, lft[i])
			} else {
				lft[i] = 0
			}
		} else {
			if s[i] == 'G' {
				lft[i] = 1 + lft[i-1]
				mx = max(mx, lft[i])
				tot++
			} else {
				lft[i] = 0
			}
		}
	}
	for i := n - 1; i >= 0; i-- {
		if i == n-1 {
			if s[i] == 'G' {
				rt[i] = 1
				mx = max(mx, rt[i])
			} else {
				rt[i] = 0
			}
		} else {
			if s[i] == 'G' {
				rt[i] = 1 + rt[i+1]
				mx = max(mx, rt[i])
			} else {
				rt[i] = 0
			}
		}
	}
	//fmt.Println(tot)
	for i := 1; i < n-1; i++ {
		if s[i] == 'S' {
			cur := lft[i-1] + rt[i+1]
			if tot != cur {
				mx = max(cur+1, mx)
			} else {
				mx = max(mx, tot)
			}
		}
	}
	printf("%d\n", mx)
}

package main

import (
	"fmt"
	"bufio"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main(){
	defer writer.Flush()
	var n,l,r,ans,cnt,m,i int
	var a, b []int
	var s string
	scanf("%d\n", &n)
	scanf("%s\n", &s)
	// printf("read : %s\n", s)
	cnt = 0
	for i=0;i<n;i++ {
		if s[i] == 'G' {
			cnt++
		}
	}
	m=0
	l=0
	r=0
	for l < n {
		if l == r {
			r++
		} else if r < n && s[r] == 'G' {
			r++
		} else if s[l] != 'G' {
			l++
		} else {
			m++
			a = append(a, l)
			b = append(b, r)
			// printf("add %d %d\n", l, r)
			l = r
		}
	}
	ans = 0
	for i=0;i<m;i++ {
		ans = max(ans, b[i]-a[i])
		if m > 1 {
			ans = max(ans, b[i]-a[i]+1)
		}
	}
	for i=1;i<m;i++ {
		if a[i] - b[i-1] == 1 {
			ans = max(ans, b[i]-a[i] + b[i-1] - a[i-1])
			if m > 2 {
				ans = max(ans, b[i]-a[i] + b[i-1] - a[i-1] + 1)
			}
		}
	}
	printf("%d\n", ans)
}

package main

import (
	"fmt"
)

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

type cut struct {
	start, end int
}

func (p* cut) len() int {
	return p.end - p.start  + 1
}

func check(s []byte) int {
	if len(s) == 0 {
		return 0
	}
	var pcur *cut
	var cuts []*cut
	for i := 0; i < len(s); i++ {
		if s[i] == 71 {
			if pcur == nil {
				pcur = &cut{start:i}
			}
		} else {
			if pcur != nil {
				pcur.end = i - 1
				cuts = append(cuts, pcur)
				pcur = nil
			}
		}
	}
	if pcur != nil {
		pcur.end = len(s) - 1
		cuts = append(cuts, pcur)
	}

	return check_cuts(cuts)
}

func adj(a, b *cut) bool {
	return b.start - a.end == 2
}

func check_cuts(cuts []*cut) int {
	if len(cuts) == 0 {
		return 0
	}
	if len(cuts) == 1 {
		return cuts[0].len()
	}
	if len(cuts) == 2 {
		a, b := cuts[0], cuts[1]
		if adj(a, b) {
			return b.len() + a.len()
		} else {
			return max(b.len(), a.len()) + 1
		}
	}
	var prev *cut
	var selSize int
	for _, pc := range cuts {
		if prev == nil {
			selSize = pc.len() + 1
			prev = pc
			continue
		}

		curSize := pc.len() + 1
		if adj(prev, pc) {
			curSize += prev.len()
		}

		if curSize > selSize {
			selSize = curSize
		}

		prev = pc
	}
	return selSize
}

func main() {
	var n int
	fmt.Scanln(&n)
	s := make([]byte, n)
	fmt.Scanln(&s)
	fmt.Println(check(s))
}

package main

import (
	"fmt"
	"bufio"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main(){
	defer writer.Flush()
	var n,l,r,ans,cnt,m,i int
	var a, b []int
	var s string
	scanf("%d\n", &n)
	scanf("%s\n", &s)
	// printf("read : %s\n", s)
	cnt = 0
	for i=0;i<n;i++ {
		if s[i] == 'G' {
			cnt++
		}
	}
	m=0
	l=0
	r=0
	for l < n {
		if l == r {
			r++
		} else if r < n && s[r] == 'G' {
			r++
		} else if s[l] != 'G' {
			l++
		} else {
			m++
			a = append(a, l)
			b = append(b, r)
			// printf("add %d %d\n", l, r)
			l = r
		}
	}
	ans = 0
	for i=0;i<m;i++ {
		ans = max(ans, b[i]-a[i])
	}
	for i=1;i<m;i++ {
		if a[i] - b[i-1] == 1 {
			ans = max(ans, b[i]-a[i] + b[i-1] - a[i-1])
			if m > 2 {
				ans = max(ans, b[i]-a[i] + b[i-1] - a[i-1] + 1)
			}
		}
	}
	printf("%d\n", ans)
}

package main

import (
	"fmt"
)

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

type cut struct {
	start, end int
}

func (p* cut) len() int {
	return p.end - p.start  + 1
}

func check(s []byte) int {
	if len(s) == 0 {
		return 0
	}
	var pcur *cut
	var cuts []*cut
	for i := 0; i < len(s); i++ {
		if s[i] == 71 {
			if pcur == nil {
				pcur = &cut{start:i}
			}
		} else {
			if pcur != nil {
				pcur.end = i - 1
				cuts = append(cuts, pcur)
				pcur = nil
			}
		}
	}
	if pcur != nil {
		pcur.end = len(s) - 1
		cuts = append(cuts, pcur)
	}

	return check_cuts(cuts)
}

func adj(a, b *cut) bool {
	return b.start - a.end == 2
}

func check_cuts(cuts []*cut) int {
	if len(cuts) == 10 {
		return 0
	}
	if len(cuts) == 1 {
		return cuts[0].len()
	}
	if len(cuts) == 2 {
		a, b := cuts[0], cuts[1]
		if adj(a, b) {
			return b.len() + a.len()
		} else {
			return max(b.len(), a.len())
		}
	}
	var prev, selected *cut
	var sel_size int
	for _, pc := range cuts {
		if selected == nil {
			selected = pc
			sel_size = pc.len() + 1
			prev = pc
			continue
		}

		cur_size := pc.len()
		if adj(prev, pc) {
			cur_size += prev.len() + 1
		} else {
			cur_size += 1
		}

		if cur_size > sel_size {
			selected = pc
			sel_size = cur_size
		}

		prev = pc
	}
	return sel_size
}

func main() {
	var n int
	fmt.Scanln(&n)
	s := make([]byte, n)
	fmt.Scanln(&s)
	fmt.Println(check(s))
}

package main

import (
	"fmt"
)

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

type cut struct {
	start, end int
}

func (p* cut) len() int {
	return p.end - p.start  + 1
}

func check(s []byte) int {
	if len(s) == 0 {
		return 0
	}
	var pcur *cut
	var cuts []*cut
	for i := 0; i < len(s); i++ {
		if s[i] == 71 {
			if pcur == nil {
				pcur = &cut{start:i}
			}
		} else {
			if pcur != nil {
				pcur.end = i - 1
				cuts = append(cuts, pcur)
				pcur = nil
			}
		}
	}
	if pcur != nil {
		pcur.end = len(s) - 1
		cuts = append(cuts, pcur)
	}

	return check_cuts(cuts)
}

func adj(a, b *cut) bool {
	return b.start - a.end == 2
}

func check_cuts(cuts []*cut) int {
	if len(cuts) == 10 {
		return 0
	}
	if len(cuts) == 1 {
		return cuts[0].len()
	}
	if len(cuts) == 2 {
		a, b := cuts[0], cuts[1]
		if adj(a, b) {
			return b.len() + a.len()
		} else {
			return max(b.len(), a.len()) + 1
		}
	}
	var prev *cut
	var selSize int
	for _, pc := range cuts {
		if prev == nil {
			selSize = pc.len() + 1
			prev = pc
			continue
		}

		curSize := pc.len() + 1
		if adj(prev, pc) {
			curSize += prev.len()
		}

		if curSize > selSize {
			selSize = curSize
		}

		prev = pc
	}
	return selSize
}

func main() {
	var n int
	fmt.Scanln(&n)
	s := make([]byte, n)
	fmt.Scanln(&s)
	fmt.Println(check(s))
}

package main

import (
	"fmt"
)

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

type cut struct {
	start, end int
}

func (p* cut) len() int {
	return p.end - p.start  + 1
}

func check(s []byte) int {
	if len(s) == 0 {
		return 0
	}
	var pcur *cut
	var cuts []*cut
	for i := 0; i < len(s); i++ {
		if s[i] == 71 {
			if pcur == nil {
				pcur = &cut{start:i}
			}
		} else {
			if pcur != nil {
				pcur.end = i - 1
				cuts = append(cuts, pcur)
				pcur = nil
			}
		}
	}
	if pcur != nil {
		pcur.end = len(s) - 1
		cuts = append(cuts, pcur)
	}

	return check_cuts(cuts)
}

func adj(a, b *cut) bool {
	return b.start - a.end == 2
}

func check_cuts(cuts []*cut) int {
	if len(cuts) == 10 {
		return 0
	}
	if len(cuts) == 1 {
		return cuts[0].len()
	}
	if len(cuts) == 2 {
		a, b := cuts[0], cuts[1]
		if adj(a, b) {
			return b.len() + a.len()
		} else {
			return max(b.len(), a.len())
		}
	}
	var prev, selected *cut
	var sel_size int
	for _, pc := range cuts {
		if selected == nil {
			selected = pc
			sel_size = pc.len()
			prev = pc
			continue
		}

		cur_size := pc.len()
		if adj(prev, pc) {
			cur_size += prev.len() + 1
		}

		if cur_size > sel_size {
			selected = pc
			sel_size = cur_size
		}

		prev = pc
	}
	return sel_size
}

func main() {
	var n int
	fmt.Scanln(&n)
	s := make([]byte, n)
	fmt.Scanln(&s)
	fmt.Println(check(s))
}

package main

import (
  "fmt"
)

func max(x, y int) int {
  if x > y {
    return x
  }
  return y
}

func main() {
  var n int;
  var s string;
  fmt.Scanf("%d\n%s", &n, &s);

  lhs := make([]int, n)
  rhs := make([]int, n)

  var c, tot, sol int = 0, 0, 0

  for i, ch := range(s) {
    lhs[i] = c
    if ch == 'G' {
      c += 1
      tot += 1
    } else {
      c = 0
    }
    sol = max(sol, c)
  }

  c = 0
  for i := len(s)-1; i >= 0; i -=1 {
    rhs[i] = c
    if s[i] == 'G' {
      c += 1
    } else {
      c = 0
    }
    sol = max(sol, c)
  }

  for i, ch := range(s) {
    if ch == 'S' {
      tmp := lhs[i] + rhs[i]
      if (tmp < tot) {
        sol = max(sol, tmp + 1)
      }
    }
  }

  fmt.Println(sol)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var str string
	fmt.Scan(&n, &str)
	cnt := 0
	cntG := 0
	arrf := make([]int, 0)
	for i := 0; i < n; i++ {
		if str[i] == 'G' {
			cnt++
			cntG++
			arrf = append(arrf, cnt)
		} else {
			arrf = append(arrf, cnt)
			cnt = 0
		}
	}
	cnt = 0
	arrb := make([]int, 0)
	for i := n - 1; i >= 0; i-- {
		if str[i] == 'G' {
			cnt++
			arrb = append(arrb, cnt)
		} else {
			arrb = append(arrb, cnt)
			cnt = 0
		}
	}
	ans := 0
	for i := 0; i < n; i++ {
		if ans < arrb[n - i - 1] {
			ans = arrb[n - i - 1]
		}
		if ans < arrf[i] {
			ans = arrf[i]
		}
		if str[i] == 'S' {
			curSum := arrb[n - i - 1] + arrf[i] + 1
			if curSum <= cntG && ans < curSum {
				ans = curSum
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func main() {
	defer writer.Flush()
	var n, mx, tot int
	scanf("%d\n", &n)
	lft := make([]int, n)
	rt := make([]int, n)
	var s string
	scanf("%s\n", &s)
	for i := 0; i < n; i++ {
		if i == 0 {
			if s[i] == 'G' {
				lft[i] = 1
				tot++
				mx = max(mx, lft[i])
			} else {
				lft[i] = 0
			}
		} else {
			if s[i] == 'G' {
				lft[i] = 1 + lft[i-1]
				mx = max(mx, lft[i])
				tot++
			} else {
				lft[i] = 0
			}
		}
	}
	for i := n - 1; i >= 0; i-- {
		if i == n-1 {
			if s[i] == 'G' {
				rt[i] = 1
				mx = max(mx, rt[i])
			} else {
				rt[i] = 0
			}
		} else {
			if s[i] == 'G' {
				rt[i] = 1 + rt[i+1]
				mx = max(mx, rt[i])
			} else {
				rt[i] = 0
			}
		}
	}
	for i := 1; i < n-1; i++ {
		if s[i] == 'S' {
			cur := lft[i-1] + rt[i+1]
			if tot != cur && cur > 1 {
				mx = max(cur+1, mx)
			} else {
				mx = max(mx,tot)
			}
		}
	}
	printf("%d\n", mx)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func main() {
	defer writer.Flush()
	var n, mx, tot int
	scanf("%d\n", &n)
	lft := make([]int, n)
	rt := make([]int, n)
	var s string
	scanf("%s\n", &s)
	for i := 0; i < n; i++ {
		if i == 0 {
			if s[i] == 'G' {
				lft[i] = 1
				tot++
				mx = max(mx, lft[i])
			} else {
				lft[i] = 0
			}
		} else {
			if s[i] == 'G' {
				lft[i] = 1 + lft[i-1]
				mx = max(mx, lft[i])
				tot++
			} else {
				lft[i] = 0
			}
		}
	}
	for i := n - 1; i >= 0; i-- {
		if i == n-1 {
			if s[i] == 'G' {
				rt[i] = 1
				mx = max(mx, rt[i])
			} else {
				rt[i] = 0
			}
		} else {
			if s[i] == 'G' {
				rt[i] = 1 + rt[i+1]
				mx = max(mx, rt[i])
			} else {
				rt[i] = 0
			}
		}
	}
	for i := 1; i < n-1; i++ {
		if s[i] == 'S' {
			cur := lft[i-1] + rt[i+1]
			if tot != cur && cur > 1 {
				mx = max(cur+1, mx)
			} else {
				mx = tot
			}
		}
	}
	printf("%d\n", mx)
}

// Seperate the string to groups, for each group,
// there will be a chance to replace a S to G right after that group
package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, line, groups := 0, "", [][2]int{}
	fmt.Fscanf(reader, "%d\n%s\n", &n, &line)
	l, r := -1, -1
	// Break the input into G groups, each group stands by [lIndex, RIndex]
	for i := 0; i < len(line); i++ {
		if line[i:i+1] == "G" {
			if l != -1 {
				r++
			} else {
				l, r = i, i
			}
			if i == (len(line) - 1) {
				groups = append(groups, [2]int{l, r})
			}
			continue
		}
		if l != -1 {
			groups = append(groups, [2]int{l, r})
		}
		l, r = -1, -1
	}

	max := 0
	for i := 0; i < len(groups); i++ {
		res := groups[i][1] - groups[i][0] + 1
		if i == (len(groups) - 1) {
			if i > 0 {
				res++
			}
		} else {
			res++
			if (groups[i][1] - groups[i+1][0]) == 2 {
				res += groups[i+1][1] - groups[i+1][0] + 1
				if (i == (len(groups) - 2)) && (i == 0) {
					res--
				}
			}
		}

		if max < res {
			max = res
		}
	}

	fmt.Println(max)
}

// Seperate the string to groups, for each group,
// there will be a chance to replace a S to G right after that group
package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, line, groups := 0, "", [][2]int{}
	fmt.Fscanf(reader, "%d\n%s\n", &n, &line)
	l, r := -1, -1
	// Break the input into G groups, each group stands by [lIndex, RIndex]
	for i := 0; i < len(line); i++ {
		if line[i:i+1] == "G" {
			if l != -1 {
				r++
			} else {
				l, r = i, i
			}
			if i == (len(line) - 1) {
				groups = append(groups, [2]int{l, r})
			}
			continue
		}
		if l != -1 {
			groups = append(groups, [2]int{l, r})
		}
		l, r = -1, -1
	}

	max := 0
	for i := 0; i < len(groups); i++ {
		res := groups[i][1] - groups[i][0] + 1
		if i == (len(groups) - 1) {
			if i > 0 {
				res++
			}
		} else {
			res++
			if groups[i+1][0] == (groups[i][1] + 2) {
				extra := groups[i+1][1] - groups[i+1][0] + 1
				res += extra
				if i == (len(groups)-2) && (extra > 1) {
					res--
				}
			}
		}
		if max < res {
			max = res
		}
	}
	fmt.Println(max)
}

// Seperate the string to groups, for each group,
// there will be a chance to replace a S to G right after that group
package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, line, groups := 0, "", [][2]int{}
	fmt.Fscanf(reader, "%d\n%s\n", &n, &line)
	l, r := -1, -1
	// Break the input into G groups, each group stands by [lIndex, RIndex]
	for i := 0; i < len(line); i++ {
		if line[i:i+1] == "G" {
			if l != -1 {
				r++
			} else {
				l, r = i, i
			}
			if i == (len(line) - 1) {
				groups = append(groups, [2]int{l, r})
			}
			continue
		}
		if l != -1 {
			groups = append(groups, [2]int{l, r})
		}
		l, r = -1, -1
	}

	max := 0
	for i := 0; i < len(groups); i++ {
		res := groups[i][1] - groups[i][0] + 1
		if i == (len(groups) - 1) {
			if i > 1 {
				res++
			}
		} else {
			res++
			if groups[i+1][0] == (groups[i][1] + 2) {
				res += groups[i+1][1] - groups[i+1][0] + 1
			}
			if i == (len(groups) - 2) {
				res--
			}
		}
		if max < res {
			max = res
		}
	}
	fmt.Println(max)
}

// Seperate the string to groups, for each group,
// there will be a chance to replace a S to G right after that group
package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, line, groups := 0, "", [][2]int{}
	fmt.Fscanf(reader, "%d\n%s\n", &n, &line)
	l, r := -1, -1
	// Break the input into G groups, each group stands by [lIndex, RIndex]
	for i := 0; i < len(line); i++ {
		if line[i:i+1] == "G" {
			if l != -1 {
				r++
			} else {
				l, r = i, i
			}
			if i == (len(line) - 1) {
				groups = append(groups, [2]int{l, r})
			}
			continue
		}
		if l != -1 {
			groups = append(groups, [2]int{l, r})
		}
		l, r = -1, -1
	}

	max := 0
	for i := 0; i < len(groups); i++ {
		res := groups[i][1] - groups[i][0] + 1
		if i == (len(groups) - 1) {
			if i > 0 {
				res++
			}
		} else {
			res++
			if groups[i+1][0] == (groups[i][1] + 2) {
				res += groups[i+1][1] - groups[i+1][0] + 1
				if i == (len(groups) - 2) {
					res--
				}
			}
		}
		if max < res {
			max = res
		}
	}
	fmt.Println(max)
}

// Seperate the string to groups, for each group,
// there will be a chance to replace a S to G right after that group
package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, line, groups := 0, "", [][2]int{}
	fmt.Fscanf(reader, "%d\n%s\n", &n, &line)
	l, r := -1, -1
	// Break the input into G groups, each group stands by [lIndex, RIndex]
	for i := 0; i < len(line); i++ {
		if line[i:i+1] == "G" {
			if l != -1 {
				r++
			} else {
				l, r = i, i
			}
			if i == (len(line) - 1) {
				groups = append(groups, [2]int{l, r})
			}
			continue
		}
		if l != -1 {
			groups = append(groups, [2]int{l, r})
		}
		l, r = -1, -1
	}

	max := 0
	for i := 0; i < len(groups); i++ {
		res := groups[i][1] - groups[i][0] + 1
		if i == (len(groups) - 1) {
			if i > 1 {
				res++
			}
		} else {
			res++
			if groups[i+1][0] == (groups[i][1] + 2) {
				res += groups[i+1][1] - groups[i+1][0] + 1
				if i == (len(groups) - 2) {
					res--
				}
			}
		}
		if max < res {
			max = res
		}
	}
	fmt.Println(max)
}

// Seperate the string to groups, for each group,
// there will be a chance to replace a S to G right after that group
package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, line, groups := 0, "", [][2]int{}
	fmt.Fscanf(reader, "%d\n%s\n", &n, &line)
	l, r := -1, -1
	// Break the input into G groups, each group stands by [lIndex, RIndex]
	for i := 0; i < len(line); i++ {
		if line[i:i+1] == "G" {
			if l != -1 {
				r++
			} else {
				l, r = i, i
			}
			if i == (len(line) - 1) {
				groups = append(groups, [2]int{l, r})
			}
			continue
		}
		if l != -1 {
			groups = append(groups, [2]int{l, r})
		}
		l, r = -1, -1
	}

	max := 0
	for i := 0; i < len(groups); i++ {
		res := groups[i][1] - groups[i][0] + 1
		if i == (len(groups) - 1) {
			if i > 0 {
				res++
			}
		} else {
			res++
			if groups[i+1][0] == (groups[i][1] + 2) {
				extra := groups[i+1][1] - groups[i+1][0] + 1
				res += extra
				if i == (len(groups)-2) && (extra > 1) && (i == 0) {
					res--
				}
			}
		}
		if max < res {
			max = res
		}
	}
	fmt.Println(max)
}


package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func ia(m int) []int { return make([]int,m) }
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N,K := gi(),gi(); X,Y := gi(),gi(); X--; Y--; A := gis(K); for i:=0;i<K;i++ { A[i]-- }; U,V := fill2(N-1); for i:=0;i<N-1;i++ { U[i]--; V[i]-- }
		//Build the graph
		gr := make([][]int,N)
		for i:=0;i<N-1;i++ { u,v := U[i],V[i]; gr[u] = append(gr[u],v); gr[v] = append(gr[v],u) }
		//Mark the path down to the target
		needToVisit := make([]bool,N); for _,a := range A { needToVisit[a] = true }
		pathToY := make([]bool,N)
		var dfs1 func(n,p int) bool
		dfs1 = func(n,p int) bool {
			ans := false
			if n == Y { ans = true }
			for _,c := range gr[n] { if c != p && dfs1(c,n) { ans = true } }
			pathToY[n] = ans
			return ans
		}
		dfs1(X,-1)
		var dfs2 func(n,p int) bool
		dfs2 = func(n,p int) bool {
			ans := false
			if needToVisit[n] || pathToY[n] { ans = true }
			for _,c := range gr[n] { if c != p && dfs2(c,n) { ans = true } }
			needToVisit[n] = ans
			return ans
		}
		dfs2(X,-1)
		var dfs3 func(n,p int) int
		dfs3 = func(n,p int) int {
			ans := 0
			for _,c := range gr[n] {
				if c == p || !needToVisit[c] { continue }
				cans := dfs3(c,n)
				if pathToY[c] { ans += 1 + cans } else { ans += 2 + cans }
			}
			return ans
		}
		ans := dfs3(X,-1)
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

var wr = bufio.NewWriter(os.Stdout)
var rd = bufio.NewScanner(os.Stdin)

func nextInt() int {
	rd.Scan()
	if n, err := strconv.Atoi(rd.Text()); err == nil {
		return n
	}
	return 0
}
func Max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}
func Min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	defer wr.Flush()
	rd.Buffer(make([]byte, 0), 1000000009)
	rd.Split(bufio.ScanWords)
	T := nextInt()
	for Case := 0; Case < T; Case++ {
		solve()
	}
}

var N = 200010
var n, k, x, y int
var d1 = make([]int, N)
var d2 = make([]int, N)
var head = make([]int, N)
var nxt = make([]int, N*2)
var to = make([]int, N*2)
var vis = make([]bool, N)
var st = make([]bool, N)
var fa = make([]int, N)
var tot = -1

func add(a, b int) {
	tot++
	nxt[tot] = head[a]
	to[tot] = b
	head[a] = tot
}
func dfs(u, pre, dep int, dis []int, ok bool) {
	dis[u] = dep
	if ok {
		fa[u] = pre
	}
	for i := head[u]; i != -1; i = nxt[i] {
		if to[i] != pre {
			dfs(to[i], u, dep+1, dis, ok)
		}
	}
}
func solve() {
	n, k, x, y = nextInt(), nextInt(), nextInt(), nextInt()
	tot = -1
	for i := 1; i <= n; i++ {
		head[i] = -1
		vis[i] = false
		st[i] = false
	}
	for i := 1; i <= k; i++ {
		t := nextInt()
		vis[t] = true
	}
	for i := 1; i < n; i++ {
		a, b := nextInt(), nextInt()
		add(a, b)
		add(b, a)
	}
	dfs(x, -1, 0, d1, true)
	dfs(y, -1, 0, d2, false)
	res := 0
	st[x]=true
	for i := 1; i <= n; i++ {
		if !vis[i] || st[i]{
			continue
		}
		t := i
		st[t] = true
		res += 2
		for fa[t] != -1 && !st[fa[t]] {
			t = fa[t]
			st[t] = true
			res += 2
			// fmt.Fprint(wr, " ", t)
		}
		// fmt.Fprint(wr, " ", res, "X\n")
	}
	ans := 0x3f3f3f3f
	for i := 1; i <= n; i++ {
		if vis[i] {
			ans = Min(ans, d2[i]-d1[i])
		}
	}
	fmt.Fprint(wr, ans+res, "\n")
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)
func slove() {
	defer w.Flush()
	var n, k int
	fmt.Fscan(r, &n, &k)
	var x, y int
	fmt.Fscan(r, &x, &y)
	x, y = x-1, y-1
	pass := make(map[int]bool)
	for i:=0;i<k;i++ {
		var a int
		fmt.Fscan(r, &a)
		pass[a-1] = true
	}
	edge := make([][]int, n)
	for i:=0;i<n-1;i++ {
		var a, b int
		fmt.Fscan(r, &a, &b)
		edge[a-1] = append(edge[a-1], b-1)
		edge[b-1] = append(edge[b-1], a-1)
	}
	l := 0
	var dfs func(i, from, level int) int
	dfs = func(i, from, level int) int {
		if i == y {
			l = level
		}
		ans := 0
		for _, j := range edge[i] {
			if j == from {
				continue
			}
			a := dfs(j, i, level+1)
			if a != -1 {
				ans += 2 + a
			}
		}
		if _, ok := pass[i]; ok || i == y {
			return ans
		}
		if ans == 0 {
			return -1
		} else {
			return ans
		}
	}
	fmt.Fprintln(w, dfs(x, -1, 0)-l)
}

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main
import("bufio";."fmt";"os")

// github.com/EndlessCheng/codeforces-go
func main() {
	in:=bufio.NewReader(os.Stdin)
	var T,n,k,x,y,v,w int
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&n,&k,&x,&y)
		a:=make([]bool,n+1)
		a[y]=true
		for;k>0;k--{Fscan(in,&v);a[v]=true}
		g:=make([][]int,n+1)
		for i:=1;i<n;i++{
			Fscan(in,&v,&w)
			g[v]=append(g[v],w)
			g[w]=append(g[w],v)
		}
		ans:=n*2-2
		var f func(v,p,d int)bool
		f=func(v,p,d int)bool{
			if v==y{ans-=d}
			c:=a[v]
			for _,w:=range g[v]{
				if w!=p&&f(w,v,d+1){c=true}
			}
			if!c{ans-=2}
			return c
		}
		f(x,0,0)
		Println(ans)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1675F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, k, x, y, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &x, &y)
		need := make([]bool, n+1)
		for ; k > 0; k-- {
			Fscan(in, &v)
			need[v] = true
		}
		need[x] = true
		need[y] = true
		g := make([][]int, n+1)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		ans := n*2 - 2
		var f func(v, fa, d int) bool
		f = func(v, fa, d int) bool {
			if v == y {
				ans -= d
			}
			found := need[v]
			for _, w := range g[v] {
				if w != fa && f(w, v, d+1) {
					found = true
				}
			}
			if !found {
				ans -= 2
			}
			return found
		}
		f(x, 0, 0)
		Fprintln(out, ans)
	}
}

func main() { CF1675F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1675F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, k, x, y, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &x, &y)
		need := make([]bool, n+1)
		for ; k > 0; k-- {
			Fscan(in, &v)
			need[v] = true
		}
		need[x] = true
		need[y] = true
		deg := make([]int, n+1)
		g := make([][]int, n+1)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
			deg[v]++
			deg[w]++
		}

		ans := n*2 - 2
		del := make([]bool, n+1)
		q := []int{}
		for i, d := range deg {
			if d == 1 && !need[i] {
				q = append(q, i)
			}
		}
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			del[v] = true
			ans -= 2
			for _, w := range g[v] {
				if deg[w]--; deg[w] == 1 && !need[w] {
					q = append(q, w)
				}
			}
		}

		var f func(v, fa, d int) bool
		f = func(v, fa, d int) bool {
			if v == y {
				ans -= d
				return true
			}
			for _, w := range g[v] {
				if w != fa && !del[w] {
					if f(w, v, d+1) {
						return true
					}
				}
			}
			return false
		}
		f(x, 0, 0)
		Fprintln(out, ans)
	}
}

func main() { CF1675F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1675F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, k, x, y, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &x, &y)
		need := make([]bool, n+1)
		for ; k > 0; k-- {
			Fscan(in, &v)
			need[v] = true
		}
		need[x] = true
		need[y] = true
		g := make([][]int, n+1)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		ans := n*2 - 2
		var f func(v, fa, d int) bool
		f = func(v, fa, d int) bool {
			if v == y {
				ans -= d
				return true
			}
			found := need[v]
			for _, w := range g[v] {
				if w != fa && f(w, v, d+1) {
					found = true
				}
			}
			if !found {
				ans -= 2
			}
			return found
		}
		f(x, 0, 0)
		Fprintln(out, ans)
	}
}

func main() { CF1675F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n, k, x, y int
		fmt.Fscan(in, &n, &k, &x, &y)
		task := make(map[int]bool)
		for i := 0; i < k; i++ {
			ta := 0
			fmt.Fscan(in, &ta)
			if ta == x || ta == y {
				continue
			}
			task[ta] = true
		}
		path := make([][]int, n+1)
		for i := 0; i < n-1; i++ {
			a, b := 0, 0
			fmt.Fscan(in, &a, &b)
			path[a] = append(path[a], b)
			path[b] = append(path[b], a)
		}
		alone := make(map[int]bool)
		var findy func(i, last int) bool
		findy = func(i, last int) bool {
			if i == y {
				return true
			}
			for _, j := range path[i] {
				if j == last {
					continue
				}
				ok := findy(j, i)
				if i != x {
					alone[i] = true
				}
				if ok {
					delete(task, i)
					return true
				}
			}
			return false
		}
		findy(x, x)
		visited := make(map[int]bool)
		ans := 0
		//fmt.Println(alone, task)
		var dfs func(i int, length int)
		dfs = func(i int, length int) {
			visited[i] = true
			if i == y {
				ans += length
				length = 0
			}
			if task[i] {
				ans += length * 2
				length = 0
			}
			//fmt.Println(i, ans)
			for _, j := range path[i] {
				if visited[j] {
					continue
				}
				if !alone[j] && alone[i] && j != y {
					dfs(j, 1)
				} else {
					dfs(j, length+1)
				}
			}

		}
		dfs(x, 0)
		fmt.Fprintln(out, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n, k, x, y int
		fmt.Fscan(in, &n, &k, &x, &y)
		task := make(map[int]bool)
		for i := 0; i < k; i++ {
			ta := 0
			fmt.Fscan(in, &ta)
			if ta == x || ta == y {
				continue
			}
			task[ta] = true
		}
		path := make([][]int, n+1)
		for i := 0; i < n-1; i++ {
			a, b := 0, 0
			fmt.Fscan(in, &a, &b)
			path[a] = append(path[a], b)
			path[b] = append(path[b], a)
		}
		var findy func(i, last int) bool
		findy = func(i, last int) bool {
			if i == y {
				return true
			}
			for _, j := range path[i] {
				if j == last {
					continue
				}
				ok := findy(j, i)
				if ok {
					delete(task, i)
					return true
				}
			}
			return false
		}
		findy(x, x)
		visited := make(map[int]bool)
		ans := 0
		var dfs func(i int, length int)
		dfs = func(i int, length int) {
			visited[i] = true
			if i == y {
				ans += length
				length = 0
			}
			if task[i] {
				ans += length * 2
				length = 0
			}
			for _, j := range path[i] {
				if visited[j] {
					continue
				}
				dfs(j, length+1)
			}
		}
		dfs(x, 0)
		fmt.Fprintln(out, ans)
	}
}


package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"bytes"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	mood := make([]int, n)
	for i := 0;i < n - 1;i ++ {
		fmt.Fscanf(reader, "%d ", &mood[i])
	}
	fmt.Fscanf(reader, "%d\n", &mood[n - 1])
	for i := 1;2 * i < n;i ++ {
		if n % i != 0 {
			continue
		}
		for j := 0;j < i;j ++ {
			ok := 1
			for k := j;k < n; k += i {
				ok *= mood[k]
			}
			if ok == 1 {
				fmt.Printf("YES")
				return
			}
		}
	}
	fmt.Printf("NO")
	return
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  knights := make([]int, n)
  for i := 0; i < n; i++ {
    knights[i] = getI()
  }
  for a := 3; a <= n; a++ {
    if n%a == 0 {
      fmt.Fprintf(os.Stderr, "a = %d\n", a)
      b := n/a
      for i := 0; i < b; i++ {
        okay := true
        for j := i; j < n; j += b {
          if knights[j] == 0 {
            okay = false
            break
          }
        }
        if okay {
          fmt.Fprintf(os.Stderr, "a = %d, b = %d, i = %d\n", a, b, i)
          writer.WriteString("YES\n")
          return
        }
      }
    }
  }
  writer.WriteString("NO\n")
}


package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var in = bufio.NewScanner(os.Stdin)

func readInt() int64 {
	if !in.Scan() {
		panic(in.Err())
	}
	res := int64(0)
	for _, b := range in.Bytes() {
		res = res*10 + int64(b-'0')
	}
	return res
}

type byDiff [][2]int

func (s byDiff) Len() int           { return len(s) }
func (s byDiff) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s byDiff) Less(i, j int) bool { return s[i][0]-s[i][1] < s[j][0]-s[j][1] }

func main() {
	in.Split(bufio.ScanWords)
	n := int(readInt())
	x := readInt()
	y := readInt()
	edges := make([][]int, n)
	for i := 1; i < n; i++ {
		u := int(readInt() - 1)
		v := int(readInt() - 1)
		edges[u] = append(edges[u], v)
		edges[v] = append(edges[v], u)
	}
	if x >= y {
		for i := range edges {
			if len(edges[i]) == n-1 {
				fmt.Println(x + int64(n-2)*y)
				return
			}
		}
		fmt.Println(int64(n-1) * y)
		return
	}

	var drop func(v, p int) [2]int
	drop = func(v, p int) [2]int {
		var r [][2]int
		for _, u := range edges[v] {
			if u == p {
				continue
			}
			r = append(r, drop(u, v))
		}
		switch len(r) {
		case 0:
			return [2]int{0, 0}
		case 1:
			a := 1 + r[0][1]
			if a > r[0][0] {
				a = r[0][0]
			}
			return [2]int{a, a}
		}
		sort.Sort(byDiff(r))
		take := func(k int) int {
			t := len(r) - k
			for i := 0; i < k; i++ {
				t += r[i][0]
			}
			for i := k; i < len(r); i++ {
				t += r[i][1]
			}
			return t
		}
		a := take(0)
		b := take(1)
		c := take(2)
		if b > a {
			b = a
		}
		if c > b {
			c = b
		}
		return [2]int{b, c}
	}
	d := int64(drop(0, -1)[1])
	fmt.Println(d*y + (int64(n)-1-d)*x)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var edges [][]int

func main() {
	defer out.Flush()
	var n int
	var x, y int64
	fmt.Fscan(in, &n, &x, &y)
	edges = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	var xCount int
	if x >= y {
		if isStar() {
			xCount = 1
		} else {
			xCount = 0
		}
	} else {
		xCount, _ = solve(0, 0)
	}
	fmt.Fprintln(out, x*int64(xCount)+y*int64(n-1-xCount))
}

func isStar() bool {
	for _, vEdges := range edges {
		if len(vEdges) == len(edges)-1 {
			return true
		}
	}
	return false
}

func solve(v, parent int) (ans int, isAvail bool) {
	availChildren := 0
	for _, u := range edges[v] {
		if u == parent {
			continue
		}
		childAns, childIsAvail := solve(u, v)
		ans += childAns
		if childIsAvail {
			availChildren++
		}
	}
	switch availChildren {
	case 0:
		isAvail = true
	case 1:
		isAvail = true
		ans += 1
	default:
		isAvail = false
		ans += 2
	}
	return
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var edges [][]int

func main() {
	defer out.Flush()
	var n int
	var x, y int64
	fmt.Fscan(in, &n, &x, &y)
	edges = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	var xCount int
	if x >= y {
		if isStar() {
			xCount = 1
		} else {
			xCount = 0
		}
	} else {
		xCount, _ = solve(0, 0)
	}
	fmt.Fprintln(out, x*int64(xCount)+y*int64(n-1-xCount))
}

func isStar() bool {
	for _, vEdges := range edges {
		if len(vEdges) == len(edges)-1 {
			return true
		}
	}
	return false
}

func solve(v, parent int) (ans int, isAvail bool) {
	availChildren := 0
	for _, u := range edges[v] {
		if u == parent {
			continue
		}
		dAns, childIsAvail := solve(u, v)
		ans += dAns
		if childIsAvail {
			availChildren++
		}
	}
	isAvail = availChildren < 2
	switch availChildren {
	case 0:
		isAvail = true
	case 1:
		isAvail = true
		ans += 1
	default:
		isAvail = false
		ans += 2
	}
	return
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var in = bufio.NewScanner(os.Stdin)

func readInt() int64 {
	if !in.Scan() {
		panic(in.Err())
	}
	res := int64(0)
	for _, b := range in.Bytes() {
		res = res*10 + int64(b-'0')
	}
	return res
}

type byDiff [][2]int

func (s byDiff) Len() int           { return len(s) }
func (s byDiff) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s byDiff) Less(i, j int) bool { return s[i][0]-s[i][1] < s[j][0]-s[j][1] }

func main() {
	in.Split(bufio.ScanWords)
	n := int(readInt())
	x := readInt()
	y := readInt()
	edges := make([][]int, n)
	for i := 1; i < n; i++ {
		u := int(readInt() - 1)
		v := int(readInt() - 1)
		edges[u] = append(edges[u], v)
		edges[v] = append(edges[v], u)
	}
	if x >= y {
		for i := range edges {
			if len(edges[i]) == n-1 {
				fmt.Println(x + int64(n-2)*y)
				return
			}
		}
		fmt.Println(int64(n-1) * y)
		return
	}

	var drop func(v, p int) [2]int
	drop = func(v, p int) [2]int {
		var r [][2]int
		for _, u := range edges[v] {
			if u == p {
				continue
			}
			r = append(r, drop(u, v))
		}
		switch len(r) {
		case 0:
			return [2]int{0, 0}
		case 1:
			a := 1 + r[0][1]
			if a > r[0][0] {
				a = r[0][0]
			}
			return [2]int{a, a}
		}
		sort.Sort(byDiff(r))
		take := func(k int) int {
			t := len(r) - k
			for i := 0; i < k; i++ {
				t += r[i][0]
			}
			for i := k; i < len(r); i++ {
				t += r[i][1]
			}
			return t
		}
		a := take(0)
		b := take(1)
		c := take(2)
		if b > a {
			b = a
		}
		if c > b {
			c = b
		}
		return [2]int{b, c}
	}
	d := int64(drop(0, -1)[1])
	fmt.Println(d)
	fmt.Println(d*y + (int64(n)-1-d)*x)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var n int
var x, y int64
var edges [][]int
var xCount int
var yCount int
var childrenByHeight []ChildrenByHeight

func main() {
	defer out.Flush()
	fmt.Fscan(in, &n, &x, &y)
	edges = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	if x >= y {
		if isStar() {
			fmt.Fprintln(out, y*int64(n-2)+x)
		} else {
			fmt.Fprintln(out, y*int64(n-1))
		}
		return
	}
	childrenByHeight = make([]ChildrenByHeight, n)
	computeHeight(0, 0)
	solve2(0)
	fmt.Fprintln(out, x*int64(xCount)+y*int64(yCount))
}

func isStar() bool {
	for _, es := range edges {
		if len(es) == n-1 {
			return true
		}
	}
	return false
}

func computeHeight(v, parent int) int {
	for _, u := range edges[v] {
		if u == parent {
			continue
		}
		h := computeHeight(u, v)
		childrenByHeight[v] = append(childrenByHeight[v], ChildWithHeight{u, h})
	}
	sort.Sort(childrenByHeight[v])
	if len(childrenByHeight[v]) == 0 {
		return 1
	}
	return childrenByHeight[v][0].height + 1
}

func solve2(v int) {
	cbh := childrenByHeight[v]
	if len(cbh) < 2 {
		solve1(v)
		return
	}
	xCount += 2
	solve1(cbh[0].vertex)
	solve1(cbh[1].vertex)
	for _, child := range cbh[2:] {
		yCount++
		solve2(child.vertex)
	}
}

func solve1(v int) {
	cbh := childrenByHeight[v]
	if len(cbh) == 0 {
		return
	}
	xCount++
	solve1(cbh[0].vertex)
	for _, child := range cbh[1:] {
		yCount++
		solve2(child.vertex)
	}
}

type ChildrenByHeight []ChildWithHeight

type ChildWithHeight struct {
	vertex int
	height int
}

func (a ChildrenByHeight) Less(i, j int) bool {
	return a[i].height > a[j].height
}

func (a ChildrenByHeight) Len() int {
	return len(a)
}

func (a ChildrenByHeight) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var n int
var x, y int64
var edges [][]int
var xCount int
var yCount int
var childrenByHeight []ChildrenByHeight

func main() {
	defer out.Flush()
	fmt.Fscan(in, &n, &x, &y)
	if x >= y {
		if n == 2 {
			fmt.Fprintln(out, x)
		} else {
			fmt.Fprintln(out, y*int64(n-1))
		}
		return
	}
	edges = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	childrenByHeight = make([]ChildrenByHeight, n)
	computeHeight(0, 0)
	solve2(0)
	fmt.Fprintln(out, x*int64(xCount)+y*int64(yCount))
}

func computeHeight(v, parent int) int {
	for _, u := range edges[v] {
		if u == parent {
			continue
		}
		h := computeHeight(u, v)
		childrenByHeight[v] = append(childrenByHeight[v], ChildWithHeight{u, h})
	}
	sort.Sort(childrenByHeight[v])
	if len(childrenByHeight[v]) == 0 {
		return 1
	}
	return childrenByHeight[v][0].height + 1
}

func solve2(v int) {
	cbh := childrenByHeight[v]
	if len(cbh) < 2 {
		solve1(v)
		return
	}
	xCount += 2
	solve1(cbh[0].vertex)
	solve1(cbh[1].vertex)
	for _, child := range cbh[2:] {
		yCount++
		solve2(child.vertex)
	}
}

func solve1(v int) {
	cbh := childrenByHeight[v]
	if len(cbh) == 0 {
		return
	}
	xCount++
	solve1(cbh[0].vertex)
	for _, child := range cbh[1:] {
		yCount++
		solve2(child.vertex)
	}
}

type ChildrenByHeight []ChildWithHeight

type ChildWithHeight struct {
	vertex int
	height int
}

func (a ChildrenByHeight) Less(i, j int) bool {
	return a[i].height > a[j].height
}

func (a ChildrenByHeight) Len() int {
	return len(a)
}

func (a ChildrenByHeight) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var n int
var x, y int64
var edges [][]int
var xCount int
var yCount int
var childrenByHeight []ChildrenByHeight

func main() {
	defer out.Flush()
	fmt.Fscan(in, &n, &x, &y)
	edges = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	if x >= y {
		if isStar() {
			fmt.Fprintln(out, y*int64(n-2)+x)
		} else {
			fmt.Fprintln(out, y*int64(n-1))
		}
		return
	}
	solve1cache = make([]int, n)
	solve2cache = make([]int, n)
	for i := 0; i < n; i++ {
		solve1cache[i] = -1
		solve2cache[i] = -1
	}
	childrenByHeight = make([]ChildrenByHeight, n)
	computeHeight(0, 0)
	yCount = solve2(0)
	xCount = n - 1 - yCount
	fmt.Fprintln(out, x*int64(xCount)+y*int64(yCount))
}

func isStar() bool {
	for _, es := range edges {
		if len(es) == n-1 {
			return true
		}
	}
	return false
}

func computeHeight(v, parent int) int {
	for _, u := range edges[v] {
		if u == parent {
			continue
		}
		h := computeHeight(u, v)
		childrenByHeight[v] = append(childrenByHeight[v], ChildWithHeight{u, h})
	}
	sort.Sort(childrenByHeight[v])
	if len(childrenByHeight[v]) == 0 {
		return 1
	}
	return childrenByHeight[v][0].height + 1
}

var solve2cache []int
var solve1cache []int

func solve2(v int) int {
	if solve2cache[v] != -1 {
		return solve2cache[v]
	}
	cbh := childrenByHeight[v]
	if len(cbh) < 2 {
		return solve1(v)
	}
	children := make(ChildrenByHeight, 0)
	for _, child := range cbh {
		children = append(children, ChildWithHeight{child.vertex, solve2(child.vertex)})
	}
	sort.Sort(children)
	result := 0
	result += solve1(children[0].vertex)
	result += solve1(children[1].vertex)
	for _, child := range children[2:] {
		result += child.height
	}
	solve2cache[v] = result
	return result
}

func solve1(v int) int {
	if solve1cache[v] != -1 {
		return solve1cache[v]
	}
	cbh := childrenByHeight[v]
	if len(cbh) == 0 {
		return 0
	}
	children := make(ChildrenByHeight, 0)
	for _, child := range cbh {
		children = append(children, ChildWithHeight{child.vertex, solve2(child.vertex)})
	}
	sort.Sort(children)
	result := 0
	result += solve1(children[0].vertex)
	for _, child := range children[1:] {
		result += child.height
	}
	solve1cache[v] = result
	return result
}

type ChildrenByHeight []ChildWithHeight

type ChildWithHeight struct {
	vertex int
	height int
}

func (a ChildrenByHeight) Less(i, j int) bool {
	return a[i].height > a[j].height
}

func (a ChildrenByHeight) Len() int {
	return len(a)
}

func (a ChildrenByHeight) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var n int
var x, y int64
var edges [][]int
var xCount int
var yCount int
var childrenByHeight []ChildrenByHeight

func main() {
	defer out.Flush()
	fmt.Fscan(in, &n, &x, &y)
	edges = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
	}
	if x >= y {
		if isStar() {
			fmt.Fprintln(out, y*int64(n-2)+x)
		} else {
			fmt.Fprintln(out, y*int64(n-1))
		}
		return
	}
	solve1cache = make([]int, n)
	solve2cache = make([]int, n)
	for i := 0; i < n; i++ {
		solve1cache[i] = -1
		solve2cache[i] = -1
	}
	childrenByHeight = make([]ChildrenByHeight, n)
	computeHeight(0, 0)
	yCount = solve2(0)
	xCount = n - 1 - yCount
	fmt.Fprintln(out, x*int64(xCount)+y*int64(yCount))
}

func isStar() bool {
	for _, es := range edges {
		if len(es) == n-1 {
			return true
		}
	}
	return false
}

func computeHeight(v, parent int) int {
	for _, u := range edges[v] {
		if u == parent {
			continue
		}
		h := computeHeight(u, v)
		childrenByHeight[v] = append(childrenByHeight[v], ChildWithHeight{u, h})
	}
	sort.Sort(childrenByHeight[v])
	if len(childrenByHeight[v]) == 0 {
		return 1
	}
	return childrenByHeight[v][0].height + 1
}

var solve2cache []int
var solve1cache []int

func solve2(v int) int {
	if solve2cache[v] != -1 {
		return solve2cache[v]
	}
	cbh := childrenByHeight[v]
	if len(cbh) < 2 {
		return solve1(v)
	}
	children := make(ChildrenByHeight, 0)
	for _, child := range cbh {
		children = append(children, ChildWithHeight{child.vertex, solve2(child.vertex)})
	}
	sort.Sort(children)
	result := 0
	result += solve1(children[0].vertex)
	result += solve1(children[1].vertex)
	for _, child := range children[2:] {
		result += 1 + child.height
	}
	solve2cache[v] = result
	return result
}

func solve1(v int) int {
	if solve1cache[v] != -1 {
		return solve1cache[v]
	}
	cbh := childrenByHeight[v]
	if len(cbh) == 0 {
		return 0
	}
	children := make(ChildrenByHeight, 0)
	for _, child := range cbh {
		children = append(children, ChildWithHeight{child.vertex, solve2(child.vertex)})
	}
	sort.Sort(children)
	result := 0
	result += solve1(children[0].vertex)
	for _, child := range children[1:] {
		result += 1 + child.height
	}
	solve1cache[v] = result
	return result
}

type ChildrenByHeight []ChildWithHeight

type ChildWithHeight struct {
	vertex int
	height int
}

func (a ChildrenByHeight) Less(i, j int) bool {
	return a[i].height > a[j].height
}

func (a ChildrenByHeight) Len() int {
	return len(a)
}

func (a ChildrenByHeight) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}