#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int INF = 1e9;
char s[N];
int a[2][N];
int premin[N], premax[N], sufmin[N], sufmax[N];
int calc(int x, int id, int d) {
  int recmin = min(premin[x - 1], sufmin[x] + d);
  recmin = min(recmin, a[id][x - 1] + d);
  int recmax = max(premax[x - 1], sufmax[x] + d);
  recmax = max(recmax, a[id][x - 1] + d);
  return recmax - recmin + 1;
}
int main() {
  int T;
  scanf("%d", &T);
  int n, cnt1, cnt2, ans, rec1, rec2, flag1, flag2;
  long long anss;
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    cnt1 = cnt2 = 0;
    for (int i = 1; i <= n; i++) {
      if (s[i] == 'A') {
        a[0][cnt1 + 1] = a[0][cnt1] - 1;
        cnt1++;
      } else if (s[i] == 'D') {
        a[0][cnt1 + 1] = a[0][cnt1] + 1;
        cnt1++;
      } else if (s[i] == 'S') {
        a[1][cnt2 + 1] = a[1][cnt2] - 1;
        cnt2++;
      } else {
        a[1][cnt2 + 1] = a[1][cnt2] + 1;
        cnt2++;
      }
    }
    premin[0] = premax[0] = 0;
    sufmin[cnt1 + 1] = INF;
    sufmax[cnt1 + 1] = -INF;
    for (int i = 1; i <= cnt1; i++) {
      premin[i] = min(premin[i - 1], a[0][i]);
      premax[i] = max(premax[i - 1], a[0][i]);
    }
    for (int i = cnt1; i >= 1; i--) {
      sufmin[i] = min(sufmin[i + 1], a[0][i]);
      sufmax[i] = max(sufmax[i + 1], a[0][i]);
    }
    rec1 = premax[cnt1] - premin[cnt1] + 1;
    ans = INF;
    for (int i = 1; i <= cnt1; i++) {
      ans = min(ans, calc(i, 0, -1));
      ans = min(ans, calc(i, 0, 1));
    }
    if (ans < rec1)
      flag1 = 1;
    else
      flag1 = 0;
    premin[0] = premax[0] = 0;
    sufmin[cnt2 + 1] = INF;
    sufmax[cnt2 + 1] = -INF;
    for (int i = 1; i <= cnt2; i++) {
      premin[i] = min(premin[i - 1], a[1][i]);
      premax[i] = max(premax[i - 1], a[1][i]);
    }
    for (int i = cnt2; i >= 1; i--) {
      sufmin[i] = min(sufmin[i + 1], a[1][i]);
      sufmax[i] = max(sufmax[i + 1], a[1][i]);
    }
    rec2 = premax[cnt2] - premin[cnt2] + 1;
    ans = INF;
    for (int i = 1; i <= cnt2; i++) {
      ans = min(ans, calc(i, 1, -1));
      ans = min(ans, calc(i, 1, 1));
    }
    if (ans < rec2)
      flag2 = 1;
    else
      flag2 = 0;
    anss = 1LL * rec1 * rec2;
    if (flag1) anss = min(anss, 1LL * (rec1 - 1) * rec2);
    if (flag2) anss = min(anss, 1LL * rec1 * (rec2 - 1));
    printf("%I64d\n", anss);
  }
  return 0;
}
