#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
using namespace std;
const int MAXN = 100005;
const int oo = 1e9+7;
struct Edge{int v, flow, next;}edge[MAXN<<1];
int Head[MAXN], cnt;
int layer[MAXN];
void InIt(int N)
{
cnt = 0;
memset(Head, -1, sizeof(Head));
}
void AddEdge(int u, int v, int flow)
{
edge[cnt].v = v;
edge[cnt].flow = flow;
edge[cnt].next = Head[u];
Head[u] = cnt++;
}
bool bfs(int start, int End)
{
queue<int>Q;Q.push(start);
memset(layer, 0, sizeof(layer));
layer[start] = 1;
while(Q.size())
{
int u = Q.front();Q.pop();
if(u == End)return true;
for(int j=Head[u]; j!=-1; j=edge[j].next)
{
int v = edge[j].v;
if(layer[v] == false && edge[j].flow)
{
layer[v] = layer[u] + 1;
Q.push(v);
}
}
}
return false;
}
int dfs(int u, int MaxFlow, int End)
{
if(u == End)return MaxFlow;
int uflow = 0;
for(int j=Head[u]; j!=-1; j=edge[j].next)
{
int v = edge[j].v;
if(layer[v]-1 == layer[u] && edge[j].flow)
{
int flow = min(MaxFlow-uflow, edge[j].flow);
flow = dfs(v, flow, End);
edge[j].flow -= flow;
edge[j^1].flow += flow;
uflow += flow;
if(uflow == MaxFlow)
break;
}
}
if(uflow == 0)///优化防止重搜,不优化会超时
layer[u] = 0;
return uflow;
}
int dinic(int start, int End, int N)
{
int MaxFlow = 0;
while(bfs(start, End) == true)
MaxFlow += dfs(start, oo, End);
return MaxFlow;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i, N, M, x, y, east=-oo, west=oo, start, End;
scanf("%d%d", &N, &M);
InIt(N);
for(i=1; i<=N; i++)
{
scanf("%d%d", &x, &y);
if(west > x)
{
west = x;
start = i;
}
if(east < x)
{
east = x;
End = i;
}
}
while(M--)
{
int u, v, flow;
scanf("%d%d%d", &u, &v, &flow);
AddEdge(u, v, flow);
AddEdge(v, u, flow);
}
printf("%d\n", dinic(start, End, N));
}
return 0;
}