Problem: 2063C - Remove Exactly Two
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 2):
import sys,math,bisect,heapq
input=sys.stdin.readline
#remember file names are input.txt and output.txt 
#remember to pass only one argument to print
#add this lines before your program and after defining fast io(or that will over write testing)
#after testing done set flag =0
flag=0
if flag:
    class out:
        def __init__(self):
            self.fo=open("cp_with_python/cp/output.txt","w")
        def inu(self,*x):
            if str(type(x))!="<class 'str'>":
                x=str(x)
            self.fo.write(x)
            self.fo.write("\n")
    class inp:
        def __init__(self):
            self.fi=open("cp_with_python/cp/input.txt","r")
        def inu(self):
            return self.fi.readline()
    fy=out()
    print=fy.inu
    fx=inp()
    input=fx.inu

for _ in range(int(input())):
    n=int(input())
    #n,s1,s2=map(int,input().split())
    #a=list(map(int,input().split()))
    graph=[set() for i in range(n)]
    inc=[[i,0] for i in range(n)]
    for i in range(n-1):
        x,y=map(int,input().split())
        graph[x-1].add(y-1)
        graph[y-1].add(x-1)
        inc[x-1][1]+=1
        inc[y-1][1]+=1
    inc.sort(key=lambda x:x[1],reverse=True)
    ans=[0]*n
    if n==2:
        print(0)
    else:
        if inc[1][1]>inc[2][1]:
            if inc[1][0] in graph[inc[0][0]]:
                print(inc[0][1]+inc[1][1]-2)
            else:
                print(inc[0][1]+inc[1][1]-1)
        else:
            if inc[1][0] in graph[inc[0][0]]:
                if inc[2][0] in graph[inc[0][0]]:
                    print(max(inc[1][1]+inc[2][1]+1,inc[1][1]+inc[0][1])-2)
                else:
                    print(inc[0][1]+inc[2][1]-1)
            else:
                print(inc[0][1]+inc[1][1]-1)

Submission (verdict: Runtime error on test 1):
import sys,math,bisect,heapq
input=sys.stdin.readline
#remember file names are input.txt and output.txt 
#remember to pass only one argument to print
#add this lines before your program and after defining fast io(or that will over write testing)
#after testing done set flag =0
flag=1
if flag:
    class out:
        def __init__(self):
            self.fo=open("cp_with_python/cp/output.txt","w")
        def inu(self,*x):
            if str(type(x))!="<class 'str'>":
                x=str(x)
            self.fo.write(x)
            self.fo.write("\n")
    class inp:
        def __init__(self):
            self.fi=open("cp_with_python/cp/input.txt","r")
        def inu(self):
            return self.fi.readline()
    fy=out()
    print=fy.inu
    fx=inp()
    input=fx.inu

for _ in range(int(input())):
    n=int(input())
    #n,s1,s2=map(int,input().split())
    #a=list(map(int,input().split()))
    graph=[set() for i in range(n)]
    inc=[[i,0] for i in range(n)]
    for i in range(n-1):
        x,y=map(int,input().split())
        graph[x-1].add(y-1)
        graph[y-1].add(x-1)
        inc[x-1][1]+=1
        inc[y-1][1]+=1
    inc.sort(key=lambda x:x[1],reverse=True)
    while inc:
        if inc[-1][1]==1:
            inc.pop()
        else:
            break
    ans=[0]*n
    if len(inc)==0:
        print(0)
    elif len(inc)==1:
        print(n-2)
    elif len(inc)==2:
        print(inc[0][1]+inc[1][1]-2)
    else:
        if inc[1][1]>inc[2][1]:
            if inc[1][0] in graph[inc[0][0]]:
                print(inc[0][1]+inc[1][1]-2)
            else:
                print(inc[0][1]+inc[1][1]-1)
        else:
            if inc[1][0] in graph[inc[0][0]]:
                if inc[2][0] in graph[inc[0][0]]:
                    print(max(inc[1][1]+inc[2][1]+1,inc[1][1]+inc[0][1])-2)
                else:
                    print(inc[0][1]+inc[2][1]-1)
            else:
                print(inc[0][1]+inc[1][1]-1)