#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,j;
scanf("%d",&n);
int str[n][5];
for(i=0;i<n;i++){
for(j=0;j<4;j++){
scanf("%d",&str[i][j]);
}
}
for(i=0;i<n;i++){
if(str[i][1]-str[i][0]==str[i][3]-str[i][2])
str[i][4]=str[i][3]+(str[i][3]-str[i][2]);
else
str[i][4]=str[i][3]*(str[i][3]/str[i][2]);
printf("%d",str[i][5]);
}
return 0;
}