We need to solve: given k pairs (boy_i, girl_i). Count number of unordered pairs of these pairs (i, j) (i < j) such that the two pairs are disjoint (no common boy or girl). Equivalent to selecting two edges from a bipartite graph with left vertices (boys 1..a) and right vertices (girls 1..b), edges given. Count number of unordered edge pairs that are vertex-disjoint.
Constraints: a,b,k up to 2e5 per test case, and sums of a,b,k across test cases each ≤ 2e5. t up to 1e4. So overall we can afford O(k log something) per test case.
We need to compute total number of unordered pairs of edges that are not sharing a vertex.
Let total number of unordered pairs = C(k,2). From this subtract pairs that share a boy or share a girl. But careful: pairs that share both a boy and a girl? That would be the same edge (duplicate edges are not allowed; each pair is unique). So we can't have two different edges that share both boy and girl, as they would be same pair. So overlapping only in one side? Actually two distinct edges can share a boy but different girls; or share a girl but different boys; or share both? That would be same edge, so not possible. So we need to subtract pairs that share at least one vertex. Use inclusion-exclusion: count pairs that share a boy + pairs that share a girl - pairs that share both boy and girl? But as noted, pairs that share both are zero. So answer = total - (number of pairs sharing a boy) - (number of pairs sharing a girl) + (number of pairs sharing both). But since sharing both is impossible, answer = total - sum over boys of C(cnt_b[boy], 2) - sum over girls of C(cnt_g[girl], 2). But is that correct? Consider a pair that shares a boy and also shares a girl? That can't happen because if two edges share a boy and also share a girl, then they would be the same edge (boy and girl same). So indeed no double counting between boy-sharing and girl-sharing? Actually a pair of edges that share a boy automatically do not share a girl because if they shared both they'd be identical. So sets of pairs sharing a boy and pairs sharing a girl are disjoint. But if we subtract both, we won't subtract any pair twice because no pair belongs to both categories. However, some pairs are counted in both categories if they share both? But none exist. So we can just subtract both sums. But careful: Is there any pair that shares a boy and a girl? That would be two edges (b,g1) and (b,g2) share boy; also they share? not girl. For a pair (b1,g) and (b2,g) share girl. But for a pair (b,g1) and (b,g2) it only shares boy, not girl. So it's not in both categories. So the sets are disjoint. So answer = C(k,2) - sum_{boy} C(cnt_b[boy],2) - sum_{girl} C(cnt_g[girl],2).
But is that correct? Let's test with example.
Test 1: a=3,b=4,k=4, pairs: (1,2), (1,3), (2,2), (3,4). Compute cnt_b: boy1:2, boy2:1, boy3:1 => sum C(2,2)=1, others 0 => total 1. cnt_g: girl2:2? Actually pairs: (1,2),(2,2) -> girl2 appears in two? also (1,2) and (2,2): yes girl2 appears in 2. girl3 appears in (1,3) -> 1, girl4 appears in (3,4) ->1. So cnt_g: girl2:2 => C(2,2)=1, others 0 => sum=1. total C(4,2)=6. Answer = 6 - 1 - 1 =4. matches.
Test 2: a=1,b=1,k=1. total=0, sums=0 => 0. matches.
Test 3: a=2,b=2,k=4, pairs: (1,1),(1,2),(2,1),(2,2). cnt_b: boy1:2, boy2:2 => C(2,2)=1 each => sum=2. cnt_g: girl1:2, girl2:2 => sum=2. total C(4,2)=6. 6-2-2=2. matches.
So the solution appears straightforward: For each test case, compute frequency of each boy among the k edges, frequency of each girl. Then compute answer = C(k,2) - sum_{x} C(cnt_b[x],2) - sum_{y} C(cnt_g[y],2). All modulo? It says print integer, no mod needed. But answer could be up to ~ (2e5 choose 2) ~ 2e10, fits in 64-bit. So use 64-bit (unsigned long long). So that's it.
However, careful: sums of a,b,k over all test cases ≤ 2e5. So we can allocate arrays of size a+1 and b+1 per test case, but since total across tests ≤2e5, we can allocate per test case and it's fine. But a and b individually can be up to 2e5 but sum across tests also ≤2e5. So we can use vectors sized a+1 and b+1 per test.
Implementation: read t. For each test: read a,b,k. Then read array of k ints boy list; then array of k ints girl list. Use two vectors cntB(a+1,0), cntG(b+1,0). Loop over edges i=0..k-1: boy = a_i, girl = b_i; cntB[boy]++; cntG[girl]++. Then compute totalPairs = (long long)k*(k-1)/2. Then for each cnt in cntB (from 1 to a) subtract cnt*(cnt-1)/2. Same for cntG. Output totalPairs - sumB - sumG.
We need to ensure we only iterate over boys that appear? But since a may be large but sum of k across tests is small, we could iterate over all a? But sum of a across tests ≤ 2e5, so we can iterate over full range [1..a] for each test. That is acceptable. Similarly for b.
Potential pitfalls: The input may have a or b up to 2e5 but sum over tests ≤2e5, so total loops over a's across all test cases is ≤2e5. Similarly b. So fine.
Time complexity: O(a+b+k) per test case, total across tests O(2e5) is fine.
But is there any catch? The problem statement: "It is guaranteed that the sums of a, b, and k over all test cases do not exceed 2 ⋅ 10^5." Usually it's "the sum of a over all test cases does not exceed 2e5" and similarly for b and k. So yes.
Thus solution is trivial.
But why is this problem interesting? Possibly there is a hidden difficulty: If we misinterpret and think overlapping both sides needs to be accounted? But we already deduced it's not needed. However, we must ensure the logic is sound: two edges are vertex-disjoint if they do not share any vertex. The complement: share at least one vertex. Since any two distinct edges can share at most one type of vertex (boy or girl) because sharing both would be same edge. So the number of pairs that share a vertex equals sum over vertices of C(deg(v),2) where v ranges over all boys and girls. Because each pair that shares a vertex is counted exactly once? Let's check: If two edges share a boy, they are counted in the sum over boys. Could they be also counted in the sum over girls? No, because they don't share a girl (if they share the boy, they have different girls; maybe could they also share a girl? If they share a boy and also share a girl, then the edges would be (b,g) and (b,g) same, not allowed). So they are counted exactly once in the boy sum. Similarly for girl-sharing. So indeed the sum over vertices of C(deg(v),2) counts each bad pair exactly once. So answer = total pairs - sum_v C(deg(v),2).
This is simpler than inclusion-exclusion. So yes.
Thus solution is straightforward.
Now implement.
We need to read t test cases. For each, allocate vectors.
But we must be careful: a and b can be as large as 2e5, but maybe we can allocate using global arrays sized max across tests? However sum of a across tests ≤ 2e5, but a might be large in a test with small k? Example: t=1, a=200000, b=200000, k=1. That's allowed because sum of a =200000 ≤200000, sum b=200000, sum k=1. So we need arrays of size 200001. That's fine. But if we allocate vectors of size a+1 for each test, total memory may be okay as we deallocate after each test. So fine.
Alternatively, we could use maps but not needed.
Edge Cases: k=0? But constraints say k≥1? Actually (1 ≤ a, b, k ≤ 2⋅10^5) so k≥1. So ok.
Now produce output.
Let's test with sample.
Implementation: use long long for calculations.
Potential overflow: k up to 2e5, C(k,2) ~ 2e10 fits in 64-bit signed long long (max ~9e18). So safe.
Now confirm that sum of a over tests is ≤ 2e5, so we won't have huge total allocations.
Will there be multiple test cases with large a but small k? Yes but total a sum ≤ 2e5, so sum of sizes of vectors allocated across tests is bounded by total a + total b + t? Actually each test we allocate vectors of size a+1 and b+1. Sum of (a+1) across tests is <= 2e5 + t, t up to 1e4, that's okay. Similarly for b.
Better: we can allocate using vectors with resize(a+1) and (b+1).
Now code.
But wait: There is nuance: The problem statement says "the sums of a, b, and k over all test cases do not exceed 2⋅10^5". It says sums of a, b, and k individually? Usually it means sum of a over all test cases ≤ 2e5, sum of b ≤ 2e5, sum of k ≤ 2e5. So yes.
Now implement:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int a, b, k;
cin >> a >> b >> k;
vector<int> boys(k), girls(k);
for (int i=0; i<k; i++) cin >> boys[i];
for (int i=0; i<k; i++) cin >> girls[i];
vector<int> cntB(a+1,0), cntG(b+1,0);
for (int i=0; i<k; i++) {
cntB[boys[i]]++;
cntG[girls[i]]++;
}
ll total = (ll)k * (k-1) / 2;
ll bad = 0;
for (int i=1; i<=a; i++) {
ll c = cntB[i];
bad += c * (c-1) / 2;
}
for (int i=1; i<=b; i++) {
ll c = cntG[i];
bad += c * (c-1) / 2;
}
ll ans = total - bad;
cout << ans << '\n';
}
return 0;
}
Check with examples: Should output 4,0,2.
We should verify on more cases: e.g., a=2,b=2,k=3, pairs: (1,1),(1,2),(2,1). Then cntB: 1->2,2->1 => sum C=1; cntG: 1->2,2->1 => sum C=1; total=3; answer=3-2=1. Actually possible disjoint pairs? Only (1,2) and (2,1) are disjoint? That's indeed one. Yes.
Another: a=1,b=2,k=2, pairs: (1,1),(1,2). They share boy, so no disjoint pairs; answer = C(2,2)=1 - C(2,2)=1 => 0. Good.
Now we must consider if the answer can be negative? No, because total >= sum over boys + sum over girls? Actually total may be less than sum of these two? But sum over boys + sum over girls counts each bad pair exactly once, so total >= sum. So nonnegative.
Thus solution correct.
But wait: The problem statement: "You need to choose two of these pairs so that no person is in more than one pair." That means we select two pairs (i.e., two edges) such that the four individuals are all distinct. That's exactly what we counted.
Yes.
So it's straightforward.
However, I want to double-check potential pitfalls: Because the sums of a, b, k are each ≤ 2e5, not the total of a+b+k. So using vector of size a+1 each test case is fine because sum of a over all tests ≤ 2e5. So total memory usage across tests at a given time might be only one test's vectors, each up to 2e5+1, which is fine.
Thus we can implement.
We should note that using a vector for each test and looping from 1..a may be inefficient if a is large and we have many test cases with small a. But sum of a ≤ 2e5, so total loops across all tests is ≤2e5, fine.
Alright.
One more nuance: There's potential that a or b could be up to 2e5, but the sum of a over tests may be less than that. Example: t=10^4, each with a=20, sum a=200k. So loops over 20 per test = 200k total. So fine.
Now final answer.
But wait, there's an alternative solution: Use counting of pairs by degrees, but we already have it.
Thus answer ready.
I'll provide final solution.
However, I notice something: In the problem statement they give the example: (1, 3) and (3, 4) are allowed? They share boy 3? Actually (1,3) and (3,4): boy 3 appears in both? Wait (1,3) has boy 1, girl 3; (3,4) has boy 3, girl 4. They share girl? No, they share no common person: boy1, girl3, boy3, girl4. So they are disjoint. Yes. So answer includes that.
Our calculation gave 4 which included that.
Ok.
Now implement in code.
But careful: Input format: first line of each test case contains a,b,k. Then second line contains k integers for boy indices. Third line for girl indices. So we need to read them accordingly.
Edge Cases: Ensure we read using int for a,b,k. Use long long for answer.
Will output as integer.
Now test with provided sample:
Input:
3
3 4 4
1 1 2 3
2 3 2 4
1 1 1
1
1
2 2 4
1 1 2 2
1 2 1 2
Our code yields:
first: total 6 - boy_bad 1 - girl_bad 1 =4
second: total 0 - 0 -0 =0 (k=1 => total 0)
third: total 6 - boy_bad (1+1)=2 - girl_bad (1+1)=2 =2
matches.
Thus final answer.
Now maybe we can think about big test: k=200000 all edges with distinct boys and girls? But then cntB each 1, cntG each 1 => bad=0 => answer = C(200000,2) large ~ 2e10. That's fine.
Now possible improvements: we can avoid iterating from 1..a and 1..b if we only use vertices that appear. But not necessary.
Alright.
Now write final answer in the required format.
But there might be a twist: The problem says "the sums of a, b, and k over all test cases do not exceed 2e5". This might be ambiguous: could it be that the sum of a, the sum of b, and the sum of k each separately ≤ 2e5? Yes typical CF problems phrase: "It is guaranteed that the sums of a, b, and k over all test cases do not exceed 2 ⋅ 10^5." Means sum_a ≤ 2e5, sum_b ≤ 2e5, sum_k ≤ 2e5. So we are fine.
Thus final solution.
Now I'll produce final answer.
But wait, maybe I'm missing something: The problem says "choose two of these pairs so that no person is in more than one pair." That means we need to select two pairs that are vertex-disjoint. But what about ordering? They consider ways different if they consist of different pairs. So unordered pairs.
Thus answer is correct.
So we can code and submit.
But some might think about double counting of bad pairs if both share a boy and a girl? Actually we argued it's impossible. But is it impossible? Suppose we have edges (b,g1) and (b2,g). They share? They don't share either. That's fine. For two edges to share both a boy and a girl, they'd have to have same boy and same girl, meaning same edge. Since edges are distinct, that cannot happen. So sets are disjoint.
Thus solution is valid.
Now implement. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int a, b, k;
cin >> a >> b >> k;
vector<int> boys(k), girls(k);
for (int i = 0; i < k; ++i) cin >> boys[i];
for (int i = 0; i < k; ++i) cin >> girls[i];
vector<int> cntB(a + 1, 0), cntG(b + 1, 0);
for (int i = 0; i < k; ++i) {
cntB[boys[i]]++;
cntG[girls[i]]++;
}
ll total = (ll)k * (k - 1) / 2;
ll bad = 0;
for (int i = 1; i <= a; ++i) {
ll c = cntB[i];
bad += c * (c - 1) / 2;
}
for (int i = 1; i <= b; ++i) {
ll c = cntG[i];
bad += c * (c - 1) / 2;
}
ll ans = total - bad;
cout << ans << '\n';
}
return 0;
}