#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; const int MOD = 1000000007; int n, m, a[5], b[5], dis; int ans, x, y; bool p[5]; void dfs(int k, int now, int s, int all) { if (now > all) { dis = min(dis, s + abs(k - y)); return ; } for (int i = 1; i <= 3; i++) if (p[i] == 0) { p[i] = 1; dfs(b[i], now + 1, s + abs(k - a[i]) + 1, all); dfs(a[i], now + 1, s + abs(k - b[i]) + 1, all); p[i] = 0; } } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 1; i <= 3; i++) scanf("%d%d", &a[i], &b[i]); ans = 0; for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); dis = abs(x - y); for (int j = 0; j <= 3; j++) dfs(x, 1, 0, j); ans = ((ll) ans + (ll) i * (ll) dis % MOD ) % MOD; } printf("%d\n", ans); } }