#include using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template inline string toString(T x) {ostringstream sout;sout< VI; typedef vector VVI; typedef vector VL; typedef vector> VVL; typedef vector VS; typedef pair P; typedef tuple tpl; #define ALL(a) (a).begin(),(a).end() #define SORT(c) sort((c).begin(),(c).end()) #define REVERSE(c) reverse((c).begin(),(c).end()) #define EXIST(m,v) (m).find((v)) != (m).end() #define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i) #define RREP(i,n) RFOR(i,n,0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL<<60; constexpr long long MOD = 1000000007; // 998244353; #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } struct mint { long long x; mint(long long x=0):x((x%MOD+MOD)%MOD){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) { if ((x += MOD-a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= MOD; if(x < 0) x += MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(long long t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD-2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res/=a; } }; void Main(){ int H,W; cin >> H >> W; vector hz(H,0), wz(W,0); int nz = 0; ll A[H][W]; REP(i,H)REP(j,W){ cin >> A[i][j]; if(A[i][j] == 0){ hz[i]++; wz[j]++; nz++; } } mint s[4][H+1][W+1]; REP(j,4)REP(i,H+1) s[j][i][0] = 1; REP(j,4)REP(i,W+1) s[j][0][i] = 1; REP(i,H)REP(j,W){ if(s[0][i+1][j].x==0 || s[0][i][j+1].x==0) s[0][i+1][j+1] = 0; else s[0][i+1][j+1] = s[0][i+1][j]*s[0][i][j+1]/s[0][i][j]*A[i][j]; } REP(i,H)REP(j,W){ if(s[1][i+1][j].x==0 || s[1][i][j+1].x==0) s[1][i+1][j+1] = 0; else s[1][i+1][j+1] = s[1][i+1][j]*s[1][i][j+1]/s[1][i][j]*A[H-1-i][j]; } REP(i,H)REP(j,W){ if(s[2][i+1][j].x==0 || s[2][i][j+1].x==0) s[2][i+1][j+1] = 0; else s[2][i+1][j+1] = s[2][i+1][j]*s[2][i][j+1]/s[2][i][j]*A[H-1-i][W-1-j]; } REP(i,H)REP(j,W){ if(s[3][i+1][j].x==0 || s[3][i][j+1].x==0) s[3][i+1][j+1] = 0; else s[3][i+1][j+1] = s[3][i+1][j]*s[3][i][j+1]/s[3][i][j]*A[i][W-1-j]; } int Q; cin >> Q; REP(_,Q){ int r,c; cin >> r >> c; VI h{r-1,H-r,H-r,r-1}, w{c-1,c-1,W-c,W-c}; mint ans(1); REP(i,4) ans *= s[i][h[i]][w[i]]; cout << ans.x << en; } return; } int main(void){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<>t; REP(_,t) Main(); return 0; }