#include<bits/stdc++.h> #define P 1000000007 usingnamespace std; typedeflonglong lnt; template <classT> inlinevoidread(T &x){ x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, n1, n2, r[20]; lnt s, sa, sb, sc; bool mrk[20]; lnt inv(lnt x){ lnt ret = 1; for (int k = P-2; k; k >>= 1, x = x*x%P) if (k&1) ret = ret*x%P; return ret; } lnt C(lnt n, int m){ lnt ret = 1; for (int i = 1; i <= m; i++) ret = ret*(n-i+1)%P; for (int i = 1; i <= m; i++) ret = ret*inv(i)%P; return ret; } voidupd(lnt &res){ lnt ss = s, f = 1; for (int i = 1; i <= n; i++) if (mrk[i]) ss -= (r[i]+1), f *= -1; if (ss >= 0) res = (res+f*C(ss+n, n)+P)%P; } voidDFS(int stp, lnt &res){ if (s < 0) {res = 0; return;} if (stp > n) {upd(res); return;} mrk[stp] = true, DFS(stp+1, res); mrk[stp] = false, DFS(stp+1, res); } intmain(){ int T; read(T); while (T--) { read(n1), s = sa = sb = sc = 0; for (int i = 1, L, R; i <= n1; i++) read(L), read(R), r[i] = R-L, s += R; read(n2); for (int i = 1, L, R; i <= n2; i++) read(L), read(R), r[i+n1] = R-L, s -= L; n = n1+n2, r[n+1] = 0x7f7f7f7f; s--, DFS(1, sa), s++, DFS(1, sc), sc = (sc-sa+P)%P; for (int i = 1; i <= n; i++) sa = sa*inv(r[i]+1)%P; for (int i = 1; i <= n; i++) sc = sc*inv(r[i]+1)%P; sb = (1-sa-sc+P+P)%P, printf("%lld %lld %lld\n", sa, sc, sb); } return0; }