#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  int tc;
  cin >> tc;

  while (tc--) {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    priority_queue<int> aq, bq;

    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      while (a[i] % 2 == 0) a[i] >>= 1;
      aq.push(a[i]);
    }

    for (int i = 0; i < n; ++i) {
      cin >> b[i];
      while (b[i] % 2 == 0) b[i] >>= 1;
      bq.push(b[i]);
    }

    while (!aq.empty() && !bq.empty()) {
      int aTop = aq.top(), bTop = bq.top();
      if (aTop == bTop) {
        aq.pop();
        bq.pop();
      } else if (aTop > bTop) {
        while (aTop > bTop) aTop >>= 1;
        aq.pop();
        aq.push(aTop);
      } else {
        break;
      }
    }

    cout << (aq.empty() && bq.empty() ? "DA\n" : "NE\n");
  }
  return 0;
}