/* *********************************************** Author :kuangbin Created Time :2016/3/5 18:58:26 File Name :F:\ACM\2016ACM\BestCoder\BC74\A.cpp ************************************************ */ #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int a[100]; int cnt; int gethash(int n) { return lower_bound(a,a+cnt,n)-a; } int dis[20][20]; const int MOD = 1e9+7; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int n,m; scanf("%d",&T); int a1,a2,a3; int b1,b2,b3; while(T--) { scanf("%d%d",&n,&m); cnt = 0; scanf("%d%d",&a1,&b1); a[cnt++] = a1; a[cnt++] = b1; scanf("%d%d",&a2,&b2); a[cnt++] = a2; a[cnt++] = b2; scanf("%d%d",&a3,&b3); a[cnt++] = a3; a[cnt++] = b3; sort(a,a+cnt); cnt = unique(a,a+cnt)-a; for(int i = 0;i < cnt;i++) for(int j = 0;j < cnt;j++) dis[i][j] = abs(a[i]-a[j]); int t1,t2; t1 = gethash(a1); t2 = gethash(b1); dis[t1][t2] = dis[t2][t1] = min(dis[t1][t2],1); t1 = gethash(a2); t2 = gethash(b2); dis[t1][t2] = dis[t2][t1] = min(dis[t1][t2],1); t1 = gethash(a3); t2 = gethash(b3); dis[t1][t2] = dis[t2][t1] = min(dis[t1][t2],1); for(int k = 0;k < cnt;k++) for(int i = 0;i < cnt;i++) for(int j = 0;j < cnt;j++) dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]); int s,t; long long ans = 0; for(int k = 1;k <= m;k++) { scanf("%d%d",&s,&t); int tmp = abs(s-t); for(int i = 0;i < cnt;i++) for(int j = 0;j < cnt;j++) { tmp = min(tmp,abs(a[i]-s) + abs(a[j]-t) + dis[i][j]); } ans = (ans + (long long)tmp*k)%MOD; } printf("%d\n",(int)ans); } return 0; }