Problem: 2063C - Remove Exactly Two
Language: Java 21

Previous submission (verdict: Wrong answer on test 2):
import java.util.*;
import java.io.*;
import java.math.*;

public class sub {

	static int mod = 998244353;

	public static void main(String args[]) throws IOException {
		int t = sc.nextInt();
		while(t-->0) {
			int n = sc.nextInt();
			ArrayList<ArrayList<Integer>> adj = new ArrayList();
			for(int i = 0;i<n;i++)adj.add(new ArrayList());
			int deg[] = new int[n];
			for(int i = 0;i<n-1;i++) {
				int u = sc.nextInt()-1;
				int v = sc.nextInt()-1;
				adj.get(u).add(v);
				adj.get(v).add(u);
				deg[u]++;
				deg[v]++;
			}
			TreeMap<Integer, Integer> tm = new TreeMap(Collections.reverseOrder());
			for(int i = 0;i<deg.length;i++) {
				tm.put(deg[i], tm.getOrDefault(deg[i], 0)+1);
			}
			Set<Integer> set = tm.keySet();
			int ans = 0;
			for(int i = 0;i<n;i++) {
				for(int j : adj.get(i)) {
					ans = Math.max(ans, deg[j]+deg[i]-2);
					tm.put(deg[j], tm.get(deg[j])-1);
				}
				tm.put(deg[i], tm.get(deg[i])-1);
				//out.println(pq);
				for(int key :  set) {
					ans=Math.max(ans, deg[i]+tm.get(key)-1);
				}
				for(int j : adj.get(i)) {
					tm.put(deg[j], tm.get(deg[j])+1);
				}
				tm.put(deg[i], tm.get(deg[i])+1);
			}
			out.println(ans);
		}
		out.close();
	}

//	static long bincoef(int n, int k) {
//	    if (n < k) return 0L;
//	    return (p[n] * pow((p[n - k] * p[k]) % mod, mod - 2, mod)) % mod;
//	}
	
	static class Node implements Comparable<Node> {
        long cost;
        int u, v;
        Node(long cost, int u, int v) {
            this.cost = cost;
            this.u = u;
            this.v = v;
        }
        public int compareTo(Node o) {
            return Long.compare(this.cost, o.cost);
        }
    }

	static long pow(long a, long b, long mod) {
		if (b == 0) {
			return 1L;
		}
		long val = pow(a, b / 2, mod);
		if (b % 2 == 0) {
			return val * val % mod;
		}
		return val * val % mod * a % mod;
	}

	static long gcd(long a, long b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	static long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}

	static Kattio sc = new Kattio();
	static PrintWriter out = new PrintWriter(System.out);

	static class Kattio {

		static BufferedReader r;
		static StringTokenizer st;

		public Kattio() {
			r = new BufferedReader(new InputStreamReader(System.in));
		}

		public String next() {
			try {
				while (st == null || !st.hasMoreTokens()) {
					st = new StringTokenizer(r.readLine());
				}
				return st.nextToken();
			} catch (Exception e) {
				return null;
			}
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}

Submission (verdict: Time limit exceeded on test 19):
import java.util.*;
import java.io.*;
import java.math.*;

public class sub {

	static int mod = 998244353;

	public static void main(String args[]) throws IOException {
		int t = sc.nextInt();
		while(t-->0) {
			int n = sc.nextInt();
			ArrayList<ArrayList<Integer>> adj = new ArrayList();
			for(int i = 0;i<n;i++)adj.add(new ArrayList());
			int deg[] = new int[n];
			for(int i = 0;i<n-1;i++) {
				int u = sc.nextInt()-1;
				int v = sc.nextInt()-1;
				adj.get(u).add(v);
				adj.get(v).add(u);
				deg[u]++;
				deg[v]++;
			}
			TreeMap<Integer, Integer> tm = new TreeMap(Collections.reverseOrder());
			for(int i = 0;i<deg.length;i++) {
				tm.put(deg[i], tm.getOrDefault(deg[i], 0)+1);
			}
			Set<Integer> set = tm.keySet();
			int ans = 0;
			for(int i = 0;i<n;i++) {
				for(int j : adj.get(i)) {
					ans = Math.max(ans, deg[j]+deg[i]-2);
					tm.put(deg[j], tm.get(deg[j])-1);
				}
				tm.put(deg[i], tm.get(deg[i])-1);
				//out.println(pq);
				for(int key :  set) {
					ans=Math.max(ans, deg[i]+(tm.get(key)>0 ? key : 0)-1);
				}
				for(int j : adj.get(i)) {
					tm.put(deg[j], tm.get(deg[j])+1);
				}
				tm.put(deg[i], tm.get(deg[i])+1);
			}
			out.println(ans);
		}
		out.close();
	}

//	static long bincoef(int n, int k) {
//	    if (n < k) return 0L;
//	    return (p[n] * pow((p[n - k] * p[k]) % mod, mod - 2, mod)) % mod;
//	}
	
	static class Node implements Comparable<Node> {
        long cost;
        int u, v;
        Node(long cost, int u, int v) {
            this.cost = cost;
            this.u = u;
            this.v = v;
        }
        public int compareTo(Node o) {
            return Long.compare(this.cost, o.cost);
        }
    }

	static long pow(long a, long b, long mod) {
		if (b == 0) {
			return 1L;
		}
		long val = pow(a, b / 2, mod);
		if (b % 2 == 0) {
			return val * val % mod;
		}
		return val * val % mod * a % mod;
	}

	static long gcd(long a, long b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	static long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}

	static Kattio sc = new Kattio();
	static PrintWriter out = new PrintWriter(System.out);

	static class Kattio {

		static BufferedReader r;
		static StringTokenizer st;

		public Kattio() {
			r = new BufferedReader(new InputStreamReader(System.in));
		}

		public String next() {
			try {
				while (st == null || !st.hasMoreTokens()) {
					st = new StringTokenizer(r.readLine());
				}
				return st.nextToken();
			} catch (Exception e) {
				return null;
			}
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}