#include #include #include #include #include #include using namespace std; #define inf 0x3f3f3f3f #define LL long long #define ls i << 1 #define rs ls | 1 #define md (ll + rr >> 1) #define lson ll, md, ls #define rson md + 1, rr, rs #define mod 1000000007 #define N 100010 #define M 300020 int fst[N], vv[M], nxt[M], e; void init(){ memset(fst, -1, sizeof fst), e = 0; } void add(int u, int v){ vv[e] = v, nxt[e] = fst[u], fst[u] = e++; } bool vis[N]; void bfs(int *dis, int s){ dis[s] = 0; vis[s] = 1; queue q; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); for(int i = fst[u]; ~i; i = nxt[i]){ int v = vv[i]; if(!vis[v] && dis[v] > dis[u] + 1){ vis[v] = 1, dis[v] = dis[u] + 1; q.push(v); } } } } int dis[8][N], a[N]; int main(){ int cas; scanf("%d", &cas); while(cas--){ int n, m; scanf("%d%d", &n, &m); init(); for(int i = 1; i < n; ++i) add(i, i + 1), add(i + 1, i); for(int i = 0; i < 6; i += 2){ scanf("%d%d", &a[i], &a[i+1]); add(a[i+1], a[i]); add(a[i], a[i+1]); } memset(dis, 0x3f, sizeof dis); for(int i = 0; i < 6; ++i){ memset(vis, 0, sizeof vis); bfs(dis[i], a[i]); } int ans = 0; for(int i = 1; i <= m; ++i){ int u, v; scanf("%d%d", &u, &v); if(u > v) swap(u, v); int tmp = v - u; for(int j = 0; j < 6; ++j) tmp = min(tmp, dis[j][u] + dis[j][v]); ans = (ans + (LL)i * tmp % mod) % mod; } printf("%d\n", ans); } return 0; }