description: This is the easy version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved.
  
  A forest is an undirected graph without cycles (not necessarily connected).
  
  Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: 
  
    * After adding edges, both of their graphs are still forests. 
    * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. 
  
  
  
  Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.
  
  Input
  
  The first line contains three integers n, m_1 and m_2 (1 ≤ n ≤ 1000, 0 ≤ m_1, m_2 < n) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest.
  
  Each of the next m_1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the edges in Mocha's forest.
  
  Each of the next m_2 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the edges in Diana's forest.
  
  Output
  
  The first line contains only one integer h, the maximum number of edges Mocha and Diana can add (in each forest).
  
  Each of the next h lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the edge you add each time.
  
  If there are multiple correct answers, you can print any one of them.
  
  Examples
  
  Input
  
  
  3 2 2
  1 2
  2 3
  1 2
  1 3
  
  
  Output
  
  
  0
  
  
  Input
  
  
  5 3 2
  5 4
  2 1
  4 3
  4 3
  1 4
  
  
  Output
  
  
  1
  2 4
  
  
  Input
  
  
  8 1 2
  1 7
  2 6
  1 5
  
  
  Output
  
  
  5
  5 2
  2 3
  3 4
  4 7
  6 8
  
  Note
  
  In the first example, we cannot add any edge.
  
  In the second example, the initial forests are as follows.
  
  <image>
  
  We can add an edge (2, 4).
  
  <image>
solution: 
  import java.io.*;
  import java.util.*;
  
  public class MochaAndDiana2ndTrial {
  	static ArrayList<String> ans;
  	static HashSet<Integer> rep;
  
  	public static void main(String[] args) throws Exception {
  		Scanner sc = new Scanner(System.in);
  		PrintWriter pw = new PrintWriter(System.out);
  		int n = sc.nextInt();
  		int m1 = sc.nextInt();
  		int m2 = sc.nextInt();
  		UnionFind1(n);
  		UnionFind2(n);
  		ans = new ArrayList<>();
  		for (int i = 0; i < m1; i++) {
  			unionSet1(sc.nextInt() - 1, sc.nextInt() - 1);
  		}
  		for (int i = 0; i < m2; i++) {
  			unionSet2(sc.nextInt() - 1, sc.nextInt() - 1);
  		}
  		for (int i = 0; i < n; i++) {
  			findSet1(i);
  			findSet2(i);
  		}
  		for (int i = 0; i < n; i++) {
  			for (int j = i + 1; j < n; j++) {
  				if (!(isSameSet1(i, j) || isSameSet2(i, j))) {
  					ans.add((i + 1) + " " + (j + 1));
  					unionSet1(i, j);
  					unionSet2(i, j);
  				}
  			}
  		}
  		pw.println(ans.size());
  		for (int i = 0; i < ans.size(); i++)
  			pw.println(ans.get(i));
  
  		pw.close();
  	}
  
  	static int[] p1, rank1, setSize1;
  	static int numSets1;
  
  	static public void UnionFind1(int N) {
  		p1 = new int[numSets1 = N];
  		rank1 = new int[N];
  		setSize1 = new int[N];
  		for (int i = 0; i < N; i++) {
  			p1[i] = i;
  			setSize1[i] = 1;
  		}
  	}
  
  	static public int findSet1(int i) {
  		return p1[i] == i ? i : (p1[i] = findSet1(p1[i]));
  	}
  
  	static public boolean isSameSet1(int i, int j) {
  		return findSet1(i) == findSet1(j);
  	}
  
  	static public void unionSet1(int i, int j) {
  		if (isSameSet1(i, j))
  			return;
  		numSets1--;
  		int x = findSet1(i), y = findSet1(j);
  		if (rank1[x] > rank1[y]) {
  			p1[y] = x;
  			setSize1[x] += setSize1[y];
  		} else {
  			p1[x] = y;
  			setSize1[y] += setSize1[x];
  			if (rank1[x] == rank1[y])
  				rank1[y]++;
  		}
  	}
  
  	static public int numDisjointSets1() {
  		return numSets1;
  	}
  
  	static public int sizeOfSet1(int i) {
  		return setSize1[findSet1(i)];
  	}
  
  	static int[] p2, rank2, setSize2;
  	static int numSets2;
  
  	static public void UnionFind2(int N) {
  		p2 = new int[numSets2 = N];
  		rank2 = new int[N];
  		setSize2 = new int[N];
  		for (int i = 0; i < N; i++) {
  			p2[i] = i;
  			setSize2[i] = 1;
  		}
  	}
  
  	static public int findSet2(int i) {
  		return p2[i] == i ? i : (p2[i] = findSet2(p2[i]));
  	}
  
  	static public boolean isSameSet2(int i, int j) {
  		return findSet2(i) == findSet2(j);
  	}
  
  	static public void unionSet2(int i, int j) {
  		if (isSameSet2(i, j))
  			return;
  		numSets2--;
  		int x = findSet2(i), y = findSet2(j);
  		if (rank2[x] > rank2[y]) {
  			p2[y] = x;
  			setSize2[x] += setSize2[y];
  		} else {
  			p2[x] = y;
  			setSize2[y] += setSize2[x];
  			if (rank2[x] == rank2[y])
  				rank2[y]++;
  		}
  	}
  
  	static public int numDisjointSets2() {
  		return numSets2;
  	}
  
  	static public int sizeOfSet2(int i) {
  		return setSize2[findSet2(i)];
  	}
  
  	// --------------------sTufF ----------------------
  	static class Scanner {
  		StringTokenizer st;
  		BufferedReader br;
  
  		public Scanner(InputStream s) {
  			br = new BufferedReader(new InputStreamReader(s));
  		}
  
  		public Scanner(FileReader r) {
  			br = new BufferedReader(r);
  		}
  
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  
  		public double nextDouble() throws IOException {
  			String x = next();
  			StringBuilder sb = new StringBuilder("0");
  			double res = 0, f = 1;
  			boolean dec = false, neg = false;
  			int start = 0;
  			if (x.charAt(0) == '-') {
  				neg = true;
  				start++;
  			}
  			for (int i = start; i < x.length(); i++)
  				if (x.charAt(i) == '.') {
  					res = Long.parseLong(sb.toString());
  					sb = new StringBuilder("0");
  					dec = true;
  				} else {
  					sb.append(x.charAt(i));
  					if (dec)
  						f *= 10;
  				}
  			res += Long.parseLong(sb.toString()) / f;
  			return res * (neg ? -1 : 1);
  		}
  
  		public long[] nextlongArray(int n) throws IOException {
  			long[] a = new long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public Long[] nextLongArray(int n) throws IOException {
  			Long[] a = new Long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public int[] nextIntArray(int n) throws IOException {
  			int[] a = new int[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public Integer[] nextIntegerArray(int n) throws IOException {
  			Integer[] a = new Integer[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  	}
  
  }
  