#include <bits/stdc++.h>
using namespace std;
pair<int, int> find(vector<int> &v, int pos) {
  auto it = lower_bound(v.begin(), v.end(), pos);
  if (*it == pos) {
    return make_pair(it - v.begin(), 1);
  } else {
    return make_pair(it - v.begin(), 0);
  }
}
int t, n, m, k, xx, yy;
vector<pair<int, int> > xzam, yzam;
pair<int, int> fx, fy;
long long int res, curr;
vector<int> x, y;
int licz[1001000];
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++) {
      cin >> xx;
      x.push_back(xx);
    }
    for (int i = 0; i < m; i++) {
      cin >> yy;
      y.push_back(yy);
    }
    for (int i = 0; i < k; i++) {
      cin >> xx >> yy;
      fx = find(x, xx);
      fy = find(y, yy);
      if (fx.second) {
        if (!fy.second) {
          xzam.push_back(make_pair(xx, fy.first));
        }
      } else {
        yzam.push_back(make_pair(yy, fx.first));
      }
    }
    sort(xzam.begin(), xzam.end());
    sort(yzam.begin(), yzam.end());
    for (int i = 0; i <= xzam.size(); i++) {
      if (i > 0 && (i == xzam.size() || xzam[i] != xzam[i - 1])) {
        res += licz[xzam[i - 1].second] * curr;
        licz[xzam[i - 1].second] += curr;
        curr = 0;
      }
      if (i < xzam.size()) {
        curr++;
      }
    }
    for (int i = 0; i < xzam.size(); i++) {
      licz[xzam[i].second] = 0;
    }
    for (int i = 0; i <= yzam.size(); i++) {
      if (i > 0 && (i == yzam.size() || yzam[i] != yzam[i - 1])) {
        res += licz[yzam[i - 1].second] * curr;
        licz[yzam[i - 1].second] += curr;
        curr = 0;
      }
      if (i < yzam.size()) {
        curr++;
      }
    }
    for (int i = 0; i < yzam.size(); i++) {
      licz[yzam[i].second] = 0;
    }
    cout << res << endl;
    res = 0;
    xzam.clear();
    yzam.clear();
    x.clear();
    y.clear();
  }
}
