#include<stdio.h>
int main(void) {
int n, m, d, m2, d2;
long long num;
int a[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int sum_day, i;
scanf("%d", &n);
while (n--) {
scanf("%d%d%lld%d%d",&m,&d, &num,&m2,&d2);
if (m == m2)//相同月份
sum_day = d2 - d;
else {
sum_day = a[m] - d + d2;//相邻月份
if (m2 - m != 1) {//不相邻月份
for (i = m + 1; i < m2; i++)
sum_day += a[i];
}
}
for (i = 0; i < sum_day; i++)
num *= 2;
printf("%lld\n", num);
}
return 0;
}