#include <bits/stdc++.h>
using namespace std;
vector<int> max_(2), min_(2);
vector<vector<int>> in_(2);
long long preob(bool st) {
  int x = 1;
  if (st == true) {
    x = 0;
  }
  bool st1 = false, st2 = true;
  auto it = in_[x].begin();
  for (int i = 0; i < in_[x].size(); ++i, it++) {
    if (*it == max_[x]) {
      st1 = true;
      if (i == 0) {
      } else {
        it--;
        if (min_[x] > *it - 1) {
          st2 = false;
        }
        it++;
      }
    }
    if (*it == min_[x] && st1 == true) {
      st2 = false;
    }
  }
  if (st2) {
    return abs(min_[x]) + max_[x];
  }
  st1 = false, st2 = true;
  it = in_[x].begin();
  for (int i = 0; i < in_[x].size(); ++i, it++) {
    if ((*it) == min_[x]) {
      st1 = true;
      if (i == 0) {
      } else {
        it--;
        if (max_[x] < *it + 1) {
          st2 = false;
        }
        it++;
      }
    }
    if ((*it) == max_[x] && st1 == true) {
      st2 = false;
    }
  }
  if (st2) {
    return abs(min_[x]) + max_[x];
  } else {
    return abs(min_[x]) + max_[x] + 1;
  }
}
void solve() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string in_s;
  cin >> in_s;
  int coun_1 = 0, coun_2 = 0;
  max_ = {0, 0};
  min_ = {0, 0};
  in_[0].clear();
  in_[1].clear();
  in_[0].push_back(0);
  in_[1].push_back(0);
  for (int i = 0; i < in_s.size(); ++i) {
    if (in_s[i] == 'W') {
      coun_1++;
      in_[0].push_back(coun_1);
    }
    if (in_s[i] == 'S') {
      coun_1--;
      in_[0].push_back(coun_1);
    }
    if (in_s[i] == 'D') {
      coun_2++;
      in_[1].push_back(coun_2);
    }
    if (in_s[i] == 'A') {
      coun_2--;
      in_[1].push_back(coun_2);
    }
    max_[0] = max(coun_1, max_[0]);
    max_[1] = max(coun_2, max_[1]);
    min_[0] = min(coun_1, min_[0]);
    min_[1] = min(coun_2, min_[1]);
  }
  long long a = max_[0] + abs(min_[0]) + 1;
  long long b = max_[1] + abs(min_[1]) + 1;
  cout << min(a * preob(false), b * preob(true)) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
