#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
vector<pair<int, int> > v_col[maxn];
vector<vector<int> > v;
vector<int> appear[maxn];
vector<int> temp[maxn];
int father[maxn], size[maxn];
bool visit[maxn];
int bigid[maxn];
vector<int> big;
const int bign = 450;
int ans[505][maxn];
int findroot(int x) {
  int root = x;
  for (; father[root] != -1; root = father[root])
    ;
  int temp;
  for (; father[x] != -1;) {
    temp = father[x];
    father[x] = root;
    x = temp;
  }
  return root;
}
void u(int x, int y) {
  x = findroot(x);
  y = findroot(y);
  if (x == y) return;
  if (size[x] > size[y]) swap(x, y);
  father[x] = y;
  size[y] += size[x];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int i;
  for (i = 0; i < m; i++) {
    int x, y, col;
    scanf("%d%d%d", &x, &y, &col);
    x--;
    y--;
    col--;
    v_col[col].push_back(make_pair(x, y));
  }
  for (i = 0; i < n; i++) {
    father[i] = -1;
    size[i] = 1;
    visit[i] = false;
  }
  for (i = 0; i < m; i++) {
    int j;
    for (j = 0; j < v_col[i].size(); j++) {
      u(v_col[i][j].first, v_col[i][j].second);
    }
    for (j = 0; j < v_col[i].size(); j++) {
      int t = findroot(v_col[i][j].first);
      if (!visit[v_col[i][j].first]) {
        visit[v_col[i][j].first] = true;
        temp[t].push_back(v_col[i][j].first);
      }
      if (!visit[v_col[i][j].second]) {
        visit[v_col[i][j].second] = true;
        temp[t].push_back(v_col[i][j].second);
      }
    }
    for (j = 0; j < v_col[i].size(); j++) {
      int t1 = v_col[i][j].first;
      int t2 = v_col[i][j].second;
      if (size[t1] < size[t2]) {
        swap(t1, t2);
      }
      if ((father[t1] == -1) && (visit[t1])) {
        v.push_back(temp[t1]);
        temp[t1].clear();
      }
      father[t1] = -1;
      size[t1] = 1;
      visit[t1] = false;
      father[t2] = -1;
      size[t2] = 1;
      visit[t2] = false;
    }
  }
  for (i = 0; i < v.size(); i++) {
    int j;
    for (j = 0; j < v[i].size(); j++) {
      appear[v[i][j]].push_back(i);
    }
  }
  for (i = 0; i < n; i++) {
    if (appear[i].size() > bign) {
      bigid[i] = big.size();
      big.push_back(i);
    } else {
      bigid[i] = -1;
    }
  }
  for (i = 0; i < big.size(); i++) {
    int t = big[i];
    int j;
    for (j = 0; j < appear[t].size(); j++) {
      int t2 = appear[t][j];
      int k;
      for (k = 0; k < v[t2].size(); k++) {
        ans[i][v[t2][k]]++;
      }
    }
  }
  int q;
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    int x1, x2;
    scanf("%d%d", &x1, &x2);
    x1--;
    x2--;
    if (bigid[x1] == -1) {
      swap(x1, x2);
    }
    if (bigid[x1] != -1) {
      printf("%d\n", ans[bigid[x1]][x2]);
    } else {
      int t1 = 0, t2 = 0;
      int ans = 0;
      for (;;) {
        if (t1 >= appear[x1].size()) break;
        if (t2 >= appear[x2].size()) break;
        int x = appear[x1][t1];
        int y = appear[x2][t2];
        if (x == y) {
          t1++;
          t2++;
          ans++;
          if (t1 >= appear[x1].size()) break;
          if (t2 >= appear[x2].size()) break;
        }
        if (x < y) {
          t1++;
          if (t1 >= appear[x1].size()) break;
        }
        if (x > y) {
          t2++;
          if (t2 >= appear[x2].size()) break;
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
