@aayushvrshney The way you are writing code you may miss some cases( as a case mentioned by @carre ) , so all we have to care about is that what max sum and value cells can handle and this can be found at very beginning of the program and then fill matrix accordingly , here there will be one edge case when `n == 1 && m == 1`

that you have also covered in your code.

```
#include <bits/stdc++.h>
using namespace std;
int n, m , x , y , ans ;
void solve()
{
cin >> n >> m >> x >> y ;
if(n == 1 && m == 1)
ans = x;
else
{
x = min( x , y ) ; /// WE CAN HAVE ATMOST X IN SINGLE GRID
y = min( 2 * x , y ) ; /// SUM OF TWO GRIDS CAN BE ATMOST Y AND IT CANNOT EXEED 2*x
ans = ( ( n * m + 1 ) / 2 ) * x + ( n * m / 2 ) * ( y - x ) ;
}
cout << ans << endl ;
}
int main() {
int t;
cin>>t;
while(t--)
solve() ;
}
```