#include <bits/stdc++.h>
using namespace std;
const int N = 3001;
int n, m, h[N];
vector<int> v[N];
vector<vector<pair<int, long long> > > dp;
pair<int, long long> operator+(pair<int, long long> l, pair<int, long long> r) {
  l.first += r.first;
  l.second += r.second;
  return l;
}
pair<int, long long> apply(pair<int, long long> a) {
  a.first += a.second > 0;
  a.second = 0;
  return a;
}
vector<pair<int, long long> > merge(vector<pair<int, long long> > &a,
                                    vector<pair<int, long long> > &b) {
  vector<pair<int, long long> > ret(a.size() + b.size() + 1, make_pair(-1, 0));
  for (int i = 0; i < (int)a.size(); i++) {
    for (int j = 0; j < (int)b.size(); j++) {
      ret[i + j] = max(ret[i + j], a[i] + b[j]);
      ret[i + j + 1] = max(ret[i + j + 1], a[i] + apply(b[j]));
    }
  }
  for (int i = 0; i + 1 < (int)ret.size(); i++) {
    ret[i + 1] = max(ret[i + 1], apply(ret[i]));
  }
  return ret;
}
void dfs(int node, int pnode) {
  dp[node].push_back(make_pair(0, h[node]));
  for (auto &i : v[node]) {
    if (i == pnode) continue;
    dfs(i, node);
    dp[node] = merge(dp[node], dp[i]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    dp.clear();
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      v[i].clear();
      h[i] = 0;
    }
    dp.resize(n);
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      h[i] -= x;
    }
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      h[i] += x;
    }
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      v[x].push_back(y);
      v[y].push_back(x);
    }
    dfs(0, -1);
    cout << dp[0][m].first << endl;
  }
}
