#include using namespace std; typedef long long ll; const int MAXN = 1e5+5; vector G[2*MAXN]; ll a[2*MAXN]; int main() { int T; scanf("%d", &T); while(T--) { int n, m, k; scanf("%d%d%d", &n, &m, &k); for(int i = 0; i <= n+m; i++) G[i].clear(); while(k--) { int x, y; scanf("%d%d", &x, &y); y += n; G[x].push_back(y); G[y].push_back(x); } for(int i = n+1; i <= n+m; i++) { a[i] = 0; for(int j = 0; j < G[i].size(); j++) { int v = G[i][j]; a[i] += (ll)G[v].size()-1LL; } } ll ans = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j < G[i].size(); j++) { int u = G[i][j]; ans += 2LL*(a[u]-(ll)G[i].size()+1LL); } } printf("%I64d\n", ans); } return 0; }