#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18 - 1;
void solve() {
  long long int a[3], b[3];
  for (int i = 0; i < 3; i++) cin >> a[i];
  for (int i = 0; i < 3; i++) cin >> b[i];
  long long int c = 0;
  long long int x = min(a[2], b[1]);
  c += 2 * x;
  a[2] -= x;
  b[1] -= x;
  for (int i = 2; i >= 0; i--) {
    x = min(a[2], b[i]);
    a[2] -= x;
    b[i] -= x;
  }
  x = min(a[1], b[1]);
  a[1] -= x;
  b[1] -= x;
  x = min(a[1], b[0]);
  a[1] -= x;
  b[0] -= x;
  x = min(a[1], b[2]);
  a[1] -= x;
  b[2] -= x;
  c -= x * 2;
  cout << c << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) solve();
}
