using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, d, w) = (c[0], c[1], c[2]); var dlist = NMap(d); var wlist = NMap(w); var dur = new UnionFindTree(n); foreach (var edge in dlist) dur.Unite(edge[0], edge[1]); var wur = new UnionFindTree(n); foreach (var edge in wlist) wur.Unite(edge[0], edge[1]); var wgroups = new HashSet<int>[n]; for (var i = 0; i < n; ++i) wgroups[i] = new HashSet<int>(); for (var i = 0; i < n; ++i) wgroups[dur.GetRoot(i)].Add(wur.GetRoot(i)); var wcontains = new Dictionary<int, int>[n]; for (var i = 0; i < n; ++i) wcontains[i] = new Dictionary<int, int>(); for (var i = 0; i < n; ++i) { var dr = dur.GetRoot(i); var wr = wur.GetRoot(i); if (wcontains[wr].ContainsKey(dr)) ++wcontains[wr][dr]; else wcontains[wr][dr] = 1; } var ans = 0L; for (var i = 0; i < n; ++i) { if (i != dur.GetRoot(i)) continue; ans += (dur.GetSize(i) - 1L) * dur.GetSize(i); foreach (var wr in wgroups[i]) ans += ((long)wur.GetSize(wr) - wcontains[wr][i]) * dur.GetSize(i); } for (var i = 0; i < n; ++i) { if (i != wur.GetRoot(i)) continue; } WriteLine(ans); } class UnionFindTree { int[] roots; public UnionFindTree(int size) { roots = new int[size]; for (var i = 0; i < size; ++i) roots[i] = -1; } public int GetRoot(int a) { if (roots[a] < 0) return a; return roots[a] = GetRoot(roots[a]); } public bool IsSameTree(int a, int b) { return GetRoot(a) == GetRoot(b); } public bool Unite(int a, int b) { var x = GetRoot(a); var y = GetRoot(b); if (x == y) return false; if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; } roots[x] += roots[y]; roots[y] = x; return true; } public int GetSize(int a) { return -roots[GetRoot(a)]; } } }