#include<bits/stdc++.h> usingnamespace std; constint N = 2e5 + 9; #define int long long
int a[N], c[N], ans; structNode { int l, r; booloperator<(const Node &u) const { return l < u.l; } }tmp[N];
voidmergesort(int bg, int ed) { if (bg == ed) return; int mid = (bg + ed) / 2, i = bg, j = mid + 1, k = bg; mergesort(bg, mid), mergesort(mid + 1, ed); while (i <= mid && j <= ed) { if (a[i] <= a[j]) c[k++] = a[i++]; else c[k++] = a[j++], ans += mid - i + 1; } while (i <= mid) c[k++] = a[i++]; while (j <= ed) c[k++] = a[j++]; for (int l = bg; l <= ed; l++) a[l] = c[l]; }
voidsolve() { int n; cin >> n; for (int i = 1; i <= n; i ++) cin >> tmp[i].l >> tmp[i].r; sort(tmp + 1, tmp + 1 + n); for (int i = 1; i <= n; i ++) a[i] = tmp[i].r; ans = 0; mergesort(1, n); cout << ans << '\n'; }
signedmain() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) solve(); return0; }