include<iostream>
using namespace std;
struct pixel
{
float R;
float G;
float B;
};
struct pixel image[300][300];
struct pixe
{
int R;
int G;
int B;
};
struct pixe imag[300][300];
int main()
{
float ar,ag,ab,br,bg,bb;
int w,h;
while(cin>>ar>>br>>ag>>bg>>ab>>bb)
{
cin>>w>>h;
for(int i =0;i<h;i++)
{
for(int j=0;j<w;j++)
{
cin>>image[i][j].R>>image[i][j].G>>image[i][j].B;
imag[i][j].R=ar*image[i][j].R+br+0.5;
imag[i][j].G=ag*image[i][j].G+bg+0.5;
imag[i][j].B=ab*image[i][j].B+bb+0.5;
if(imag[i][j].R>255)imag[i][j].R=255;
if(imag[i][j].G>255)imag[i][j].G=255;
if(imag[i][j].B>255)imag[i][j].B=255;
if(imag[i][j].R<=0)imag[i][j].R=0;
if(imag[i][j].G<=0)imag[i][j].G=0;
if(imag[i][j].B<=0)imag[i][j].B=0;
}
}
cout<<w<<" "<<h<<endl;
for(int i =0;i<h;i++)
{
for(int j=0;j<w;j++)
{
cout<<imag[i][j].R<<" "<<imag[i][j].G<<" "<<imag[i][j].B<<" ";
}
}
cout<<endl;
}
return 0;
}